各浏览器下载文件名不乱码的解决办法

在项目中发现,使用Safari下载中文名文件的时候,下载的文件名是乱码的问题。但是这个问题在IE,Firefox,Chrome中是没有的。原先以为是Safari的bug,但是细细研究之下你会发现这个简单的文件下载问题在HTTP协议里经历了多少波折。

这里直接贴出java代码

/**
* <pre>
* 浏览器下载文件时需要在服务端给出下载的文件名,当文件名是ASCII字符时没有问题
* 当文件名有非ASCII字符时就有可能出现乱码
*
* 这里的实现方式参考这篇文章
* http://blog.robotshell.org/2012/deal-with-http-header-encoding-for-file-download/
*
* 最终设置的response header是这样:
*
* Content-Disposition: attachment;
*                      filename="encoded_text";
*                      filename*=utf-8''encoded_text
*
* 其中encoded_text是经过RFC 3986的“百分号URL编码”规则处理过的文件名
* </pre>
* @param response
* @param filename
* @return
*/
public static void setFileDownloadHeader(HttpServletResponse response, String filename) {
  String headerValue = "attachment;";
  headerValue += " filename="" + encodeURIComponent(filename) +"";";
  headerValue += " filename*=utf-8''" + encodeURIComponent(filename);
  response.setHeader("Content-Disposition", headerValue);
}

/**
* <pre>
* 符合 RFC 3986 标准的“百分号URL编码”
* 在这个方法里,空格会被编码成%20,而不是+
* 和浏览器的encodeURIComponent行为一致
* </pre>
* @param value
* @return
*/
public static String encodeURIComponent(String value) {
  try {
    return URLEncoder.encode(value, "UTF-8").replaceAll("\+", "%20");
  } catch (UnsupportedEncodingException e) {
    e.printStackTrace();
    return null;
  }
}

转载自:https://segmentfault.com/a/1190000005994758

感谢大神解决长久以来的问题。

原文地址:https://www.cnblogs.com/ShawnYang/p/8668504.html