File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Servlets and the fly likes Servlet Write a file to server Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "Servlet Write a file to server" Watch "Servlet Write a file to server" New topic
Author

Servlet Write a file to server

Joseph Mathew
Greenhorn

Joined: Apr 21, 2004
Posts: 19
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
Brahim Bakayoko
Ranch Hand

Joined: Aug 29, 2003
Posts: 155
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 ]

SCJP, SCWCD, SCBCD, IBM CSD WebSphere v5, <br />A+, MCP 2000 and 2000 server, CST, and few incompleted certification tracks.<br /> <br />Ivory Coast<br /> <br />Analyze your web Request/Response @ <a href="http://webtools.servehttp.com" target="_blank" rel="nofollow">http://webtools.servehttp.com</a> down for a while...
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12823
    
    5
TCH!
FileWriter is for character data - it will mangle a binary file. Use FileOutputStream instead.
Bill
Brahim Bakayoko
Ranch Hand

Joined: Aug 29, 2003
Posts: 155
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

Joined: Apr 21, 2004
Posts: 19
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

Joined: Aug 29, 2003
Posts: 155
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

Joined: Apr 21, 2004
Posts: 19
**********************************************************************

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.
Frank Carver
Sheriff

Joined: Jan 07, 1999
Posts: 6920
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:



Read about me at frankcarver.me ~ Raspberry Alpha Omega ~ Frank's Punchbarrel Blog
damien malone
Ranch Hand

Joined: May 06, 2003
Posts: 35
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
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Servlet Write a file to server