文件下载

IE7下载时文件名乱码

悬赏:20 发布时间:2008-11-19 提问人:jasongreen (高级程序员)
代码大致是这样写的 
Java代码   收藏代码
  1. response.setContentType(contentType);  
  2. if(StringUtils.hasText(encoding))  
  3.     response.setCharacterEncoding(encoding);  
  4. else  
  5.     response.setCharacterEncoding("utf-8");  
  6. response.setHeader("Content-Disposition""filename=\"" + new String(fileName.getBytes(), "ISO-8859-1") + "\"");  


1. 
使用windows测试,罗列一个目录下的文件, fileName 取的是 file.getName() 均可正常下载。 

2. 
使用Linux服务器,从mysql(utf-8)中获取的fileName, 
使用 IE6、Firefox、chrom、迅雷 均能正常下载,但IE7下载时是乱码,某些文件下载时弹出下载出错的框,进度条0%,这部分文件将文件名改掉后,重新上传,又可以正常下载,确定为文件名乱码所导致的错误。 

初步怀疑是 new String(fileName.getBytes(), "ISO-8859-1") 的问题
问题补充:
代码大致是这样写的 
<pre name="code" class="java"> 
response.setContentType(contentType); 
if(StringUtils.hasText(encoding)) 
response.setCharacterEncoding(encoding); 
else 
response.setCharacterEncoding("utf-8"); 
response.setHeader("Content-Disposition", "filename=\"" + new String(fileName.getBytes(), "ISO-8859-1") + "\"");</pre> 

1. 
使用windows测试,正常,IE7也可以正常下载 

2. 
使用Linux服务器, 
使用 IE6、Firefox、chrom、迅雷 均能正常下载,但IE7下载时是乱码,某些文件下载时弹出下载出错的框,进度条0%,这部分文件将文件名改掉后,重新上传,又可以正常下载,确定为文件名乱码所导致的错误。 

初步怀疑是 new String(fileName.getBytes(), "ISO-8859-1") 的问题 



问题补充:
问题已解决: 

Java代码   收藏代码
  1. String s = new String("中文");            sb.append("\nGetBytes():"+byteToString(s.getBytes()));  
  2. sb.append("\nGetBytes(GBK):"+byteToString(s.getBytes("GBK")));  
  3. sb.append("\nGetBytes(UTF-8):"+byteToString(s.getBytes("UTF-8")));  
  4. sb.append("\nGetBytes(ISO):"+byteToString(s.getBytes("ISO-8859-1")));  
  5. String s2 = new String(s.getBytes(),"ISO-8859-1");  
  6. sb.append("\nout:"+byteToString(s2.getBytes("ISO-8859-1")));  


win: 
GBK 
GetBytes():d6d0cec4 
GetBytes(GBK):d6d0cec4 
GetBytes(UTF-8):e4b8ade69687 
GetBytes(ISO):3f3f 
out:d6d0cec4 

linux: 
UTF-8 
GetBytes():e4b8ade69687 
GetBytes(GBK):d6d0cec4 
GetBytes(UTF-8):e4b8ade69687 
GetBytes(ISO):3f3f 
out:e4b8ade69687 

分析: IE7 不能够识别以 UTF-8 编码的下载文件名。 
解决方法: new String(fileName.getBytes("GBK"),"ISO-8859-1")

采纳的答案

2008-11-19 agile_boy (初级程序员)
new String(fileName.getBytes("ISO-8859-1")) 
or 
new String(fileName.getBytes("ISO-8859-1"),"UTF-8")

提问者对于答案的评价:
分给你了

其他回答

引用
new String(fileName.getBytes(), "ISO-8859-1")


Debug下看是否正常的文件名; 就能确定是否是编码问题了..
bohemia (架构师) 2008-11-19
应该是文件名乱码的问题。 
我觉得还是用filter实现编码转换比较好。 
http://onlylove.iteye.com/blog/156508
water84222 (初级程序员) 2008-11-19
引用

初步怀疑是 new String(fileName.getBytes(), "ISO-8859-1") 的问题

应该是 
Java代码   收藏代码
  1. new String(fileName.getBytes("ISO-8859-1"), "GBK")  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值