• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Downloading file

 
Ranch Hand
Posts: 56
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 118
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Phil
 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
reply
    Bookmark Topic Watch Topic
  • New Topic