代码大致是这样写的
[code="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") + "\"");[/code]
1.
使用windows测试,罗列一个目录下的文件, fileName 取的是 file.getName() 均可正常下载。
2.
使用Linux服务器,从mysql(utf-8)中获取的fileName,
使用 IE6、Firefox、chrom、迅雷 均能正常下载,但IE7下载时是乱码,某些文件下载时弹出下载出错的框,进度条0%,这部分文件将文件名改掉后,重新上传,又可以正常下载,确定为文件名乱码所导致的错误。
初步怀疑是 new String(fileName.getBytes(), "ISO-8859-1") 的问题
[b]问题补充:[/b]
代码大致是这样写的
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测试,正常,IE7也可以正常下载
2.
使用Linux服务器,
使用 IE6、Firefox、chrom、迅雷 均能正常下载,但IE7下载时是乱码,某些文件下载时弹出下载出错的框,进度条0%,这部分文件将文件名改掉后,重新上传,又可以正常下载,确定为文件名乱码所导致的错误。
初步怀疑是 new String(fileName.getBytes(), "ISO-8859-1") 的问题
[b]问题补充:[/b]
问题已解决:
[code="java"]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")));[/code]
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")