中文乱码问题解决方案

时间:2023-02-04 23:00:43

UTF-8编码与GBK,GB2312编码区别

UTF-8:Unicode TransformationFormat-8bit,允许含BOM,但通常不含BOM。是用以解决国际上字符的一种多字节编码,它对英文使用8位(即一个字节),中文使用24为(三个字节)来编码。UTF-8包含全世界所有国家需要用到的字符,是国际编码,通用性强。UTF-8编码的文字可以在各国支持UTF8字符集的浏览器上显示。如,如果是UTF8编码,则在外国人的英文IE上也能显示中文,他们无需下载IE的中文语言支持包。


GBK是国家标准GB2312基础上扩容后兼容GB2312的标准。GBK的文字编码是用双字节来表示的,即不论中、英文字符均使用双字节来表示,为了区分中文,将其最高位都设定成1。GBK包含全部中文字符,是国家编码,通用性比UTF8差,不过UTF8占用的数据库比GBD大。


GBK、GB2312等与UTF8之间都必须通过Unicode编码才能相互转换:

GBK、GB2312--Unicode--UTF8

UTF8--Unicode--GBK、GB2312

 

简单的说:

UTF8是国际编码,它的通用性比较好,外国人也可以浏览论坛.

GBK是国家编码,通用性比UTF8差,不过UTF8占用的数据库比GBK大.

提示:如果您的网站客户群体主要是面向国内用户的,建议使用GBK版本,因为它可以节省空间,及相对utf-8版本来讲稳定一些。


中文乱码解决办法

1. 以post方式提交的表单数据中有中文字符

在获取请求参数值之前

request.setCharacterEncoding(“GBK”);

指定输出内容的编码格式

<%@page contentType=”text/html;charset=GBK” %>

2. 以get方式提交的表单中含有中文字符

String name=request.getParameter(“name”);

name = new String(name.getBytes(“ISO-8859-1”,”GBK”));

3. 在数据库中存储和读取中文数据

设置数据库的默认编码方式为GBK,GB2312

4.tomcat支持中文

    <Connector port="8080" protocol="HTTP/1.1" 
               maxThreads="150" connectionTimeout="20000"
               redirectPort="8443" URIEncoding="UTF-8"/>

5.过滤器

package com.wzs.action;

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;

public class SetCharacterEncodingFilter implements Filter {
protected String encoding = null;
protected FilterConfig filterConfig = null;
protected boolean ignore = true;

/**
* 获取过滤器初始化
*/
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;
}

public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException,
ServletException {
if (ignore || (request.getCharacterEncoding() == null)) {
String encoding = selectEncoding(request);
if (encoding != null)
request.setCharacterEncoding(encoding);
}
response.setCharacterEncoding(encoding);// 设置相应正文的编码方式
chain.doFilter(request, response);// 调用下一个过滤器
}

protected String selectEncoding(ServletRequest request) {
return (this.encoding);
}

public void destroy() {
this.encoding = null;
this.filterConfig = null;
}
}

<filter>
<filter-name>SetCharacterEncodingFilter</filter-name>
<filter-class>
org.sunxin.ch17.filter.SetCharacterEncodingFilter
</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>GBK</param-value>
</init-param>
<init-param>
<param-name>ignore</param-name>
<param-value>true</param-value>
</init-param>
</filter>