Hi there, I am trying to develop a servlet which check the url for file name, and then read this file from local filesystem, and sends it back to response. In order to read this file, I must first search the local file system, the read in this file, and then send it to response. I can not figure out how to find the file in local file system. Can anyone tell me how to do this? Thanks,
Joined: Mar 11, 2001
Hi Maha Anna, Thank you for your response. Now I understand the question (actually an assignment) I worked on. Just as you said, the assignment is: check the file name from URL, then read this file from local file system ( My understanding now is the server end), then check the file extension in order to set the MIME, then send this file to response. My question now is, how to read the file in server? How to get the input stream? The way my program running should be, I think, like this: after I type url like ..../myServlet/test.txt, then the file test.txt should be posted to the browser if this file exist on the server. So how should I implement this? Thanks, Mike
Joined: Jan 31, 2000
Mike, THis is my understanding of your assignment. 1. From browser, your professor may type
2. Your FileServlet will get invoked with request param fileName=test.*** where *** may vary from gif,.... pdf 3. Your FileServlet should do this 3.1 String fileName = request.getParameter("fileName); 3.2 check for fileType (gif/txt/pdf...etc) 3.3 set one more param named 'FileType' in request object request.setAttribute("FileType","pdf"); 3.4 forward to fileServ.jsp getServletContext.getRequestDispatcher().forward("/FileServ.jsp"); 4. In your fileServ.jsp 4.1 get FileType param val from request objetc
4.2 set the response object's MIME type according to FileType
4.3 Read the file contents and write to response object and flush it.