忘记从哪篇博文借鉴的了,博主看到的话可以联系我进行转载或者删除。然后自己修复了原有代码的BUG,已验证各个浏览器文件下载中文没有乱码
/**
* 解决各个浏览器文件下载中文乱码问题
* @param filename
* @return
*/
public static String getFileName(HttpServletRequest request, String filename) {
String finalFileName = "";
try {
final String userAgent = request.getHeader("USER-AGENT");
if (StringUtils.contains(userAgent, "Edge")) {
finalFileName = URLEncoder.encode(filename, "UTF8");
} else if (StringUtils.contains(userAgent, "MSIE") || StringUtils.contains(userAgent, "Trident")) {
// IE浏览器
finalFileName = URLEncoder.encode(filename, "UTF8");
} else if (StringUtils.contains(userAgent, "Mozilla")) {
// google,火狐浏览器
finalFileName = new String(filename.getBytes(), "ISO8859-1");
} else {
//其他浏览器
finalFileName = URLEncoder.encode(filename, "UTF8");
}
} catch (Exception e) {
e.printStackTrace();
}
return finalFileName;
}