0

Please help check the code. Only I the beginner can write the corrected 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 { 
        request.setCharacterEncoding("utf8");
        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>");
                } 
                catch (Exception ex) {
                    System.exit(0);
                }
            }
        }
    }
}

The error may be here:

catch (Exception ex) {
    System.exit(0);
}

And here:

FileReader reader = new FileReader(file);
Emil
  • 7,220
  • 17
  • 76
  • 135
  • 2
    1) *"The error may be here"* Change that to `catch (Exception ex) { ex.printStackTrace(); System.exit(0); }` and copy post the result as an edit to the question. 2) Fix the code formatting for the code. – Andrew Thompson Nov 01 '12 at 08:32
  • please properly format your code before posting. It hurts my eyes – Abubakkar Nov 01 '12 at 08:32
  • Print the error stack by `catch (Exception ex) { ex.printStackTrace(); System.exit(0); }` and paste the error stack by editing your question. – Jacob Nov 01 '12 at 08:32
  • i'm sorry for making this code – user1790746 Nov 01 '12 at 08:32
  • can write already corrected the code I'm still a beginner in java – user1790746 Nov 01 '12 at 08:38
  • 2
    @user1790746 In order to see what is the reason for the error, you should add `ex.printStackTrace();` just above `System.exit(0);` and paste that error stack in your question, so that other people here could help to resolve your problem very fast. Regards – Jacob Nov 01 '12 at 08:41
  • Can you do it, because every time you try to edit the code I have that does not work – user1790746 Nov 01 '12 at 08:45
  • 2
    For some reason this question is ending in chaos... why is it a community wiki now? To me this q&a does not seem to be helpful, so I voted to close. – home Nov 01 '12 at 08:48

1 Answers1

2

Should not use System.exit(0) in servlet.

I would suggest you to read this first

About servlets in StackOverflow

Then here is a file downloading example by BalusC

Serve your files

Once you understand the concepts, you can fly with servlets. All the best.

Community
  • 1
  • 1
vels4j
  • 11,208
  • 5
  • 38
  • 63