在项目中,涉及到文件下载时,当文件名包含中文和空格的时候,如果不对其进行特殊处理,就会出现问题。 1 包含中文 中文会乱码 2 包含空格 火狐浏览器,会截断空格。如:原文件名:新建文本文档 (1).txt,下载后文件名为:新建文本文档。空格后的丢失了。
3 解决代码
response
.setContentType(
"text/html;charset=UTF-8")
request
.setCharacterEncoding(
"UTF-8")
response
.setContentType(
"application/octet-stream; charset=utf-8")
//new String(fileName
.getBytes(
"gb2312"),
"ISO-8859-1") 中文编码,防止乱码
//filename用
""引起来,防止火狐截断
response
.setHeader(
"Content-Disposition",
"attachment; filename=\"" + new String(fileName
.getBytes(
"gb2312"),
"ISO-8859-1") +
"\"")
转载请注明原文地址: https://ju.6miu.com/read-500210.html