• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Initiate a download from a servlet

 
Aron Daburn
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 36
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Aron Daburn
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 64959
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Aron Daburn
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic