转:
java下载文件时文件名出现乱码的解决办法
java下载文件时文件名出现乱码的解决办法:
-
String userAgent = request.getHeader("User-Agent");
-
String formFileName = file.getFileName();
-
-
// 针对IE或者以IE为内核的浏览器:
-
if (userAgent.contains("MSIE") || userAgent.contains("Trident")) {
-
formFileName = java.net.URLEncoder.encode(formFileName, "UTF-8");
-
} else {
-
// 非IE浏览器的处理:
-
formFileName = new String(formFileName.getBytes("UTF-8"), "ISO-8859-1");
-
}
-
response.setHeader("Content-disposition",String.format("attachment; filename="%s"", formFileName));
-
response.setContentType("multipart/form-data");