我開發基于 eclipse3.2+j2ee5.0 +tomcat5.09+MySQL5.0
一、表單POST的數據的中文亂碼解決
這類型的數據中文可以通過filters來實時過濾。filters代碼如下:
package filters;
import java.io.IOException;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.UnavailableException;
public class SetCharacterEncodingFilter implements Filter ...{
PRotected String encoding = null;
protected FilterConfig filterConfig = null;
protected boolean ignore = true;
public void destroy() ...{
this.encoding = null;
this.filterConfig = null;
}
public void doFilter(ServletRequest request, ServletResponse response,
FilterChain chain)
throws IOException, ServletException ...{
// Conditionally select and set the character encoding to be used
if (ignore || (request.getCharacterEncoding() == null)) ...{
String encoding = selectEncoding(request);
if (encoding != null)
request.setCharacterEncoding(encoding);
}
// Pass control on to the next filter
chain.doFilter(request, response);
}
public void init(FilterConfig filterConfig) throws ServletException ...{
this.filterConfig = filterConfig;
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;
}
protected String selectEncoding(ServletRequest request) ...{
return (this.encoding);
}
}
filters配置(配置web.xml):
<filter>
?。糵ilter-name>Set Character Encoding</filter-name>
?。糵ilter-class>filters.SetCharacterEncodingFilter</filter-class>
?。糹nit-param>
<param-name>encoding</param-name>
?。紁aram-value>GBK</param-value>
</init-param>
</filter>
<filter-mapping>
?。糵ilter-name>Set Character Encoding</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
二、將中文數據存入數據庫亂碼問題
以mysql為例,改寫連接字符串即可:
jdbc:mysql://localhost:3306/workshopdb? useUnicode=true&characterEncoding=GBK
三、 通過url傳遞參數和識別中文文件名問題
問題表現:1、通過url傳遞參數,例如:
http://localhost:81/crjy/admin/articlelist.jsp?levelId=64&levelName=學生黨建
通過request.getParameter("levleName")得到的值為亂瑪。
?。病⒆R別中文文件名,例如:
<img src="./pic/四川地圖.jpg"> 圖片不能顯示。
解決之道:
?。薄⑷绻幌虢鉀Q第一個問題那很簡單,兩句代碼即可:
String role=request.getParameter("chara");
role=new String(role.getBytes("ISO-8859-1"),"GB2312");
out.println(role);
因為tomcat服務器默認用 ISO-8859-1 字符集的。但是這樣只能解決第一個問題不能解決中文文件名問題
?。?、兩個問題一起解決,修改server.xml,找到下列語句添加URIEncoding="GB18030",這樣兩個問題就一起解決了(不需要role=new String(role.getBytes("ISO-8859-1"),"GB2312");轉化,得到的參數即為正常的中文)
<Connector acceptCount="100" connectionTimeout="20000" debug="0" disableUploadTimeout="true" enableLookups="false" maxSpareThreads="75" maxThreads="150" minSpareThreads="25" port="81" redirectPort="8443"/>
另外許多文章介紹可以添URIEncoding="UTF-8",這樣是可以解決中文文件名問題,但是通過String role=request.getParameter("chara");得到url傳遞的參數時,得到的是UTF-8編碼的,需要轉為GB2312比較麻煩。
以上是本人在使用中的總結,希望大家提供寶貴意見。
http://blog.csdn.net/lijiuu/archive/2007/02/25/1514354.aspx
新聞熱點
疑難解答