GeeCON Prague 2014*
The moose likes JSP and the fly likes Change file name on generated download file Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » JSP
Bookmark "Change file name on generated download file" Watch "Change file name on generated download file" New topic
Author

Change file name on generated download file

Clem White
Greenhorn

Joined: Apr 29, 2002
Posts: 9
The scenario:
There is a list of files that logged in users are able to download. The files themselves are not kept within the web root, as we need to be able to ensure that a user has rights to download the file. To do this I have created a JSP page which (a) checks that the user is logged and has appropriate permissions, (b) sets the content type to something the browser does not understand (to ensure the file, which is basically just text is downloaded instead of just displayed in the browser) and (c) gets and includes the content of the file using java.io.File.
The problem:
... with this approach is that it uses a link which looks something like <a href=getfile.jsp?filename=thefile.snd >thefile.snd</a>. This means that the file name in the save dialogue always defaults to 'getfile.jsp', when it needs to default to 'thefile.snd'.
Any ideas? I've considered using an signed applet to do the download and rename the file but would like to avoid this if possible (although if anyone has the source for such an applet that would also be helpful).
Tim Holloway
Saloon Keeper

Joined: Jun 25, 2001
Posts: 16091
    
  21

Use the Content-Disposition HTTP header to override the name that will be associated with the downloaded file.


Customer surveys are for companies who didn't pay proper attention to begin with.
Clem White
Greenhorn

Joined: Apr 29, 2002
Posts: 9
Excellent - it works! Thanks heaps, Tim, I've been trying to work out a way to do this for ages.
 
GeeCON Prague 2014
 
subject: Change file name on generated download file