proxo能解决opera下载乱码吗?

普遍适用的规则分享、讨论

Moderator: phoenix

Post Reply
不夜侯
Posts: 101
Joined: Jul 29 2009, 16:22

proxo能解决opera下载乱码吗?

Post by 不夜侯 » Aug 19 2009, 15:37

opera浏览器下载论坛附件,遇到有中文名称时经常乱码。
在网上搜索到一些相关的信息如下。
Response.setHeader(”Content-Disposition”, “attachment; filename=” + fileName+”.xls”);
如果file.Name为中文则乱码。解决办法是
方法1:
response.setHeader(”Content-Disposition”, “attachment; filename=” + java.net.URLEncoder.encode(fileName, “UTF-8″));
下载的程序里有了上面一句,一般在IE6的下载提示框上将正确显示文件的名字,无论是简体中文,还是日文。但是文字只要超过17个字,就不能下载了。
一. 通过原来的方式,也就是先用URLEncoder编码,当中文文字超过17个时,IE6 无法下载文件。这是IE的bug,参见微软的知识库文章 KB816868 。原因可能是IE在处理 Response Header 的时候,对header的长度限制在150字节左右。而一个汉字编码成UTF-8是9个字节,那么17个字便是153个字节,所以会报错。而且不跟后缀也 不对.
方法2:
response.setHeader( “Content-Disposition”, “attachment;filename=” + new String( fileName.getBytes(”gb2312″), “ISO8859-1″ ) );
在确保附件文件名都是简 体中文字的情况下,那么这个办法确实是最有效的,不用让客户逐个的升级IE。如果台湾同胞用,把gb2312改成big5就行。但现在的系统通常都加入了 国际化的支持,普遍使用UTF-8。如果文件名中又有简体中文字,又有繁体中文,还有日文。那么乱码便产生了。另外,在上Firefox (v1.0-en)下载也是乱码。
我模仿论坛有会员放出的规则,写了一个却是无效。

Code: Select all

[HTTP headers]
In = TRUE
Out = FALSE
Key = "Content-Disposition: opera filename (in)"
URL = "*(/attachment.php\?aid\=[0-9]+|/job.php\?action\=download\&pid\=*)"
Match = "\1filename=$AV(\3)\2"
Replace = "\1filename=\3.getBytes("gb2312"), "ISO8859-1" \2"
请问这个规则应该怎样写?谢谢!

不夜侯
Posts: 101
Joined: Jul 29 2009, 16:22

Re: proxo能解决opera下载乱码吗?

Post by 不夜侯 » Aug 19 2009, 15:40

一个附件下载名称乱码的信息如下:

Code: Select all

New Message Log Window....

+++GET 2043+++
GET /attachment.php?aid=1785642&k=30f2fd34152727ed7456111a7bd99d7c&t=1250667266 HTTP/1.0
User-Agent: Opera/9.80 (Windows NT 6.0; U; zh-cn) Presto/2.2.15 Version/10.00
Host: bbs.winzheng.com
Accept: text/html, application/xml;q=0.9, application/xhtml+xml, image/png, image/jpeg, image/gif, image/x-xbitmap, */*;q=0.1
Accept-Language: zh-CN,zh;q=0.9,en;q=0.8
Accept-Charset: iso-8859-1, utf-8, utf-16, *;q=0.1
Accept-Encoding: gzip, deflate
Referer: http://bbs.winzheng.com/viewthread.php?tid=1450285&extra=page%3D1
Cookie: cdb_cookietime=2592000; cdb_auth=1c34g5pbmG3wr%2FC0bKzyQ239kUT8Prrje3O8ZbEgBWV4bj0PhJmL1gOgr6brEaQaqcV2DfqD2hB61H16FVqwo3yj%2F4st; cdb_frameon=yes; cdb_visitedfid=99D14D2D17D3D11D10D135D32D81D73; cdb_sid=UAjzz6; cdb_oldtopics=D1450285D; cdb_smile=1D1D0D16
Cookie2: $Version=1
Connection: keep-alive

+++RESP 2043+++
HTTP/1.0 200 OK
Server: Zeus/4.3
Date: Wed, 19 Aug 2009 07:38:40 GMT
Last-Modified: Wed, 25 Jul 2007 06:32:54 GMT
Content-Encoding: none
Content-Disposition: attachment; filename="MD5值校验.rar"
Content-Type: application/octet-stream
Content-Length: 226642
X-Powered-By: PHP/5.2.10
+++CLOSE 2043+++

ddbb
Moderator
Posts: 425
Joined: Jan 07 2008, 13:30

Re: proxo能解决opera下载乱码吗?

Post by ddbb » Aug 19 2009, 16:12

这个好像是没办法的...........

User avatar
phoenix
Site Admin
Posts: 525
Joined: Dec 29 2007, 16:27

Re: proxo能解决opera下载乱码吗?

Post by phoenix » Aug 21 2009, 14:24

Code: Select all

[HTTP headers]
In = TRUE
Out = FALSE
Key = "Content-Disposition: Opera Attachment File Name Fix 09.08.20 [phoenix] (In)"
URL = "$OHDR(User-Agent:*opera*)"
Match = "attachment; filename="\1""
Replace = "attachment; filename*=gb2312''$ESC(\1)$LOG(RRESP $DTM(c) : Content-Disposition: Attachment -> Name Fix for Opera!)"
参考资料: Test Cases for HTTP Content-Disposition header and RFC 2231/2047 Encoding

不夜侯
Posts: 101
Joined: Jul 29 2009, 16:22

Re: proxo能解决opera下载乱码吗?

Post by 不夜侯 » Sep 06 2009, 16:05

谢谢phoenix。

Post Reply