Home> Java> javaTutorial> body text

Chinese file name of java download file is garbled

王林
Release: 2020-01-16 10:27:28
Original
3498 people have browsed it

Chinese file name of java download file is garbled

The problem arises:

Upload pictures with Chinese names, but the Chinese names cannot be displayed when downloaded.

As shown:

Chinese file name of java download file is garbled

(Related video tutorial recommendation:java video tutorial)

Solution:

1. Sping-mvc.xml part code:

      
Copy after login

2. FileController.java Chinese file name acquisition code:

Get the browser format and control the file according to different browsers Name encoding format.

String userAgent = request.getHeader("User-Agent");//获取浏览器名(IE/Chome/firefox) if(userAgent.contains("MSIE")||userAgent.contains("Trident")) {//针对IE或IE为内核的浏览器 fileName=java.net.URLEncoder.encode(fileName,"UTF-8"); }else { fileName=new String(fileName.getBytes("UTF-8"),"ISO-8859-1");//谷歌控制版本 } headers.setContentDispositionFormData("attachment", fileName);// 默认下载文件名为原始文件名
Copy after login

Other browser control character formats: (details)

String userAgent = request.getHeader("User-Agent");//获取浏览器名(IE/Chome/firefox) if (userAgent.contains("firefox")) { fileName = new String(fileName.getBytes("UTF-8"), "ISO8859-1"); // firefox浏览器 } else if (userAgent.contains("MSIE")) { fileName = URLEncoder.encode(fileName, "UTF-8");// IE浏览器 }else if (userAgent.contains("CHROME")) { fileName = new String(fileName.getBytes("UTF-8"), "ISO8859-1");// 谷歌 } headers.setContentDispositionFormData("attachment", fileName);// 默认下载文件名为原始文件名
Copy after login

Recommended related articles and tutorials:java introductory tutorial

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

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
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!