wood burning stoves*
The moose likes Servlets and the fly likes Initiate a download from a servlet Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "Initiate a download from a servlet" Watch "Initiate a download from a servlet" New topic
Author

Initiate a download from a servlet

Aron Daburn
Ranch Hand

Joined: Mar 18, 2007
Posts: 36
How would I go about initiating a download from within a servlet? I tried redirecting and RequestDispatching to it's URL but those didn't work.
Aron Daburn
Ranch Hand

Joined: Mar 18, 2007
Posts: 36
Setting the response's MIME type then redirecting will work, but how do go about it if you don't know its MIME type?

When you just click on the link to its URL you don't specify its MIME type. How can I get it to start the download of a file with just knowing its URL?
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

If you're interested, I have a sample application on my site that streams image files using a servlet.

http://simple.souther.us
Look for SimpleStream


Java API J2EE API Servlet Spec JSP Spec How to ask a question... Simple Servlet Examples jsonf
Aron Daburn
Ranch Hand

Joined: Mar 18, 2007
Posts: 36
Any way to just see the code plane and simple without having to go through the JAR?


Is there not just a plane and simple way to send the request/response to the file's URL to start the download without first knowing its MIME type?
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61437
    
  67

If you are using a redirect there is no need to fiddle with the mime type. Just redirect to the file to be downloaded and the type of the file will be set by the server and handled by the browser.

If you are going to stream the file yourself, then you need to set the response headers appropriately and write the file to the output stream -- no redirect involved.

P.S. What's so difficult about unpacking a jar to see example code?
[ May 08, 2007: Message edited by: Bear Bibeault ]

[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Aron Daburn
Ranch Hand

Joined: Mar 18, 2007
Posts: 36
I had a bug in my code that was throwing me off. The MIME content was previously set. When I had the redirect, it was trying to read it as HTML, which made me think you had to set the MIME type to the file type. I thought all you had to do was a simple redirect to begin with. Sorry for such the simple error and thanks for support.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Initiate a download from a servlet