The file name downloaded by java is garbled
There are two situations when the JAVA file is downloaded with garbled characters:
1. When downloading, the Chinese file name is garbled
2. When downloading, because the path contains garbled Chinese file name, it prompts that the file cannot be found
For the solution, see the following part of the code( Recommended tutorial:java tutorial)
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 is encountered, the download page will encounter To the following Chinese garbled problem
Use the following code to solve the problem
(2) If the download encounters the second garbled code problem, as shown in the figure:
Use the following code to solve the problem: first ensure tomcat, eclipse Wait for utf-8 encoding
Then in JAVA, this is separated from the first encoding of the file name, so that they are encoded separately and do not affect each other.
The above is the detailed content of The file name downloaded by java is garbled. For more information, please follow other related articles on the PHP Chinese website!