Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Downloading file

 
Giri Prasad
Ranch Hand
Posts: 56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 118
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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).]
 
Giri Prasad
Ranch Hand
Posts: 56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Phil
 
Joey Zhou
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic