JAVA file download garbled There are two cases:
1, when downloading a file name garbled Chinese
2, because the download path includes Chinese file name garbled, suggesting that no documents
Solution See the following section of code
response.setContentType("multipart/form-data");
String userAgent = request.getHeader("User-Agent");
String oraFileName = meetingFile.getFileName();
String formFileName=oraFileName;
// 针对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("application/vnd.ms-excel;charset=utf-8");
response.setCharacterEncoding("UTF-8");
ServletOutputStream out;
// 通过文件路径获得File对象
File file = null;
if (meetingFile != null) {
file = new File(path + "upload/"+oraFileName);
}
(1) If the first garbled type, the download page encountered the following Chinese garbage problem
With the following code to solve
(2) If you download the second encounter garbled problem, as shown:
Solution with the following code: i.e. first make sure tomcat, eclipse utf-8 encoded as the
In such JAVA then separated with the first encoded file name, so that they encode, independently of each other.
Note: before the current tests can solve coding problems when Firefox and IE and other browsers to download, and for WIN10 browser that comes from time to time still garbled phenomenon