目前,我们的U盘和Mail中对含有中文文件名的下载处理都不是太好
?
在某些浏览器下,下载显示的文件名是乱码
?
我又顺便看了一下其他各邮箱和网盘的处理,只有QQ处理的比较好
?
现在时基本上每个浏览器都能正常显示中文的文件名
?
花了一些时间对IE,Firefox,Opera,Chrome,Safari这几个浏览器进行了测试
?
总结了一下,各浏览器能正确识别的编码格式,只要按照这样的编码来设置对应的Content-Disposition
?
那么应该就不会出现中文文件名的乱码问题了
?
?
?
首先,Content-Disposition值可以有以下几种编码格式
?
1. 直接urlencode:
?
?? ?Content-Disposition: attachment; filename="struts2.0%E4%B8%AD%E6%96%87%E6%95%99%E7%A8%8B.chm"
?
2. Base64编码:
?
?? ?Content-Disposition: attachment; filename="=?UTF8?B?c3RydXRzMi4w5Lit5paH5pWZ56iLLmNobQ==?="
?
3. RFC2231 规定的标准:
?
?? ?Content-Disposition: attachment; filename*=UTF-8''%E5%9B%9E%E6%89%A7.msg
?
4. 直接ISO编码的文件名:
?
?? ?Content-Disposition: attachment;filename="测试.txt"
?
然后,各浏览器支持的对应编码格式为:
?
1. ?IE浏览器,采用URLEncoder编码
?
?
?
?
?
?
?
?
- new_filename?=?URLEncoder.encode(filename,? "UTF8" );??
- //?如果没有UA,则默认使用IE的方式进行编码,因为毕竟IE还是占多数的 ??
- rtn?=?"filename="" ?+?new_filename?+? """ ;??
- if ?(userAgent?!=? null )??
- {??
- ????userAgent?=?userAgent.toLowerCase();??
- ????//?IE浏览器,只能采用URLEncoder编码 ??
- ????if ?(userAgent.indexOf( "msie" )?!=?- 1 )??
- ????{??
- ????????rtn?=?"filename="" ?+?new_filename?+? """ ;??
- ????}??
- ????//?Opera浏览器只能采用filename* ??
- ????else ? if ?(userAgent.indexOf( "opera" )?!=?- 1 )??
- ????{??
- ????????rtn?=?"filename*=UTF-8''" ?+?new_filename;??
- ????}??
- ????//?Safari浏览器,只能采用ISO编码的中文输出 ??
- ????else ? if ?(userAgent.indexOf( "safari" )?!=?- 1 ?)??
- ????{??
- ????????rtn?=?"filename="" ?+? new ?String(filename.getBytes( "UTF-8" ), "ISO8859-1" )?+? """ ;??
- ????}??
- ????//?Chrome浏览器,只能采用MimeUtility编码或ISO编码的中文输出 ??
- ????else ? if ?(userAgent.indexOf( "applewebkit" )?!=?- 1 ?)??
- ????{??
- ????????new_filename?=?MimeUtility.encodeText(filename,?"UTF8" ,? "B" );??
- ????????rtn?=?"filename="" ?+?new_filename?+? """ ;??
- ????}??
- ????//?FireFox浏览器,可以使用MimeUtility或filename*或ISO编码的中文输出 ??
-
????else
?
if
?(u
serAgent.indexOf( "mozilla" )?!=?- 1 )?? - ????{??
- ????????rtn?=?"filename*=UTF-8''" ?+?new_filename;??
- ????}??
- }??
?
?
?
?
?
?
?
?
?
?
?
?
?
?
?
?
?
?
?
?
?
?
?
?
?
?
?
?
?
?
?
?
?
?
?