不同浏览器下载文件时中文文件名乱码问题(最新)
java代码示例
服务端使用java,浏览器下载文件
@RestController
@RequestMapping("download")
public class DownloadController {
@RequestMapping("download")
public void download(HttpServletRequest request, HttpServletResponse response) throws Exception {
File file = new File("E:\\down\\123.png");
byte[] bytes = Files.readAllBytes(file.toPath());
DownloadController.download(bytes, "图片1.png", request, response);
}
public static void download(byte[] bytes, String filename, HttpServletRequest request, HttpServletResponse response) throws Exception {
String browser = getBrowser(request);
String attachmentFileName = null;
if ("Firefox".equals(browser)) {
System.out.println("当前是火狐浏览器");
attachmentFilename = "attachment;fileName=" + new String(filename.getBytes("UTF-8"), "iso-8859-1");
}
byte[] buffer = new byte[1024];
response.setContentType("application/force-download");
response.addHeader("Content-Disposition", attachmentFileName);
BufferedInputStream bis = new BufferedInputStream(new ByteArrayInputStream(bytes));
OutputStream outputStream = response.getOutputStream();
int len;
while ((len = bis.read(buffer)) != -1) {
outputStream.write(buffer, 0, len);
}
}
private static String getBrowser(HttpServletRequest request) {
String userAgent = request.getHeader("USER-AGENT");
if (userAgent.indexOf("Firefox") != -1) {
return "Firefox";
}
return null;
}
}
文件名产生乱码的原因就在于"attachment;fileName=" + new String(filename.getBytes("UTF-8"), "iso-8859-1") 这句话,这个示例适用于火狐浏览器,其它浏览器可能需要不同的编码格式。所以需要先知道浏览器再确定编码
浏览器和浏览器的User-Agent
浏览器请求时,请求头会携带User-Agent 属性,这个属性的值会带有浏览器信息
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:91.0) Gecko/20100101 Firefox/91.0
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/89.0.4389.114 Safari/537.36 Edg/89.0.774.68
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/102.0.5005.63 Safari/537.36
User-Agent: Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/101.0.4951.67 Safari/537.36 OPR/87.0.4390.36
User-Agent: Mozilla/5.0 (Windows NT 10.0; WOW64; Trident/7.0; rv:11.0) like Gecko
浏览器 | Mozilla/5.0 | Gecko | Firefox | AppleWebKit | Chrome | Safari | Edg | OPR |
---|
火狐浏览器 | √ | √ | √ | × | × | × | × | × | EDGE | √ | like | × | √ | √ | √ | √ | × | CHROME | √ | like | × | √ | √ | √ | × | × | OPERA | √ | like | × | √ | √ | √ | × | √ | IE11 | √ | like | × | × | × | × | × | × |
收集整理
- IE浏览器,只能采用URLEncoder编码(只适用于IE11以下,因为在IE11中UserAgent不再包含MSIE字符串)
- Opera浏览器,只能采用filename*
- Safari浏览器,只能采用ISO编码的中文输出(苹果的)
- Chrome浏览器,采用MimeUtility编码或ISO编码的中文输出
- FireFox浏览器,可以使用MimeUtility或filename*或ISO编码的中文输出,IE11和FireFox会走这个,IE11中的userAgent去掉了MSIE
- 旧版本的Edge浏览器中包含Edge,而不是Edg
- 旧版本IE,
Mozilla/4.0 (Windows; MSIE 6.0; Windows NT 5.2)
不同编码格式的java代码
String attachmentFilename = "attachment;fileName=" + URLEncoder.encode(filename, "UTF-8");
String attachmentFilename = "attachment;fileName*=UTF-8''" + URLEncoder.encode(filename, "UTF-8");
String attachmentFilename = "attachment;fileName=" + new String(filename.getBytes("UTF-8"), "iso-8859-1");
String attachmentFilename = "attachment;fileName=" + MimeUtility.encodeText(filename, "UTF8", "B");
目前情况
目前情况看来,最新的浏览器(上面表格中列举的)版本中,User-Agent 中都包含Mozilla/5.0 ,都(上面表格中列举的)可以采用ISO编码格式来处理中文文件名。而且很多不同浏览器都包含有相同的标识,所以区分变得不那么容易,以后可能会趋于标准化。
|