读书人

struts2 get 请求 上载 中文处理

发布时间: 2012-11-17 11:14:15 作者: rapoo

struts2 get 请求 下载 中文处理
本文转载于:http://www.blogjava.net/xcp/archive/2009/10/29/download2.html

最近因项目需要做一个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" /> 

读书人网 >软件架构设计

热点推荐