解决不同浏览器文件下载文件名乱码问题(Java)

 

public static void browserFilenameEncode(HttpServletResponse response, HttpServletRequest request,
  String downFileName) throws UnsupportedEncodingException {
    String userAgent = request.getHeader("USER-AGENT");
    if (StringUtils.contains(userAgent, "MSIE") || StringUtils.contains(userAgent, "Edge")) { // IE浏览器
      downFileName = URLEncoder.encode(downFileName, "UTF-8");
    } else if (StringUtils.contains(userAgent, "Firefox")
      || StringUtils.contains(userAgent, "Chrome")
      || StringUtils.contains(userAgent, "Safari")) { // google,火狐浏览器
      downFileName = new String(downFileName.getBytes(), "ISO-8859-1");
    } else {
      downFileName = URLEncoder.encode(downFileName, "UTF-8"); // 其他浏览器
    }
    log.info("downFileName=" + downFileName);
    response.setHeader("Content-Disposition", "attachment; filename=" + downFileName);
}

 

posted @ 2022-01-18 10:44  喜葵  阅读(378)  评论(0编辑  收藏  举报