Download with filename having special characters
843841Nov 28 2003 — edited Dec 1 2003Hello All,
I currently have a servlet that talks to a backend component that retrieves a file from the database and sends it back to the browser for download.
Everything works fine, however, when the filename contains a special character such as &, the file name gets truncated when the file save as dialog is displayed. So for example, a document named "Notes & Ideas.doc" gets sent back to the browser, the dialog box shows the filename to save as "Notes ".
Below is the snippet of code. I have tried the sending the filename back using URLEncoder.encode(), however, this does not seem to work. The application is running on a Weblogic 7 environment.
Any ideas would be appreciated. Thanks in advance.
// Write the file to the response in order to download
// Upon download, the Content-Disposition filename= specifies the name of the downloaded file to the browser
// Using attachment; filename opens the Save As dialog box
response.addHeader("Content-Disposition", "attachment; filename=" + URLEncoder.encode(fileName));
// X-download is unrecognized by browsers, so the browser will not try to open the file
response.setContentType("application/x-download");
response.getOutputStream().write(fileContent.getBytes());
response.getOutputStream().flush();