1

Servlet is very good looking and reading files that have English names like hello.txt. It does not want to read files that have a Russian name, such pushkin.txt. Is anyone able to help to solve this problem?

Here is the code:

import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class servlet extends HttpServlet {
    /**
     * 
     */
    private static final long serialVersionUID = 1L;

    public static List<String> getFileNames(File directory, String extension) {

        List<String> list = new ArrayList<String>();
        File[] total = directory.listFiles();
        for (File file : total) {
            if (file.getName().endsWith(extension)) {
                list.add(file.getName());
            }
            if (file.isDirectory()) {
                List<String> tempList = getFileNames(file, extension);
                list.addAll(tempList);          
            }
        }
        return list;
    }

    @SuppressWarnings("resource")
    protected void doPost(HttpServletRequest request, HttpServletResponse response) 
            throws ServletException, IOException{ 
            response.setContentType("text/html; charset=UTF-8");
            String myName = request.getParameter("text");

            List<String> files = getFileNames(new File("C:\\Users\\vany\\Desktop\\test"), "txt");
            for (String string : files) {
            if (myName.equals(string)) {
                   try {
                        File file = new File("C:\\Users\\vany\\Desktop\\test\\" + string);
                        FileReader reader = new FileReader(file);
                        int b;
                        PrintWriter writer = response.getWriter();
                        writer.print("<html>");
                        writer.print("<head>");
                        writer.print("<title>HelloWorld</title>");
                        writer.print("<body>");
                        writer.write("<div>");
                        while((b = reader.read()) != -1) {
                            writer.write((char) b);
                        }
                        writer.write("</div>");
                        writer.print("</body>");
                        writer.print("</html>");

                    } 
                   finally {
                   if(reader != null)  {
                      try{
                         reader.close();
                      } catch (Exception e) {
                         e.printStackTrace();
                      }
                   }
                }
                }

            }
           }
    }

The question is relevant, the problem is not solved

Eric Scot
  • 197
  • 8
  • 1
    Is your problem solved or not? You accepted an answer, but didn't leave any feedback that the problem was solved and how it was solved. – BalusC Nov 10 '12 at 20:00
  • No, my problem is not solved – Eric Scot Nov 11 '12 at 06:38
  • А что вам оставить у меня есть gmail – Eric Scot Nov 11 '12 at 06:40
  • 1
    Why did you then accept an answer which didn't solve your problem? – BalusC Nov 11 '12 at 11:10
  • Excuse me, can you help, it reads files hello.txt for example, but does not read привет.txt – Eric Scot Nov 11 '12 at 11:29
  • If anyone knows the solution please write code – Eric Scot Nov 11 '12 at 13:57
  • 1
    I compared the code to your [previous question](http://stackoverflow.com/questions/13308637/encoding-in-the-servletsjava). First, why have you removed the `request.setCharacterEncoding("UTF-8")`? Now the request parameter "text" is not decoded as UTF-8 anymore (but instead as ISO-8859-1) and the filename comparison would never succeed. – BalusC Nov 12 '12 at 10:51
  • Thanks, but that still recommend? – Eric Scot Nov 12 '12 at 10:55
  • Thank you for your code to help me, but how to do now, so you can enter the file name without. Txt, can give a simple example, please – Eric Scot Nov 12 '12 at 11:00

3 Answers3

0

I thought that you have a problem whith the statements

for (String string : files) {
        if (myName.equals(string)) {

I would compare in this way

for (File file: files) {
        if (myName.equals(file.getName())) { 

I hope that it help you. Note: Thanks for the comments, you can try it.

Greetings

0

First of all I would use a debugger to check what's wrong with that code. It's quite difficult to find a bug without running the code. If you don't want to use a debugger print out all filenames found in the directory to ensure that some file names were found:

for (String string : files) {
    System.out.println(string)
    ....

If files were found I would check whether I have rights to write to them. It might be that the application has not proper permissions to write in selected directory.

Adam Sznajder
  • 9,108
  • 4
  • 39
  • 60
0

Are files "hello.txt" and pushkin.txt directly inside "C:\Users\vany\Desktop\test\" folder? Or is pushkin.txt file in another folder from "C:\Users\vany\Desktop\test\"?

Can you show us how you invoke the servlet?

If you have pushkin.txt in another folder and you invoke the servlet with something like "folder\pushkin.txt" it will not work because getFileNames() returns file names (without folder) and "myName.equals(string)" fails as "folder\pushkin.txt" is not equal to "pushkin.txt"

Dan Iliescu
  • 435
  • 2
  • 7