开发者

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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜