aspose file tools*
The moose likes Servlets and the fly likes File Download in Servlets Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "File Download in Servlets" Watch "File Download in Servlets" New topic
Author

File Download in Servlets

ford Darcy Jr
Ranch Hand

Joined: Jan 26, 2005
Posts: 76
Hi,

I am able to upload files using com.oreilly.servlet.MultipartRequest.
But the problem is the user should be able to view the uploaded file. Can anybody please let me know how to go about achieving this.

Thanks.
James Carman
Ranch Hand

Joined: Feb 20, 2001
Posts: 580
You can stream it back to them if you want using the ServletOutputStream found on the response. However, if this type of file needs some sort of handler on the client (MS Word, JPG, etc.), then you will have to set the contentType and contentLength properties on the response also. For JPG images...

byte[] imageData = <get your image data from disk, db, wherever here>;
response.setContentType( "image/jpg" );
response.setContentLength( imageData.length );
response.getOutpuStream().write( imageData );


James Carman, President<br />Carman Consulting, Inc.
ford Darcy Jr
Ranch Hand

Joined: Jan 26, 2005
Posts: 76
Hi James Carman,

Thanks for your response. But can you please provide me with an sample code which will give me an idea as to how to achieve this.

Thanks.
James Carman
Ranch Hand

Joined: Feb 20, 2001
Posts: 580
Where are your files stored (filesystem directory relative to the servlet context root, database, etc.)? What type of files are they (JPG, PDF, Word, MP3 :-)?
[ February 16, 2005: Message edited by: James Carman ]
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
I show a little upload confirmation page with a link to the file. Could you just redirect them to the file URL?


A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
James Carman
Ranch Hand

Joined: Feb 20, 2001
Posts: 580
Originally posted by Stan James:
I show a little upload confirmation page with a link to the file. Could you just redirect them to the file URL?


If you're putting the file somewhere where it can be served by the "default" servlet (the one that serves static content), then by all means just use a link. Just make sure you save the file with the appropriate extension and you have the mime type mapping set up properly (probably already done for you).
ford Darcy Jr
Ranch Hand

Joined: Jan 26, 2005
Posts: 76
Hi James Carman,

I am able to upload all file types: text, word and pdf for now. The files are stored on the server.

I hope this helps.

Thanks.
ford Darcy Jr
Ranch Hand

Joined: Jan 26, 2005
Posts: 76
Hi,

Still struggling with File Download in servlets. Can anybody please tell me how to achieve this.

Thanks.
James Carman
Ranch Hand

Joined: Feb 20, 2001
Posts: 580
When you say that the files are stored on the server, are they available to the world via a regular URL, no servlet necessary? If so, then just use regular URLs to access them.
Carmon Colvin
Greenhorn

Joined: Feb 24, 2005
Posts: 2
I found this thread through google and am trying to do something simular with a servlet.

Here is my situation. I am writing a website for a client that has software to download. They only want it available for people to download if they have "registered" and then received an email with instructions for download. They do not want the software to be accessible via a normal URL link.

Here is what I have so far.



It works great, except.
1: This will not work if the file size is larger than Integer.MAX_VALUE. that is not a big issue but could be in the future. Is there anyway around this?

2: The "File name" in the Internet Explorer file download prompt takes on the name of the servlet. And "File type" is blank. Is there anyway to specify a file name and extension?


FYI: An example of how a "user" would access this is with a URL that looks like this.
http://localhost:8080/modeaus/DownloadRequest?action=download&validation=83ZIQU3892
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

Use Buffered streams.
In this one, I'm using ServletContext().getResourceAsStream but you could just as easily use a FileInputStream for getting the file.



Java API J2EE API Servlet Spec JSP Spec How to ask a question... Simple Servlet Examples jsonf
Carmon Colvin
Greenhorn

Joined: Feb 24, 2005
Posts: 2
Thanks, that is exactly what I needed.

Originally posted by Ben Souther:
[QB]Use Buffered streams.
In this one, I'm using ServletContext().getResourceAsStream but you could just as easily use a FileInputStream for getting the file.
Sean Sullivan
Ranch Hand

Joined: Sep 09, 2001
Posts: 427
Here's an example web application that demonstrates file upload and file download:

http://strutsblobapp.sourceforge.net/

Cheers,

-Sean
Horatio Westock
Ranch Hand

Joined: Feb 23, 2005
Posts: 221
Just to add to this thread, I have code almost the same as that sourceforge example. I've wondered how to make it support resume, but I've never really spent much time on it. I suppose the headers give info on the offset to start at, then you just begin writing out from that offset.

I remember looking into this before and my problem was that, as there was no session, they were always being redirected to login on every request. Then I forgot about it.

Has anyone done this successfully?
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: File Download in Servlets
 
Similar Threads
CD Rom auto run
ClientAbortException: java.net.SocketException: Software caused connection abort:socket write error
How do I set value in my test case method for testing Action Class?
CD Rom auto run
Could not parse configuration: /hibernate.cfg.xml