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); 
8
  • 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. Commented Nov 1, 2012 at 8:32
  • Print the error stack by catch (Exception ex) { ex.printStackTrace(); System.exit(0); } and paste the error stack by editing your question. Commented Nov 1, 2012 at 8:32
  • please properly format your code before posting. It hurts my eyes Commented Nov 1, 2012 at 8:32
  • 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 Commented Nov 1, 2012 at 8:41
  • 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. Commented Nov 1, 2012 at 8:48

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

4 Comments

With catch understand you can set: e.printStackTrace(); and FileReader reader = new FileReader(file); We must then prescribe finally { close(reader); }
still on the screen does not print
@user1790746 any error ? did u remove and put pritstacktrace() ?
I made it.to tell you that I brought

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.