如果下载的文件名称包括空格、(、)、;、@、#、&、,逗号
String newName = URLEncoder.encode(name, "UTF-8").replaceAll("\\+", "%20").replaceAll("%28", "\\(").replaceAll("%29", "\\)").replaceAll("%3B", ";").replaceAll("%40", "@").replaceAll("%23", "\\#").replaceAll("%26", "\\&").replaceAll("%2C, "\\,");
如果下载的文件名称包括%20 chrome 浏览器回解析成空格
// response.setContentType("." + URLEncoder.encode((newName), "UTF-8") + "; charset=UTF-8");
response.setContentType(".dump9; charset=UTF-8");
if (browserType != null && browserType.equals("GOOGLE_CHROME")) {
response.setHeader("Content-disposition", "attachment; filename*=UTF-8''" + newName); //处理文件名包含%20 不转换成空格
} else {
response.setHeader("Content-disposition", "attachment; filename=" + newName);
}
//response.setContentType("application/x-msdownload;");
response.setContentType("application/dmp;");
response.setHeader("Cache-Control", "no-cache");
response.setHeader("Content-Length", String.valueOf(size));
这篇博客介绍了在下载文件时遇到的文件名包含空格、括号等特殊字符导致的乱码问题,提供了解决方案。通过使用URLEncoder.encode方法和正则表达式进行替换,确保文件名正确显示。对于Chrome浏览器,使用特定的Content-disposition头来避免%20被转换为空格。
1万+

被折叠的 条评论
为什么被折叠?



