Struts1.2 FormFile 下载中文名文件问题

来源:百度知道 编辑:UC知道 时间:2024/09/21 22:21:22
代码:
String fileName=FormFile.getFileName()
response.setHeader("Content-disposition", "attachment;filename="+fileName)

下载英文名文件正常,如果是中文名文件就报错(中文名乱码找不到文件),我自己写了个Filter之后,可以下载中文名文件,但是下载下来的文件名,不是我上传的中文文件名,他会自动生成一个文件名:CA3THJME..xls

谁有解决方案 ,越详细越好,转码的方式我都试过了(new String(fileName.getBytes(),"UTF-8"))都不行,谢谢 100分

/**
* 将字符串由中文(GBK)转换成ISO-8859-1。
*
* @param string
* 原字符串
* @return 返回一个String
*/
public static String transForGBKToISO(String string) throws Exception {
String newFileName = "";
try {
newFileName = new String(string.getBytes("GBK"), "ISO-8859-1");

} catch (UnsupportedEncodingException e) {
throw new Exception(e.getMessage(), e);
}
return newFileName;
}

tomcat配置文件
<Connector port="8080" protocol="HTTP/1.1"
connectionTimeout="20000"
redirectPort="8443"
URIEncoding="UTF-8"
/>
加上URIEncoding="UTF-8"

用URLDecoder这个类可以解码..
public static String decode(String s, String enc)
使用指定的编码机制对 application/x-www-form-urlencoded 字符串解码。
s - 要解码的 String
enc