Posted on 2009-10-29 21:47
xcp 閱讀(2568)
評論(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: 聯系我