This week's book giveaway is in the Java 8 forum.
We're giving away four copies of Java 8 in Action and have Raoul-Gabriel Urma, Mario Fusco, and Alan Mycroft on-line!
See this thread for details.
The moose likes Servlets and the fly likes Using Servlet to send down a file Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "Using Servlet to send down a file" Watch "Using Servlet to send down a file" New topic
Author

Using Servlet to send down a file

ying lam
Ranch Hand

Joined: May 17, 2004
Posts: 85
Hi,

I have a Servlet (called Download) which send down a xml file (based on some query string).
My question is how can I specify the name of the file on users's desktop when people hit my servlet?

Right now, it saves into a file called 'Download'.

Thank you.
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 39576
    
  27
You set a Content-Disposition header like this:

response.setHeader("Content-Disposition", "attachment; filename=\"file.xml\"");


Ping & DNS - updated with new look and Ping home screen widget
Jeroen T Wenting
Ranch Hand

Joined: Apr 21, 2006
Posts: 1847
but browsers can ignore it, and so can users when they save it.


42
Rahul Bhattacharjee
Ranch Hand

Joined: Nov 29, 2005
Posts: 2308
When you are sending a file which the browser can open , then browser wouldnot give you the option of saving the file ; rather this would try to open the file.(Like html , xml , and pdf [if acrobat reader is installed.]).
In case you want to send an xml , it would be read by the browser rather then giving the use an option to save this file.


Rahul Bhattacharjee
LinkedIn - Blog
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 39576
    
  27
then browser wouldnot give you the option of saving the file

Only if the user wants it so. The first time the browser sees an unknown file format it will ask the user what to do with it. If the user specifies that files of that type should be saved to disk, then the browser will not try to open them.
Rahul Bhattacharjee
Ranch Hand

Joined: Nov 29, 2005
Posts: 2308
When you are sending a file which the browser can open , then browser wouldnot give you the option of saving the file


When the browser can open a file (in other words the browser understands the format of the data.) then it opens the files directly ,rather giving the user the option to save it.
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 39576
    
  27
When the browser can open a file (in other words the browser understands the format of the data.) then it opens the files directly


This is incorrect. Please re-read my previous post. Files downloaded as described above will not be opened -no matter which file type they are- unless they user specifically requested that at some point. Until she does, the file save dialog will be shown.
Rahul Bhattacharjee
Ranch Hand

Joined: Nov 29, 2005
Posts: 2308
Thanks Ulf , I got it now.
But we have to set the content-disposition header for this to happen.Is that so ?
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 39576
    
  27
Yes.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Using Servlet to send down a file
 
Similar Threads
Writer already called
send data, generate a chart and download it
java.lang.IllegalStateException: getOutputStream() error
File download and request Forward
File download servlet