Mac safari下载文件乱码

2019-07-11  本文已影响0人  w_wx_x

导语:今天测试提了一个Bug,系统中上传附件后,在safari浏览器中下载乱码,其它浏览器正常

如图所示.png

在网上查了一下,发现有很多人提出这个问题,主要是由于编码问题
如何在开发中避免这样的问题出现,这个问题要交给后端(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;
  }
}

参考地址:http://ju.outofmemory.cn/entry/133400

上一篇 下一篇

猜你喜欢

热点阅读