在JAVA WEB APP裏下載文件,當包含中文名稱時,會在不一樣瀏覽器, 不一樣操做系統裏,產生亂碼的問題;java
試過下面的方法,沒有效果。瀏覽器
String name = fileName + ".xlsx"; if (httpServletRequest.getHeader("User-Agent").toUpperCase().indexOf("MSIE") > 0) { name = URLEncoder.encode(name, "GBK"); } else { name = new String(name.getBytes("GBK"), "ISO8859-1"); } httpServletResponse.setHeader("Content-Disposition", "attachment;filename=" + name);
最後在StackOverflow上面找到了一個有效的方法;測試過在MAC,Windows, IE, Chrome, FF裏均可以;測試
final String encodedFileName = encodeFilename(fileNameToEncode); response.setHeader("Content-Disposition", "attachment; filename*=UTF-8''" + encodedFileName); private String encodeFilename(final String filename) { try{ URI uri = new URI(null, null, filename, null); String encodedName = uri.toASCIIString(); return encodedName; } catch(URISyntaxException ex){ return filename; } }
from: http://stackoverflow.com/questions/22319277/how-to-set-chinese-filename-correctly-for-different-browsers-when-download-file操作系統