Home > Java > JavaBase > body text

Solution to garbled file name of java download file

Release: 2019-12-16 16:11:35
Original
2226 people have browsed it

Solution to garbled file name of java download file

在进行文件下载的时候出现中文名称乱码,显示不出来:

response.setHeader(“Content-Disposition”, “filename=自定义的名称.jpg” )

Solution to garbled file name of java download file解决办法

对中文名称进行编码处理:

String fileName="自定义名称.jpg";
   //获得浏览器信息并转换为大写
    String agent = request.getHeader("User-Agent").toUpperCase(); 
    if(agent.indexOf("MSIE") > 0 || (agent.indexOf("GECKO")>0 && agent.indexOf("RV:11")>0)){
    //微软的浏览器(IE和Edge浏览器)
        fileName = URLEncoder.encode(fileName, "UTF-8");
     }else {
         fileName = new String(fileName.getBytes("UTF-8"), "iso-8859-1");
      }
     response.setHeader("Content-Disposition", "filename=" + fileName);
Copy after login

使用上面的代码进行编码后,经测试在Chrome, Firefox ,IE ,360浏览器都可以正常显示中文名称。

Solution to garbled file name of java download file更多java知识请关注java基础教程栏目。

The above is the detailed content of Solution to garbled file name of java download file. 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