您的位置:首页 > 编程语言 > Java开发

struts2 下载文件,下载弹出框,文件名中文乱码

2012-05-25 19:31 615 查看

Struts2 实现文件下载时,下载对话框出现中文乱码情况

解决方案:

public void setFileName(String fileName) throws UnsupportedEncodingException {
this.fileName = new String(fileName.getBytes("utf-8"), "ISO8859-1");
}
需要注意的是,文件名的编码必须与浏览器的一致。


后续:在IE环境下,下载对话框仍会出现中文乱码情况。解决方案:

public String getFileName()throws
UnsupportedEncodingException {
return new String(fileName.getBytes("utf-8"), "ISO8859-1");
}
public void setFileName(String fileName) throws UnsupportedEncodingException {
this.fileName=URLEncoder.encode(fileName, "UTF-8");
if (fileName.length() > 150) {
String guessCharset ="utf-8";  /*根据request的locale 得出可能的编码,中文操作系统通常是gb2312*/
this.fileName = new String(fileName.getBytes(guessCharset), "IsSO8859-1");
}
//this.fileName = fileName;
}

但是firefox环境下,又不行 。。 怎样兼顾ie、firefox呢? 继续跟进
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: