This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Servlets and the fly likes downloading a file. how can i set the file name? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "downloading a file. how can i set the file name?" Watch "downloading a file. how can i set the file name?" New topic

downloading a file. how can i set the file name?

Leonardo Penha
Ranch Hand

Joined: Feb 15, 2002
Posts: 68
I have a .zip file stored in my database in a BLOB field and I want my web site user to download this file when he/she clicks on a web page button. Here is what I do in my servlet:
byte[] b = /*get the BLOB field from my database*/
OutputStream out = response.getOutputStream();
When the user clicks on my web page button the above code is executed and my web browser prompts the user to download the file, but the dialog box displays the form action instead of the filename. The contents of the file are ok, but I want the user to be able to save the file with the same name as it is stored in the database.
Can anyone help me with this?
Leonardo Penha
P.S.: In my servlet I have the file name stored in a String variable.

chanoch wiggers
Ranch Hand

Joined: May 24, 2001
Posts: 245
you need to set a Http Header called content disposition to set the file name which you can do as follows:
response.setHeader("Content-disposition", "inline;filename=<your filename here>"

chanoch<p><a href="" target="_blank" rel="nofollow">Author of Professional Apache Tomcat</a></p>
jQuery in Action, 2nd edition
subject: downloading a file. how can i set the file name?
Similar Threads
Uploading file in a database
Setting response header through Javascript
Downloading file from server
Enable/disable buttons - File download(CSV)
file download-user authentication-window close