JSP Download file with filename containing spaces
I have a JSP page that handles file downloads.
I set the response header like so:
response.setContentType("application/octet-stream");
response.addHeader("Content-Disposition","attachment; filename="+fileName);
When the fileName contains spaces (i.e. "Business Report.doc"), the browser's dialog window saves the file as "Business".
I tried using URLEncoder.encode(fileName, "Unicode"); (also tried UTF-8)
but I get "Business+Report.doc" as the result.
I want the final result to be "Business Report.doc"
An开发者_高级运维y ideas?
Thanks.
You need to quote it.
response.addHeader("Content-Disposition","attachment; filename=\"" + fileName + "\"");
Note that a JSP is essentially the wrong place to handle file downloads. You will risk that the binary file get corrupted with template text. Better use a Servlet for this. Here's a basic example.
精彩评论