不同浏览器下载文件时中文文件名乱码问题(最新)

女足世界杯中国18232025-07-30 11:54:33

不同浏览器下载文件时中文文件名乱码问题(最新)

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

EDGE

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

CHROME

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

OPERA

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

IE11

User-Agent: Mozilla/5.0 (Windows NT 10.0; WOW64; Trident/7.0; rv:11.0) like Gecko

浏览器Mozilla/5.0GeckoFirefoxAppleWebKitChromeSafariEdgOPR火狐浏览器√√√×××××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代码

URLEncoder编码

String attachmentFilename = "attachment;fileName=" + URLEncoder.encode(filename, "UTF-8");

filename*

String attachmentFilename = "attachment;fileName*=UTF-8''" + URLEncoder.encode(filename, "UTF-8");

ISO编码

String attachmentFilename = "attachment;fileName=" + new String(filename.getBytes("UTF-8"), "iso-8859-1");

MimeUtility

String attachmentFilename = "attachment;fileName=" + MimeUtility.encodeText(filename, "UTF8", "B");

目前情况

目前情况看来,最新的浏览器(上面表格中列举的)版本中,User-Agent中都包含Mozilla/5.0,都(上面表格中列举的)可以采用ISO编码格式来处理中文文件名。而且很多不同浏览器都包含有相同的标识,所以区分变得不那么容易,以后可能会趋于标准化。

鹪鹩赋原文、翻译及赏析、拼音版及朗读
苹果AirPods固件升级全攻略来了,六步搞定不再愁!