You smiled and talked to me of nothing and I felt that for this I had been waiting long.

The trees come up to my window like the yearning voice of the dumb earth.

BlogJava 首页 新随笔 联系 聚合 管理
  19 Posts :: 10 Stories :: 25 Comments :: 0 Trackbacks
最近在项目开发中,处理文件下载时,出现中文文件名乱码的现象。以下是解决方法的小记。
1.Action配置:
 1 <action name="downloadFile"
 2             class="com.xxx.xxx.web.action.common.LoadFileAsInputStreamAction"
 3             method="downloadFile">
 4             <result name="success" type="stream">
 5                 <param name="contentType">
 6                     application/octet-stream;charset=UTF-8
 7                 </param>
 8                 <param name="inputName">inputStream</param>
 9                 <param name="bufferSize">100000</param>
10                 <param name="contentDisposition">
11                     attachment;fileName="${fileName}"
12                 </param>
13             </result>
14         </action>
2.对fileName进行转码:
 1     private String convertUnicode(String name) {
 2         String filename = null;
 3         try { // 支持IE和FireFox
 4             filename = new String(name.getBytes("GBK"), "ISO-8859-1");
 5         } catch (UnsupportedEncodingException e) {
 6             // e.printStackTrace();
 7             LOG.error(" ### Convert FileName Error!");
 8         }
 9         return filename;
10     }
3.修改TOMCAT server.xml:
增加URIEncoding="UTF-8"参数设置.
版权声明:转载时请以超链接形式标明文章原始出处和作者信息及本声明

posted on 2008-06-11 09:37 钩子 阅读(2450) 评论(3)  编辑  收藏 所属分类: opensources工作笔记

Feedback

# re: webwork-下载文件出现中文名称乱码的解决方法 2008-06-11 17:01 隔叶黄莺
我需要一种不用通过
filename = new String(name.getBytes("GBK"), "ISO-8859-1");

解决中文乱码的办法  回复  更多评论
  

# re: webwork-下载文件出现中文名称乱码的解决方法 2008-06-11 18:40 钩子
@隔叶黄莺
嗯,我也不喜欢再做次转换,但是目前还没有找到更好的方法。空下来分析下WW的代码看看,期望能找到更好的解决方案。  回复  更多评论
  

# re: webwork-下载文件出现中文名称乱码的解决方法 2008-08-11 13:49 钩子
另,关于filename最好是用form post的形式提交,否则如果是在url后面带参数的形式的话,会出现ie6下某些字体无法正确解析的现象。
这里小注下。  回复  更多评论
  


只有注册用户登录后才能发表评论。


网站导航: