aspose file tools *
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
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
Rancher

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

response.setHeader("Content-Disposition", "attachment; filename=\"file.xml\"");
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
Rancher

Joined: Mar 22, 2005
Posts: 42952
    
  73
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
Rancher

Joined: Mar 22, 2005
Posts: 42952
    
  73
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
Rancher

Joined: Mar 22, 2005
Posts: 42952
    
  73
Yes.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Using Servlet to send down a file