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

Downloading file

Giri Prasad
Ranch Hand

Joined: Mar 08, 2001
Posts: 56
Hi I am trying to download a file using the servlet. I called the forward method of the RequestDispatchar to the URL the file is. But when I am trying to save it, the download window is not prompting the file type. This file is a .exe
I even tried setting the header Content-Discription using the setHeader method of HttpResponse.
Any help
Thanks
Phil Hanna
Ranch Hand

Joined: Apr 05, 2001
Posts: 118
If you forward to a URL using a request dispatcher, the target of the forward needs to be something that handles requests, like a servlet or JSP. It is not the file you want to download.
To download a file, you can read the file and write its contents to the servlet output stream, as long as you do the following:
1. Set content-type to "application/octet-stream"
2. Set content-length to the file size
3. Set a Content-Disposition header with the file name as shown below
This example uses a hard-coded file name, but you can get it any other way:

This will work unmodified in Netscape. In Internet Explorer, it's slightly more complicated. The URL that calls the servlet needs to end with the extension of the file type you're downloading. That is, if you're downloading an ".exe" file, the URL must end with ".exe". You can append the file name to the request as path info, which will be ignored by the servlet. That will be enough to get IE to trigger the appropriate file save dialog.

------------------
Phil Hanna
Author of :
JSP: The Complete Reference
Instant Java Servlets
[This message has been edited by Phil Hanna (edited April 17, 2001).]


Phil Hanna<BR>Sun Certified Programmer for the Java 2 Platform<BR>Author of :<BR><A HREF="http://www.amazon.com/exec/obidos/ASIN/0072127686/electricporkchop/107-3548162-1137317" TARGET=_blank rel="nofollow">JSP: The Complete Reference</A><BR><A HREF="http://www.amazon.com/exec/obidos/ASIN/0072124253/electricporkchop/107-3548162-1137317" TARGET=_blank rel="nofollow">Instant Java Servlets</A>
Giri Prasad
Ranch Hand

Joined: Mar 08, 2001
Posts: 56
Thanks Phil
Joey Zhou
Greenhorn

Joined: May 22, 2001
Posts: 3
Originally posted by Phil Hanna:
[B]If you forward to a URL using a request dispatcher, the target of the forward needs to be something that handles requests, like a servlet or JSP. It is not the file you want to download.
To download a file, you can read the file and write its contents to the servlet output stream, as long as you do the following:
1. Set content-type to "application/octet-stream"
2. Set content-length to the file size
3. Set a Content-Disposition header with the file name as shown below
This example uses a hard-coded file name, but you can get it any other way:

This will work unmodified in Netscape. In Internet Explorer, it's slightly more complicated. The URL that calls the servlet needs to end with the extension of the file type you're downloading. That is, if you're downloading an ".exe" file, the URL must end with ".exe". You can append the file name to the request as path info, which will be ignored by the servlet. That will be enough to get IE to trigger the appropriate file save dialog.
[/B]

I tried your download code shows above. It does popup the download dialog and download a file. However, whatever the file is, it olny download the html download page itself. I couldn't work out where was going wrong. Can you point out?
Thanks


Java, milk ...
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Downloading file