Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How to read a file in local file system using servlet

 
mike nu
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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,
 
david, chen
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, there:
You may try to use "getServletContext().getRealPath("file name")"
like:
String realPath=getServletContext().getRealPath("/WEB-INF/");
Hope it works.
David
 
mike nu
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Dave,
Thank you for your reply. Is it possible for you servlet to read a file in local filesystem ( client)?
I tried your method, but it doesn't work.
Thanks,
Mike
 
maha anna
Ranch Hand
Posts: 1467
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Client? Servlets run in server end and can have access to server machine's local file system. All that's sent to browser type client is html , javascript, css etc.
Initially you mentioned you want to send a particular file which got from URL (probably sent from browser). You can upload files from client user's machines to server end. Is this what you want? If so there is an example in Jason Hunter's web site (MultipartRequest I think). Please make a search in this forum for the correct address.
regds
maha anna
[This message has been edited by maha anna (edited March 23, 2001).]
 
mike nu
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
maha anna
Ranch Hand
Posts: 1467
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mike,
THis is my understanding of your assignment.
1. From browser, your professor may type

etc

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.

For MIME std type please refer to this thread
http://www.javaranch.com/ubb/Forum7/HTML/001355.html
For reading and writing to response objects please refer to this thread. http://www.javaranch.com/ubb/Forum7/HTML/002654.html
This is overall logic for your assignment. You may have to build /modify according to your needs.
regds
maha anna
[This message has been edited by maha anna (edited March 24, 2001).]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic