[size=medium]在做文件下载时,当文件名为中文时,经常会出现乱码现象。
参考文章: http://blog.robotshell.org/2012/deal-with-http-header-encoding-for-file-download/
本文就详细给出案例来解决这一乱码问题,以及还一直未解决的一个疑问,欢迎大家一起来探讨。
大体的原因就是header中只支持ASCII,所以我们传输的文件名必须是ASCII,当文件名为中文时,必须要将该中文转换成ASCII。
转换方式有很多:
方式一:将中文文件名用ISO-8859-1进行重新编码,如headers.add("Content-disposition","attachment;filename="+new String("中国".getBytes("UTF-8"),"ISO-8859-1")+".txt");
方式二:可以对中文文件名使用url编码,如headers.add("Content-disposition","attachment;filename="+URLEncoder.encode("中国","UTF-8")+".txt");
疑问:中文文件名转换成ASCII后传给浏览器,浏览器遇到一堆ASCII,如何能正确的还原出来我们原来的中文文件名的呢?
实验案例:
乱码现象如下:[/size]
@RequestMapping(value="/test/httpEntity1",method=RequestMethod.GET)
public HttpEntity testHttpEntity1() throws UnsupportedEncodingException{
String body="abc";
HttpHeaders headers=new HttpHeaders();
headers.add("Content-disposition","attachment;filename=中国.txt");
HttpEntity ret=new HttpEntity(body,headers);
return ret;
}
[size=medium]这里的filename直接使用中文文件,然后就造成了下面的乱码现象:
[img]http://dl2.iteye.com/upload/attachment/0100/5094/ac669428-d30c-3c71-a5cd-2dc2353372c6.png[/img]
文件名后缀也完全变了。
原因就是header只支持ASCII,所以我们要把"中国"转换成ASCII。
解决方案一:将中文文件名用ISO-8859-1进行重新编码[/size]
@RequestMapping(value="/test/httpEntity",method=RequestMethod.GET)
public HttpEntity testHttpEntity() throws UnsupportedEncodingException{
String body="abc";
HttpHeaders headers=new HttpHeaders();
headers.add("Content-disposition","attachment;filename="+new String("中国".getBytes("UTF-8"),"ISO-8859-1")+".txt");
HttpEntity ret=new HttpEntity(body,headers);
return ret;
}
[size=medium]chrome为:
[img]http://dl2.iteye.com/upload/attachment/0100/5096/ea723149-589a-3369-aef3-eea58b93808d.png[/img]
IE11为:
[img]http://dl2.iteye.com/upload/attachment/0100/5106/4f8ed89f-5e33-3d14-8724-9d94756b51f8.png[/img]
chrome解决了乱码现象,但IE没有。但是你是否想过,浏览器面对一堆Content-disposition:attachment;filename=ä¸å½.txt,它又是如何来正确显示的中文文件名"中国.txt"的呢,它肯定要对ä¸å½,重新进行UTF-8编码才能正确显示出"中国",即必须进行类似如下的操作:new String("ä¸å½".getBytes("ISO-8859-1"),"UTF-8"),才能正常显示出"中国.txt"。而IE11进行的类似操作为:new String("ä¸å½".getBytes("ISO-8859-1"),"GBK")。
同样的实验,只是把UTF-8改成GBK:[/size]
@RequestMapping(value="/test/httpEntity",method=RequestMethod.GET)
public HttpEntity testHttpEntity() throws UnsupportedEncodingException{
String body="abc";
HttpHeaders headers=new HttpHeaders();
headers.add("Content-disposition","attachment;filename="+new String("中国".getBytes("GBK"),"ISO-8859-1")+".txt");
HttpEntity ret=new HttpEntity(body,headers);
return ret;
}
[size=medium]chrome为:
[img]http://dl2.iteye.com/upload/attachment/0100/5098/ce6cc211-6689-3da8-ba11-627db1be7721.png[/img]
IE11为:
[img]http://dl2.iteye.com/upload/attachment/0100/5108/a3de1cd3-3b3c-310e-b40f-43e5348fc742.png[/img]
IE11和chrmoe都能正确显示,面对Content-disposition:attachment;filename=Öйú.txt,浏览器也必须进行如下类似的操作才能正确还原出"中国",new String("Öйú".getBytes("ISO-8859-1"),"GBK")。
这里就可以提出我们的疑问了,浏览器面对ä¸å½、Öйú都能正确还原出"中国",选择UTF-8还是GBK,它到底是怎么做到的呢?依据又是什么呢?难道它是要计算出概率?这便是我的疑问,还请大家一起探讨和研究。
接下来说说其他的解决方案:
解决方案二:可以对中文文件名使用url编码[/size]
@RequestMapping(value="/test/httpEntity",method=RequestMethod.GET)
public HttpEntity testHttpEntity() throws UnsupportedEncodingException{
String body="abc";
HttpHeaders headers=new HttpHeaders();
headers.add("Content-disposition","attachment;filename="+URLEncoder.encode("中国","UTF-8")+".txt");
HttpEntity ret=new HttpEntity(body,headers);
return ret;
}
[size=medium]chrome为:
[img]http://dl2.iteye.com/upload/attachment/0100/5100/f6b6c271-3ddc-3554-8fe2-14525381dfe6.png[/img]
IE11为:
[img]http://dl2.iteye.com/upload/attachment/0100/5110/f713cb53-eebc-35c0-9d5a-dc025e7d8939.png[/img]
也能正常显示出"中国.txt"。
然而将该方案的UTF-8换成GBK,浏览器却不支持了。
如下:[/size]
@RequestMapping(value="/test/httpEntity",method=RequestMethod.GET)
public HttpEntity testHttpEntity() throws UnsupportedEncodingException{
String body="abc";
HttpHeaders headers=new HttpHeaders();
headers.add("Content-disposition","attachment;filename="+URLEncoder.encode("中国","GBK")+".txt");
HttpEntity ret=new HttpEntity(body,headers);
return ret;
}
[size=medium]chrome为:
[img]http://dl2.iteye.com/upload/attachment/0100/5102/c623c1fb-00ba-3a03-8df8-000c11b41cb3.png[/img]
文件名也全变了。
IE11为:
[img]http://dl2.iteye.com/upload/attachment/0100/5104/14aa4c2f-f2cc-3044-9a49-664521ce2623.png[/img]
这里就是说对于URL编码,支持UTF-8,其他的好像还不支持。
解决方案三:
使用最新的解决方案,即filename*=charset'lang'value。charset则是给浏览器指明以什么编码方式来还原中文文件名。
如filename*=UTF-8''value,其中value为原始数据的UTF-8形式的URL编码。
如下:[/size]
@RequestMapping(value="/HttpEntity",method=RequestMethod.GET)
public HttpEntity testHttpEntity() throws UnsupportedEncodingException{
HttpHeaders headers=new HttpHeaders();//filename="+URLEncoder.encode("中国","UTF-8")+";
String body="abc";
headers.add("Content-Disposition","attachment;filename*=UTF-8''"+URLEncoder.encode("中国","UTF-8")+".txt");
HttpEntity ret=new HttpEntity(body, headers);
return ret;
}
[size=medium]chrome为:
[img]http://dl2.iteye.com/upload/attachment/0100/5484/acc9a515-b8ac-39b1-ba65-6520e22cbf9b.png[/img]
IE11为:
[img]http://dl2.iteye.com/upload/attachment/0100/5488/507a8750-12b3-30d2-b956-d901b3d736f8.png[/img]
都能够正确显示。
若使用headers.add("Content-Disposition","attachment;filename*=GBK''"+URLEncoder.encode("中国","UTF-8")+".txt"),对此,chrome则是按照GBK方式来还原中文文件名的,所以就会变成
[img]http://dl2.iteye.com/upload/attachment/0100/5492/ff03a0d4-fc9e-3e01-a838-046074751e8e.png[/img]
造成乱码。而IE11则直接是
[img]http://dl2.iteye.com/upload/attachment/0100/5496/f0eae8be-c588-38d3-bff4-ef52dac71143.png[/img]
若使用headers.add("Content-disposition","attachment;filename*=GBK''"+URLEncoder.encode("中国","GBK")+".txt")
chrome为:
[img]http://dl2.iteye.com/upload/attachment/0100/5502/170db5fe-1bec-3a2e-96e0-42e82c0ba2d9.png[/img]
IE11为:
[img]http://dl2.iteye.com/upload/attachment/0100/5504/aa68e61c-7766-3fef-8496-bbd97d8a742b.png[/img]
即IE11仅支持filename*=UTF-8编码形式的。
然后就是headers.add("Content-disposition","attachment;filename="+URLEncoder.encode("中国","UTF-8")+".txt;filename*=UTF-8''"+URLEncoder.encode("中国","UTF-8")+".txt");filename和filename*可以组合使用,来解决跨浏览器的问题。
对于上面提出的疑问,还请网友们解答。[/size]