天天看点

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如何能正确的还原出来我们原来的中文文件名的呢 

实验案例 

乱码现象如下 

<a href="http://my.oschina.net/pingpangkuangmo/blog/376332#">?</a>

1

2

3

4

5

6

7

8

<code>@requestmapping</code><code>(value=</code><code>"/test/httpentity1"</code><code>,method=requestmethod.get)</code>

<code>    </code><code>public</code> <code>httpentity&lt;string&gt; testhttpentity1()</code><code>throws</code> <code>unsupportedencodingexception{</code>

<code>        </code><code>string body=</code><code>"abc"</code><code>;</code>

<code>        </code><code>httpheaders headers=</code><code>new</code> <code>httpheaders();</code>

<code>        </code><code>headers.add(</code><code>"content-disposition"</code><code>,</code><code>"attachment;filename=中国.txt"</code><code>);</code>

<code>        </code><code>httpentity&lt;string&gt; ret=</code><code>new</code> <code>httpentity&lt;string&gt;(body,headers);</code>

<code>        </code><code>return</code> <code>ret;</code>

<code>    </code><code>}</code>

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

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

文件名后缀也完全变了。 

原因就是header只支持ascii所以我们要把"中国"转换成ascii。 

解决方案一将中文文件名用iso-8859-1进行重新编码 

<code>@requestmapping</code><code>(value=</code><code>"/test/httpentity"</code><code>,method=requestmethod.get)</code>

<code>    </code><code>public</code> <code>httpentity&lt;string&gt; testhttpentity()</code><code>throws</code> <code>unsupportedencodingexception{</code>

<code>        </code><code>headers.add(</code><code>"content-disposition"</code><code>,</code><code>"attachment;filename="</code><code>+</code><code>new</code> <code>string(</code><code>"中国"</code><code>.getbytes(</code><code>"utf-8"</code><code>),</code><code>"iso-8859-1"</code><code>)+</code><code>".txt"</code><code>);</code>

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 

<code>        </code><code>headers.add(</code><code>"content-disposition"</code><code>,</code><code>"attachment;filename="</code><code>+</code><code>new</code> <code>string(</code><code>"中国"</code><code>.getbytes(</code><code>"gbk"</code><code>),</code><code>"iso-8859-1"</code><code>)+</code><code>".txt"</code><code>);</code>

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

ie11和chrmoe都能正确显示面对content-disposition:attachment;filename=Öйú.txt浏览器也必须进行如下类似的操作才能正确还原出"中国"new string("Öйú".getbytes("iso-8859-1"),"gbk")。 

这里就可以提出我们的疑问了浏览器面对中å½、Öйú都能正确还原出"中国"选择utf-8还是gbk它到底是怎么做到的呢依据又是什么呢难道它是要计算出概率这便是我的疑问还请大家一起探讨和研究。 

接下来说说其他的解决方案 

解决方案二可以对中文文件名使用url编码 

<code>        </code><code>headers.add(</code><code>"content-disposition"</code><code>,</code><code>"attachment;filename="</code><code>+urlencoder.encode(</code><code>"中国"</code><code>,</code><code>"utf-8"</code><code>)+</code><code>".txt"</code><code>);</code>

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

也能正常显示出"中国.txt"。 

然而将该方案的utf-8换成gbk浏览器却不支持了。 

如下 

<code>        </code><code>headers.add(</code><code>"content-disposition"</code><code>,</code><code>"attachment;filename="</code><code>+urlencoder.encode(</code><code>"中国"</code><code>,</code><code>"gbk"</code><code>)+</code><code>".txt"</code><code>);</code>

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

文件名也全变了。 

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

这里就是说对于url编码支持utf-8其他的好像还不支持。 

解决方案三 

使用最新的解决方案即filename*=charset'lang'value。charset则是给浏览器指明以什么编码方式来还原中文文件名。 

如filename*=utf-8''value其中value为原始数据的utf-8形式的url编码。 

<code>@requestmapping</code><code>(value=</code><code>"/httpentity"</code><code>,method=requestmethod.get)         </code>

<code>        </code><code>httpheaders headers=</code><code>new</code> <code>httpheaders();</code><code>//filename="+urlencoder.encode("中国","utf-8")+";</code>

<code>        </code><code>headers.add(</code><code>"content-disposition"</code><code>,</code><code>"attachment;filename*=utf-8''"</code><code>+urlencoder.encode(</code><code>"中国"</code><code>,</code><code>"utf-8"</code><code>)+</code><code>".txt"</code><code>);</code>

<code>        </code><code>httpentity&lt;string&gt; ret=</code><code>new</code> <code>httpentity&lt;string&gt;(body, headers);</code>

HTTP协议header中Content-Disposition中文文件名乱码
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") 

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

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

继续阅读