`

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

实验案例:
乱码现象如下:

	@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直接使用中文文件,然后就造成了下面的乱码现象:


文件名后缀也完全变了。

原因就是header只支持ASCII,所以我们要把"中国"转换成ASCII。
解决方案一:将中文文件名用ISO-8859-1进行重新编码

@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为:

IE11为:



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:

@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为:

IE11为:

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

接下来说说其他的解决方案:
解决方案二:可以对中文文件名使用url编码

@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为:

IE11为:



也能正常显示出"中国.txt"。
然而将该方案的UTF-8换成GBK,浏览器却不支持了。
如下:

@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为:

文件名也全变了。
IE11为:


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

解决方案三:
使用最新的解决方案,即filename*=charset'lang'value。charset则是给浏览器指明以什么编码方式来还原中文文件名。
如filename*=UTF-8''value,其中value为原始数据的UTF-8形式的URL编码。
如下:

@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为:


IE11为:


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


造成乱码。而IE11则直接是



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


IE11为:


即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*可以组合使用,来解决跨浏览器的问题。

对于上面提出的疑问,还请网友们解答。
  • 大小: 43.1 KB
  • 大小: 49.6 KB
  • 大小: 48.1 KB
  • 大小: 53.6 KB
  • 大小: 50.9 KB
  • 大小: 8.3 KB
  • 大小: 7.9 KB
  • 大小: 9.1 KB
  • 大小: 10.2 KB
  • 大小: 53.4 KB
  • 大小: 7.9 KB
  • 大小: 47.2 KB
  • 大小: 8.1 KB
  • 大小: 64.7 KB
  • 大小: 9.4 KB
分享到:
评论

相关推荐

    ( response.setHeader()下载中文文件名乱码

    ( response.setHeader()下载中文文件名乱码问题

    下载文件个别浏览器文件名乱码解决办法

    代码如下: if (context.Request.UserAgent.ToLower().IndexOf(“msie”, System.StringComparison.Ordinal) &gt; -1)//IE浏览器 { context.Response.AddHeader(“content-disposition”, “filename=” + ...

    解析如何在PHP下载文件名中解决乱码的问题

    那么用Content-Disposition设置下载的文件名,这个也有不少人知道吧。基本上,下载程序都是这么写的:复制代码 代码如下:$filename = “document.txt”;header(‘Content-Type: application/octet-stream’);header...

    PHP附件下载中文名称乱码的解决方法

    本文实例讲述了PHP附件下载...header ( "Content-disposition: attachment; filename=$filename.xls" ); 网上说,在RFC2231的定义里面, 多语言编码的Content-Disposition应该这么定义: 复制代码 代码如下:Content-

    php做下载文件的实现代码及文件名中乱码解决方法

    ?php header(“Content-Type: application/force-download”); header(“Content-Disposition: attachment; filename=ins.jpg”);... 如何在PHP下载文件名中解决乱码 通过把Content-Type设置为application/octet-stre

    PHP 强制下载文件代码

    复制代码 代码如下: &lt;?... header(“Content-Type: ... 您可能感兴趣的文章:php中强制下载文件的代码(解决了IE下中文文件名乱码问题)php强制下载类型的实现代码IE php关于强制下载文件的代码php 强制下载文件实现代码

    Asp.net中的页面乱码的问题

    下载文件时指定文件名,中文的文件名出现了乱码? Response.AddHeader(“Content-Disposition”, “attachment; filename=”+HttpUtility.UrlEncoding(filename.ToString ())); 3.如何识别字符串中是否包含韩文

    文件下载及web文件的contentType类型大全

    Response.AddHeader("Content-Disposition", "attachment;filename="+filename);//设置文件名 response.addHeader("Content-Length",file.length);//设置下载文件大小 response.setContentType("application/octet...

    可以显示中文名称的下载组件

    但是它本身自带的download功能并不支持中文名称的文件,在下载的时候会出现乱码,我自己编写了一个FileDownload类,放到了这个jar包中,这个类用的UTF-8编码方式,所以可以对中文文件名进行很好的支持,下面是这个类...

    ssh(structs,spring,hibernate)框架中的上传下载

     文件数据存储在Blob类型的FILE_CONTENT表字段上,在Spring中采用OracleLobHandler来处理Lob字段(包括Clob和Blob),由于在程序中不需要引用到oracle数据驱动程序的具体类且屏蔽了不同数据库处理Lob字段方法上的...

Global site tag (gtag.js) - Google Analytics