首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 软件管理 > 软件架构设计 >

struts2 get 请求 上载 中文处理

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

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" /> 

热点排行