您的位置:首页 > 其它

文件下载中文件名乱码的解决方法

2011-06-27 15:23 369 查看
在处理文件下载时候,根据浏览器不同,对文件名的处理稍有差别。

比如日语文件名的时候的处理函数。

public static String toAttachmentFileString(HttpServletRequest request, String fileName) throws Exception {

String userAgent = request.getHeader("User-Agent");

String retValue = null;

// IE

if (userAgent != null && userAgent.indexOf("MSIE") != -1) {

retValue = new String(fileName.getBytes("Windows-31J"), "iso-8859-1");

} else {

//Firefox,Chrome

retValue = MimeUtility.encodeWord(fileName, "Windows-31J", "B");

}

return retValue;

}

需要注意的是chrome的时候,fileName必须是完整的文件名
。而不是文件名的中需要处理的那一部分。

比如文件名为download_下载_201106.csv,如果只处理的 下载 这两个字,然后做成完整文件名的话,下载的时候还是有问题的。

再有一个地方,tomcat5.5以后,GET的时候日文,中文的参数会是乱码。需在server.xml中设置一下参数。

<Connector connectionTimeout="20000" port="8080" protocol="HTTP/1.1" useBodyEncodingForURI="true"

redirectPort="8443"/>

参考官方网站:

HTTP:http://tomcat.apache.org/tomcat-6.0-doc/config/http.html

AJP:http://tomcat.apache.org/tomcat-6.0-doc/config/ajp.html
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: