IE7下载时文件名乱码
悬赏:20 发布时间:2008-11-19 提问人:jasongreen (高级程序员)
猎头职位:
代码大致是这样写的
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") 的问题
问题补充:
问题已解决:
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")
- 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") + "\"");
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") 的问题
问题补充:
问题已解决:
- String s = new String("中文"); sb.append("\nGetBytes():"+byteToString(s.getBytes()));
- sb.append("\nGetBytes(GBK):"+byteToString(s.getBytes("GBK")));
- sb.append("\nGetBytes(UTF-8):"+byteToString(s.getBytes("UTF-8")));
- sb.append("\nGetBytes(ISO):"+byteToString(s.getBytes("ISO-8859-1")));
- String s2 = new String(s.getBytes(),"ISO-8859-1");
- 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")
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
我觉得还是用filter实现编码转换比较好。
http://onlylove.iteye.com/blog/156508
water84222 (初级程序员) 2008-11-19
引用
初步怀疑是 new String(fileName.getBytes(), "ISO-8859-1") 的问题
应该是
- new String(fileName.getBytes("ISO-8859-1"), "GBK")