我用的平臺是Tomcat 5.5.12+MySQL 4.1.14 操作系統(tǒng)為WinXP sp2 和 Redhat 7.2
一、Tomcat配置的地方,主要針對5.5.X版本,其他低版本可能無效
Tomcat安裝到系統(tǒng)時,如果系統(tǒng)的內(nèi)碼為GBK,則能少很多問題....
經(jīng)反復(fù)查找后,發(fā)現(xiàn)影響的東西,在Web.XML中 和Server.XML配置一下,就可以消除相關(guān)的影響....
其中conf\Server.XML中,在Connector節(jié)中加入設(shè)置,如:
其中conf\Web.XML中,加入
二、加入過濾器能夠解決的問題,針對post提交有效 ***************SetCharacterEncodingFilter.java package com.cecp.filter; import javax.servlet.*; public class SetCharacterEncodingFilter implements Filter { protected String encoding = null; protected FilterConfig filterConfig = null; protected boolean ignore = true; public void destroy() { this.encoding = null; } public void doFilter(ServletRequest request, ServletResponse response, if (ignore || (request.getCharacterEncoding() == null)) { chain.doFilter(request, response); } public void init(FilterConfig filterConfig) throws ServletException { this.filterConfig = filterConfig; } protected String selectEncoding(ServletRequest request) { return (this.encoding); } } 在Web.XML中,加入 二、MySQL配置的地方 大部分針對4.1.12 4.1本身支持多語言 4.1中,如果設(shè)置庫為Itainl,可以保持和4.0的兼容,并在其他工具,如Delphi,PB中,可以直接使用中文,可以在過渡舊數(shù)據(jù)時使用... 4.0默認(rèn)編碼為 Itainl,但可以設(shè)置服務(wù)器ini [mysqld] default-character-set=gbk,其效果,相當(dāng)于表內(nèi)容為GBK,但庫為itain1的方式 四、JDBC驅(qū)動串中能夠解決的問題 在Jsp或者Bean中直接輸入jdbc串時,如下 在XML配置文件中輸入jdbc串時,如下 五、WebWork托管修正的方法 WebWork.property文件中,可以配置一下編碼和語言 使用以下函數(shù)手工轉(zhuǎn)碼.. toCN(vValue); public static String toCN(String vValue) { 七、中文文件上載的問題 中文文件上載后,應(yīng)該更名保存比較好 因?yàn)殒溄硬荒苤苯釉L問中文文件名,所以,要么采用更名下載的方式,要么就讀到內(nèi)存中,輸出下載 現(xiàn)在,我的處理方式,是上載時,保存文件為另一個名字,如Guid,再保存GUID和原有的中文文件名,下載時,通過數(shù)據(jù)庫查詢到原有的中文文件名,讀取GUID文件后,賦中文名字到客戶端,這樣能比較方便的處理 當(dāng)然也可以保持文件原名保存,但容易出現(xiàn)重名,而且,同樣不能直接根據(jù)文件名下載,只能使用getFile.jsp?FileName=文件名.doc的方式,下載真正的文件 另外,有幾點(diǎn)心得,用GBK實(shí)在是好麻煩...以后還是考慮用UTF-8,像DW8等工具,能很好的處理UTF-8中文,Java本身也是支持UTF-8的,這種方式實(shí)在是很不理想,且麻煩。
import java.io.IOException;
this.filterConfig = null;
FilterChain chain)
throws IOException, ServletException {
String encoding = selectEncoding(request);
if (encoding != null)
request.setCharacterEncoding(encoding);
}
this.encoding = filterConfig.getInitParameter("encoding");
String value = filterConfig.getInitParameter("ignore");
if (value == null)
this.ignore = true;
else if (value.equalsIgnoreCase("true"))
this.ignore = true;
else if (value.equalsIgnoreCase("yes"))
this.ignore = true;
else
this.ignore = false;
4.1中 庫和表編碼設(shè)置為GBK ,那么在PhpAdmin和Java中可以處理中文,在其他工具中,需要打開連接后加入"Set Names GBK"后,才能使用中文的查詢和更新
jdbc:mysql://127.0.0.1/cecpdmV90_test?useUnicode=true&characterEncoding=GBK
jdbc:mysql://localhost/pdmtest?useUnicode=true&characterEncoding=GBK
webwork.locale=zh_CN
webwork.i18n.encoding=GBK
六、代碼中修正的方法
try {
return new String(vValue.getBytes("ISO-8859-1"), "GBK");
} catch (Exception e) {
return vValue;
}
}