JSP Download a file with a file name containing spaces

I have a JSP page that handles file uploads.

I set the response header like this:

response.setContentType("application/octet-stream"); response.addHeader("Content-Disposition","attachment; filename="+fileName); 

When the file_name contains spaces (ie, โ€œBusiness Report.docโ€), the browser dialog box saves the file as โ€œBusinessโ€.

I tried using URLEncoder.encode (file_name, "Unicode"); (also tried UTF-8)

but the result is "Business + Report.doc".

I want the end result to be "Business Report.doc"

Any ideas?

Thanks.

+4
source share
1 answer

You need to quote it.

 response.addHeader("Content-Disposition","attachment; filename=\"" + fileName + "\""); 

Please note that JSP is essentially the wrong place to handle file uploads. You risk that the binary will be corrupted by the template text. Better use a servlet for this. Here is a basic example .

+7
source

All Articles