File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Servlets and the fly likes Download file to a users PC Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "Download file to a users PC" Watch "Download file to a users PC" New topic

Download file to a users PC

Steve Campbell
Ranch Hand

Joined: Dec 18, 2000
Posts: 41
I've written a servlet which creates either a comma delimited file whose suffix is .csv, or a XML format file which ends with .xml. I create these using a FileWriter/BufferedWriter/PrintWriter combo and output to a directory under webapps\bax (using Tomcat). In my JSP program I have some HTML (<A Href) which downloads the file to the users pc (after a file save dialog box). I don't have a problem when the .csv file is clicked for download, but when the .xml is clicked, the file is output on the screen instead of the "file download" box. I tried changing the content type, but it didn't seem to help. Do I need to download these files by opening a port between the pc's and doing some kind of file io?
lee dalais
Ranch Hand

Joined: Feb 28, 2001
Posts: 67
thought of maybe zipping it? i guess using ZipOutputStream therefore when it gets downloaded it's smaller and the download box will appear.
Randall Twede
Ranch Hand

Joined: Oct 21, 2000
Posts: 4351

Good question. I hope you get some more replies. All I know is that if the server doesnt know how to display the file it assumes you want to download. I did that with a .class file using < a href
I would like to learn more about this as well.

Visit my download page
Jeroen Wenting
Ranch Hand

Joined: Oct 12, 2000
Posts: 5093
IE (and supposedly other browsers as well) is smart enough to check file extension as well as context type. Strangely, Excell is not smart enough to get a file from the browsercache unless it has an extension it understands, even when being passed the complete filename (it seems like Excell appends ".xls" to the filename if there is no known extension).
You might want to change both mime-type and extension and see what happens, but even then you may have problems with browsers that are smart enough to determine the mime-type on the fly based on the actual content of the file (IE especially might well try to parse the file itself first if no viewer is available and only give a 'save as' dialog if it fails (with XML it would never fail).

Frank Carver

Joined: Jan 07, 1999
Posts: 6920
Basically, IE is too arrogant. You might also try the "content-disposition" header to see if you can fool IE into giving a save-as box for XML. Use the search facility here to look for examples ...

Read about me at ~ Raspberry Alpha Omega ~ Frank's Punchbarrel Blog
Joe Paolangeli
Ranch Hand

Joined: Apr 05, 2000
Posts: 73
I have done this with .csv and .xml files.
Try adding:
+ someFileNameVar);
Steve Campbell
Ranch Hand

Joined: Dec 18, 2000
Posts: 41
Hi Joe,
I had already done a search and tried your suggestion. When I add your suggestion into my JSP program, it doesn't seem to understand the file that I asked to download. I tried giving both an absolute and relative path to the download file. It seems that both times it tries to download whatever is in the address line of the web server. This is usually the servlet which initiated the JSP page.
Do you think I should be using some kind of "FTP" command?
Rayudu addagarla
Ranch Hand

Joined: Aug 17, 2000
Posts: 39
hi guys,
any browser which has a plugin for that file extension ,it will open right in the browser. i guess there is no way to control other than asking the user to "rightclick and save target as..".
all latest versions of browsers have .xml plugin
hope this helps..

Good luck for Certfication.
Joe Paolangeli
Ranch Hand

Joined: Apr 05, 2000
Posts: 73
Sorry, I actually have a file download process in a servlet, not a JSP. Let me know if you are interested in this solution. I am not sure about the plug-in stuff but I think the setHeader method will fake out the browser.
I agree. Here's the link:
subject: Download file to a users PC
It's not a secret anymore!