Home  >  Article  >  Java  >  Java file download Chinese file name is garbled

Java file download Chinese file name is garbled

王林
王林Original
2020-02-03 09:59:162655browse

Java file download Chinese file name is garbled

There are two situations when the Chinese file name is garbled when downloading a JAVA file:

1. The Chinese file name is garbled when downloading;

Java file download Chinese file name is garbled

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

Java file download Chinese file name is garbled

Related video tutorial sharing: java video tutorial

The solution is as follows:

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);
            }

Related article tutorial sharing :java introductory tutorial

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

Statement:
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
Previous article:What does R mean in javaNext article:What does R mean in java