• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Servlet Write a file to server

 
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello all,

Does anybody know the code to write a file to the server from a servlet. For example i read a image file from the database as blob and then want to create this image file temperorly on the server.

Thanks

Joseph
 
Ranch Hand
Posts: 155
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Write the binary data to a file.

byte[] image = ....

File f = new File("pathname");
FileOutputStream fw = new FileOutputStream(f);
fw.write(image, 0, image.length);
fw.close;
[ May 14, 2004: Message edited by: Brahim Bakayoko ]
 
Author and all-around good cowpoke
Posts: 13078
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
TCH!
FileWriter is for character data - it will mangle a binary file. Use FileOutputStream instead.
Bill
 
Brahim Bakayoko
Ranch Hand
Posts: 155
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by William Brogden:
TCH!
FileWriter is for character data - it will mangle a binary file. Use FileOutputStream instead.
Bill



shame shame shame...

It pays to read what you write... Fixed.
 
Joseph Mathew
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Actually what will be the path ? Because we will be in the web server....
I figured it out how to specify the path...

File file = new File(getServletContext().getRealPath("") +"output.html");

This will give the path to htdocs, then we can specify the rest of the path.
 
Brahim Bakayoko
Ranch Hand
Posts: 155
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Joseph BG:
Actually what will be the path ? Because we will be in the web server....
I figured it out how to specify the path...

File file = new File(getServletContext().getRealPath("") +"output.html");

This will give the path to htdocs, then we can specify the rest of the path.



The path does not have to be relative to any of the server environments.
Any network or file system path will do.
Plus, your code will return null for the path. It will also return null when the passed in path is in an archive or is simply a mapping.
 
Joseph Mathew
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
**********************************************************************

The path does not have to be relative to any of the server environments.
Any network or file system path will do.
Plus, your code will return null for the path. It will also return null when the passed in path is in an archive or is simply a mapping.

***********************************************************************
I agree any network or filesystem path will be fine. What if we dont know the filesystem itself on the webserver?

But getServletContext().getRealPath("") doesnt return null, it gives the real path to the context. For example if you have installed tomcat in /usr/local/tomacat/webapps/ROOT and the WEB-INF is inside ROOT ie /usr/local/tomacat/webapps/ROOT/WEB-INF. And if you use getServletContext().getRealPath("") on any of the servlet it will return /usr/local/tomacat/webapps/ROOT. I cant understand why you said it returns null.
 
Sheriff
Posts: 7001
6
Eclipse IDE Python C++ Debian Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think what was intended was to remind you that getServletContext().getRealPath("") can return null. Remember, Tomcat is not the only server out there, and there are even some that never "expand" war files, but read files straight from the archive. Assuming that you can write files inside a web application directory is a dangerous thing to do.

However, if all you need is a place to write some temporary information for use later, the servlet API already provides a pre-configured "temporary directory" for each web application. Try something like:

 
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
sounds like you are just trying to generate some kind of dynamic content. Rather than write the image to the disk and send the temp location to the browser, you could think about getting a servlet that sets its content type header to be jpg or what ever format your image is in, and the servlet just writes the contents of the blob out. The call to your sevlet would have the key to the image as a parameter. That way you wouldnt have to clean up images, and it would be a lot more efficient that writting the image to disk, getting the web server to then read the image from disk and serve it up.

If I am second guessing you incorrectly I apologies

Damien
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic