jQuery in Action, 2nd edition*
The moose likes Web Component Certification (SCWCD/OCPJWCD) and the fly likes resource file location? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Web Component Certification (SCWCD/OCPJWCD)
Bookmark "resource file location?" Watch "resource file location?" New topic

resource file location?

grant meng

Joined: Dec 05, 2002
Posts: 4
hi :
I have got a question when I try to practice how to use getOutputStream in the following example. The question is where shall I put the file bart.gif. I tried several location such as /WEB-INF/classes , docment-root. It always complains to me "file not found". Any thoughts on that. Thank you.

public void doGet(HttpServletRequest req,
HttpServletResponse res)
throws ServletException, IOException
File f = new File("bart.gif");
byte[] bytearray = new byte[(int) f.length()];
FileInputStream is = new FileInputStream(f);
OutputStream os = res.getOutputStream();
Madan, Gopal
Ranch Hand

Joined: Aug 13, 2001
Posts: 86
I tried this with a jpg (image/jpg - mime type).
The file was directly under the Web application DocumentRoot. I also got the same error. Only thing i changed is used a forward slash just before the image file name. So I guess in your case you would use
File f = new File ("/bart.gif")

Madan, Gopal
Ranch Hand

Joined: Aug 13, 2001
Posts: 86
I need to recant my suggestions in the previous post.
It then worked because my file was in the root drive (d:\). I realized and tested out that when you create a File instance the argument must be a path to the file you want to create File instance upon. So in your case it should be as follows:
File f = new File ("d:\\whateverdir\\whateversubfolder\\bart.gif");
I am using W2K hence the above file path.
I think your best bet for portablity and all is to use the getRequestDispatcher() and/or getNamedDispatcher() methods of ServletContext object.
Bhushan Jawle
Ranch Hand

Joined: Nov 22, 2001
Posts: 249
You need to use ServletContext's getResource() or getResourceAsStream() to do your job.
You can get ServletContext as getServletContext() of GenericServlet.
wood burning stoves
subject: resource file location?
Similar Threads
problem when sending a pdf file to the browser
How to download file from jsp page
Servlet download file writer is already used by this request
File upload and download use SSL?
Q. on setContentType()