Home > Java > JavaBase > Solution to Chinese garbled java file name

Solution to Chinese garbled java file name

Release: 2019-12-11 17:25:08
Original
4938 people have browsed it

Solution to Chinese garbled java file name

There are two situations when the JAVA file is garbled when downloading: (Recommended: java video tutorial)

1. The Chinese file name is garbled when downloading

2, when downloading, because the path contains garbled Chinese file names, it prompts that the file cannot be found

For the solution, see the following part of the 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);
            }
Copy after login

(1) If the first garbled code Type, the download page encounters the following Chinese garbled problem

Solution to Chinese garbled java file name

Use the following code to solve the problem

Solution to Chinese garbled java file name

(2) If downloading Encountered the second kind of garbled code problem, as shown in the figure:

Solution to Chinese garbled java file nameUse the following code to solve it: first ensure that tomcat, eclipse, etc. are utf-8 encoded

Solution to Chinese garbled java file name

Then in JAVA, this is separated from the first encoding of file names, so that they are encoded separately and do not affect each other.

Solution to Chinese garbled java file name

For more java knowledge, please pay attention to the java basic tutorial column.

The above is the detailed content of Solution to Chinese garbled java file name. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template