jmesa 导出excel文件名中文乱码的解决
程序员文章站
2022-04-01 08:54:37
...
private String codedFileName(String userAgent, String filename, String encoding) throws UnsupportedEncodingException { String new_filename = URLEncoder.encode(filename, encoding); // 如果没有UA,则默认使用IE的方式进行编码,因为毕竟IE还是占多数的 String rtn = "=\"" + new_filename + "\""; if (userAgent != null) { userAgent = userAgent.toLowerCase(); // IE浏览器,只能采用URLEncoder编码 if (userAgent.indexOf("msie") != -1) { rtn = "=\"" + new_filename + "\""; } // Opera浏览器只能采用filename* else if (userAgent.indexOf("opera") != -1) { rtn = "*=UTF-8''" + new_filename; } // Safari浏览器,只能采用ISO编码的中文输出 else if (userAgent.indexOf("safari") != -1) { rtn = "=\"" + new String(filename.getBytes(encoding), "ISO8859-1") + "\""; } // Chrome浏览器,只能采用MimeUtility编码或ISO编码的中文输出 else if (userAgent.indexOf("applewebkit") != -1) { new_filename = MimeUtility.encodeText(filename, "UTF8", "B"); rtn = "=\"" + new_filename + "\""; } // FireFox浏览器,可以使用MimeUtility或filename*或ISO编码的中文输出 else if (userAgent.indexOf("mozilla") != -1) { rtn = "*=UTF-8''" + new_filename; } } return rtn; }