HTTP协议header中Content-Disposition中文文件名乱码

在做文件下载时当文件名为中文时经常会出现乱码现象。 
参考文章 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如何能正确的还原出来我们原来的中文文件名的呢 

实验案例 
乱码现象如下
 
1
2
3
4
5
6
7
8
@RequestMapping(value="/test/httpEntity1",method=RequestMethod.GET)
    public HttpEntity<String> testHttpEntity1() throws UnsupportedEncodingException{
        String body="abc";
        HttpHeaders headers=new HttpHeaders();
        headers.add("Content-disposition","attachment;filename=中国.txt");
        HttpEntity<String> ret=new HttpEntity<String>(body,headers);
        return ret;
    }

这里的filename直接使用中文文件然后就造成了下面的乱码现象 

HTTP协议header中Content-Disposition中文文件名乱码 
文件名后缀也完全变了。 

原因就是header只支持ASCII所以我们要把"中国"转换成ASCII。 
解决方案一将中文文件名用ISO-8859-1进行重新编码
 
1
2
3
4
5
6
7
8
@RequestMapping(value="/test/httpEntity",method=RequestMethod.GET)
    public HttpEntity<String> 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<String> ret=new HttpEntity<String>(body,headers);
        return ret;
    }

chrome为 
HTTP协议header中Content-Disposition中文文件名乱码 
IE11为 

HTTP协议header中Content-Disposition中文文件名乱码 

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
 
1
2
3
4
5
6
7
8
@RequestMapping(value="/test/httpEntity",method=RequestMethod.GET)
    public HttpEntity<String> 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<String> ret=new HttpEntity<String>(body,headers);
        return ret;
    }

chrome为 
HTTP协议header中Content-Disposition中文文件名乱码 
IE11为 
HTTP协议header中Content-Disposition中文文件名乱码 
IE11和chrmoe都能正确显示面对Content-disposition:attachment;filename=Öйú.txt浏览器也必须进行如下类似的操作才能正确还原出"中国"new String("Öйú".getBytes("ISO-8859-1"),"GBK")。 
这里就可以提出我们的疑问了浏览器面对中å½、Öйú都能正确还原出"中国"选择UTF-8还是GBK它到底是怎么做到的呢依据又是什么呢难道它是要计算出概率这便是我的疑问还请大家一起探讨和研究。 

接下来说说其他的解决方案 
解决方案二可以对中文文件名使用url编码
 
1
2
3
4
5
6
7
8
@RequestMapping(value="/test/httpEntity",method=RequestMethod.GET)
    public HttpEntity<String> testHttpEntity() throws UnsupportedEncodingException{
        String body="abc";
        HttpHeaders headers=new HttpHeaders();
        headers.add("Content-disposition","attachment;filename="+URLEncoder.encode("中国","UTF-8")+".txt");
        HttpEntity<String> ret=new HttpEntity<String>(body,headers);
        return ret;
    }

chrome为 
HTTP协议header中Content-Disposition中文文件名乱码 
IE11为 

HTTP协议header中Content-Disposition中文文件名乱码 

也能正常显示出"中国.txt"。 
然而将该方案的UTF-8换成GBK浏览器却不支持了。 
如下
 
1
2
3
4
5
6
7
8
@RequestMapping(value="/test/httpEntity",method=RequestMethod.GET)
    public HttpEntity<String> testHttpEntity() throws UnsupportedEncodingException{
        String body="abc";
        HttpHeaders headers=new HttpHeaders();
        headers.add("Content-disposition","attachment;filename="+URLEncoder.encode("中国","GBK")+".txt");
        HttpEntity<String> ret=new HttpEntity<String>(body,headers);
        return ret;
    }

chrome为 
HTTP协议header中Content-Disposition中文文件名乱码 
文件名也全变了。 
IE11为 

HTTP协议header中Content-Disposition中文文件名乱码 
这里就是说对于URL编码支持UTF-8其他的好像还不支持。 

解决方案三 
使用最新的解决方案即filename*=charset'lang'value。charset则是给浏览器指明以什么编码方式来还原中文文件名。 
如filename*=UTF-8''value其中value为原始数据的UTF-8形式的URL编码。 
如下
 
1
2
3
4
5
6
7
8
@RequestMapping(value="/HttpEntity",method=RequestMethod.GET)         
    public HttpEntity<String> 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<String> ret=new HttpEntity<String>(body, headers);
        return ret;
    }

chrome为 

HTTP协议header中Content-Disposition中文文件名乱码 
IE11为 

HTTP协议header中Content-Disposition中文文件名乱码 
都能够正确显示。 
若使用headers.add("Content-Disposition","attachment;filename*=GBK''"+URLEncoder.encode("中国","UTF-8")+".txt"),对此chrome则是按照GBK方式来还原中文文件名的所以就会变成 

HTTP协议header中Content-Disposition中文文件名乱码 
造成乱码。而IE11则直接是 

HTTP协议header中Content-Disposition中文文件名乱码 

若使用headers.add("Content-disposition","attachment;filename*=GBK''"+URLEncoder.encode("中国","GBK")+".txt") 
chrome为 

HTTP协议header中Content-Disposition中文文件名乱码 
IE11为 

HTTP协议header中Content-Disposition中文文件名乱码 
即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*可以组合使用来解决跨浏览器的问题。 

对于上面提出的疑问还请网友们解答。

上一篇:高级且优雅地远程操作服务器:认识Python模块Paramiko


下一篇:【转】Android布局优化之ViewStub