我有一个处理文件下载的 JSP 页面。
我这样设置响应头:
response.setContentType("application/octet-stream");
response.addHeader("Content-Disposition","attachment; filename="+fileName);
当文件名包含空格时(即“Business Report.doc”),浏览器的对话窗口会将文件另存为“Business”。
我尝试使用 URLEncoder.encode(fileName, "Unicode"); (也试过UTF-8)
但结果是“Business+Report.doc”。
我希望最终结果是“Business Report.doc”
有什么想法吗?
谢谢。
最佳答案
你需要引用它。
response.addHeader("Content-Disposition","attachment; filename=\"" + fileName + "\"");
请注意,JSP 本质上不是处理文件下载的错误位置。您将面临二进制文件被模板文本损坏的风险。最好为此使用 Servlet。这是一个 basic example .
关于java - JSP 下载文件名包含空格的文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3640320/