J2EE社区

苟有恒,何必三更起五更眠;
最无益,只怕一日曝十日寒.
posts - 241, comments - 318, trackbacks - 0, articles - 16

struts2 get 请求 下载 中文处理

Posted on 2009-10-29 21:47 xcp 阅读(2552) 评论(0)  编辑  收藏 所属分类: struts2
最近因项目需要做一个struts2下载功能,但是老是因为在做一些get请求的中文编码困扰,以下是解决方法
1. encodeURI将文本以utf-8的编码,具体参见随笔js对文字进行编码涉及3个函数:escape,encodeURI,encodeURIComponent的区别
例:encodeURI("download.action?filenames="+filenames+"&filepaths="+filepaths);

2.但后台与用户交互的时候,如弹出下载对话时要想正确的显示中文文件名,我们需要对字端再次编码 也就是对get方法进行编码设置,否则中文名文件将出现乱码,或无法下载的情况
例:public String getFilename() {
          try {
                return new String(filename.getBytes(), "ISO-8859-1");
         } catch (UnsupportedEncodingException e) {
               e.printStackTrace();
               return filename;
        }
    }

3.  配置tomcat/conf/server.xml
  如:  <Connector port="8080" protocol="HTTP/1.1"    connectionTimeout="20000"                redirectPort="8443" URIEncoding="UTF-8" />


名称: ♪4C.ESL | .↗Evon
口号: 遇到新问题♪先要寻找一个方案乄而不是创造一个方案こ
mail: 联系我



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


网站导航: