File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

File Download in Servlets

 
ford Darcy Jr
Ranch Hand
Posts: 76
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 580
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 );
 
ford Darcy Jr
Ranch Hand
Posts: 76
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 580
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 8791
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I show a little upload confirmation page with a link to the file. Could you just redirect them to the file URL?
 
James Carman
Ranch Hand
Posts: 580
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 76
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 76
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

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

Thanks.
 
James Carman
Ranch Hand
Posts: 580
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 13411
Firefox Browser Redhat VI Editor
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Use Buffered streams.
In this one, I'm using ServletContext().getResourceAsStream but you could just as easily use a FileInputStream for getting the file.

 
Carmon Colvin
Greenhorn
Posts: 2
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 427
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here's an example web application that demonstrates file upload and file download:

http://strutsblobapp.sourceforge.net/

Cheers,

-Sean
 
Horatio Westock
Ranch Hand
Posts: 221
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic