中文编码的常见问题
来源:互联网 发布:周子瑜日本人气 知乎 编辑:程序博客网 时间:2024/05/28 04:54
基于web的应用开发都会涉及到编码问题,特别是中文编码,一直是开发人员常见问题之一,也最为初学者所困扰。我们知道计算机最初是按英语单字节字符设计的,现在很多软件及系统仍然默认使用ISO8859-1编码来表示。因此,有的时候处理中文字符就会出现乱码的现象。出现乱码不只是简单的由某个软件所造成的,很可能与系统或相关软件有关联影响。
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
例如在java的web应用中采用过滤器的方式进行编码处理,将所有来自浏览器的请求(request)转换为UTF-8,因为浏览器发过来的请求包根据浏览器所在的操作系统编码,可能是各种形式编码。网上这方面的源代码很多,下面就是编码过滤器的源代码。需要配置web.xml 激活该Filter。
SetCharacterEncodingFilter源文件
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
...{
/** *//**
* The default character encoding to set for requests that pass through
* this filter.
*/
protected String encoding = null;
/** *//**
* The filter configuration object we are associated with. If this value
* is null, this filter instance is not currently configured.
*/
protected FilterConfig filterConfig = null;
/** *//**
* Should a character encoding specified by the client be ignored?
*/
protected boolean ignore = true;
/** *//**
* Take this filter out of service.
*/
public void destroy()
...{
this.encoding = null;
this.filterConfig = null;
}
/** *//**
* Select and set (if specified) the character encoding to be used to
* interpret request parameters for this request.
*
* @param request The servlet request we are processing
* @param result The servlet response we are creating
* @param chain The filter chain we are processing
*
* @exception IOException if an input/output error occurs
* @exception ServletException if a servlet error occurs
*/
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);
}
/** *//**
* Place this filter into service.
*
* @param filterConfig The filter configuration object
*
*encoding
* UTF-8
*
*/
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;
}
/** *//**
* Select an appropriate character encoding to be used, based on the
* characteristics of the current request and/or filter initialization
* parameters. If no character encoding should be set, return
* null.
*
* The default implementation unconditionally returns the value configured
* by the encoding initialization parameter for this
* filter.
*
* @param request The servlet request we are processing
*/
protected String selectEncoding(ServletRequest request)
...{
return (this.encoding);
}
}
* The default implementation unconditionally returns the value configured
* by the encoding initialization parameter for this
* filter.
*
* @param request The servlet request we are processing
*/
protected String selectEncoding(ServletRequest request)
...{
return (this.encoding);
}
}
default-character-set=utf-8
通过java程序操作数据库的时候,连接数据库的URL可以写成:
jdbc:mysql://localhost/test?useUnicode=true;characterEncoding=utf-8
Trackback: http://tb.blog.csdn.net/TrackBack.aspx?PostId=1382775
public class Main {
/** Creates a new instance of Main */
public Main() {
}
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
try {
// 測試中文轉碼
String x="中文訊息";
//System.out.printf("%s:/t%s/n", "String", x);
System.out.println("%s:/t%s/n" + x);
System.out.println("BIG5:/t");
byte y[]=x.getBytes("big5");
System.out.println("start y");
for (int i=0; i<y.length; i++) {
//System.out.printf("%x ", y[i]);
System.out.println("%x "+ y[i]);
}
System.out.println("end y");
System.out.println(y);
System.out.println("UTF-8:/t");
byte z[]=x.getBytes("utf-8");
System.out.println("start z");
for (int i=0; i<z.length; i++) {
//System.out.println("%x ", z[i]);
System.out.println("%x "+ z[i]);
}
System.out.println("end z");
System.out.println(z);
String v=new String(y);
System.out.println("BIG5:/t".concat(v));
String w=new String(z);
System.out.println("UTF-8:/t".concat(w));
} catch (java.io.UnsupportedEncodingException e) {
System.out.println("Error: UnsupportedEncodingException - ".concat(e.getMessage()));
System.exit(1);
}
}
}
- 中文编码的常见问题
- 【LaTeX 中文乱码,不能正常显示的解决方案,编码的常见问题和解决方案】
- 解决中文乱码的常见问题
- 中文的编码范围
- 中文编码的转换
- Ajax的中文编码
- Qt的中文编码
- Qt的中文编码
- Python的中文编码
- JAVA的中文编码
- Latin1编码的中文
- python的编码处理和中文编码
- js的中文编码和java的中文编码
- Freemarker的中文编码问题
- JAVA的中文编码问题
- 中文字符集编码的基础知识
- Struts的中文编码问题
- Flash的中文编码问题
- 求助!关于DES的
- linux 飞鸽
- 信道容量的迭代算法实现
- Linux下firebird无法识别 localhost 的解决
- log4j常用配置
- 中文编码的常见问题
- webwork学习笔记二
- 大教堂与市集(中文版)
- javascript小技巧
- 基于web开发的java常用工具
- 安装VMWARE TOOLS的步骤
- 也谈做人的原则
- OpenDBX for firebird
- 抽屉原理