我开发基于 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>
<filter-name>Set Character Encoding</filter-name>
<filter-class>filters.SetCharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>GBK</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-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")得到的值为乱玛。
2、识别中文文件名,例如:
<img src="./pic/四川地图.jpg"> 图片不能显示。
解决之道:
1、如果只想解决第一个问题那很简单,两句代码即可:
String role=request.getParameter("chara");
role=new String(role.getBytes("ISO-8859-1"),"GB2312");
out.println(role);
因为tomcat服务器默认用ISO-8859-1字符集的。但是这样只能解决第一个问题不能解决中文文件名问题
2、两个问题一起解决,修改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