Win a copy of Escape Velocity: Better Metrics for Agile Teams this week in the Agile and Other Processes forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Tim Cooke
  • Paul Clapham
  • Jeanne Boyarsky
Sheriffs:
  • Ron McLeod
  • Frank Carver
  • Junilu Lacar
Saloon Keepers:
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Al Hobbs
  • Carey Brown
Bartenders:
  • Piet Souris
  • Frits Walraven
  • fred rosenberger

resource file location?

 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
{
res.setContentType("images/gif");
File f = new File("bart.gif");
byte[] bytearray = new byte[(int) f.length()];
FileInputStream is = new FileInputStream(f);
is.read(bytearray);
OutputStream os = res.getOutputStream();
os.write(bytearray);
os.flush();
}
 
Ranch Hand
Posts: 86
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Grant:
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")


HTH
 
Madan, Gopal
Ranch Hand
Posts: 86
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Grant:
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.
HTH
 
Ranch Hand
Posts: 252
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You need to use ServletContext's getResource() or getResourceAsStream() to do your job.
You can get ServletContext as getServletContext() of GenericServlet.
Thanks,
Bhushan
 
Why fit in when you were born to stand out? - Seuss. Tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic