Java字符编码解析 (二)

2014-11-24 11:17:39 · 作者: · 浏览: 19
K inputfile outputfile

读取XML文件
1 XML文件读写同于文件读写,但应注意确保XML头中声明如与文件编码保持一致。

2 javax.xml.SAXParser类接受InputStream作为输入参数,对于Reader,需要用org.xml.sax.InputSource包装一下,再给SAXParser。

3 对于UTF-8编码 XML,注意防止编辑器自动加上\uFFFE BOM头, xml parser会报告content is not allowed in prolog。

字节数组
1 使用 new String(byteArray,encoding) 和 String.getBytes(encoding) 在字节数组和字符串之间进行转换也可以用ByteArrayInputStream/ByteArrayOutputStream转为流后再用InputStreamReader/OutputStreamWriter转换。

错误编码的字符串(iso8859-1转码gbk)
如果我们得到的字符串是由错误的转码方式产生的,例如:对于gbk中文,由iso8859-1方式转换,此时如果用调试器看到的字符串一般是的样子,长度一般为文本的字节长度,而非汉字个数。可以采用如下方式转为正确的中文:

text = new String( text.getBytes(“iso8859-1”),”gbk”);

JDBC
转换过程由JDBC Driver执行,取决于各JDBC数据库实现。对此经验尚积累不够。

1 对于ORACLE数据库,需要数据库创建时指定编码方式为gbk,否则会出现汉字转码错误

2 对于SQLServer 2000,最好以nvarchar/nchar类型存放文本,即不存在中文/编码转换问题。

3 连接 Mysql,将 connectionString 设置成 encoding 为 gb2312:

String connectionString = "jdbc:mysql://localhost/test useUnicode=true&characterEncoding=gb2312";

WEB/Servlet/JSP
1 对于JSP,确定头部加上 <%@ contenttype="text/html;charset=gb2312" page="">这样的标签。

<%@ contenttype="text/html;charset=gb2312" page="">

2 对于Servlet,确定 设置setContentType (“text/html; charset=gb2312”),以上两条用于使得输出汉字没有问题。

3 为输出HTML head中加一个

,让 浏览器正确确定HTML编码。

4 为Web应用加一个Filter,确保每个Request明确调用setCharacterEncoding方法,让输入汉字能够正确解析。

[java]
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;
import javax.servlet.http.HttpServletRequest;

/**
* Example filter that sets the character encoding to be used in parsing the
* incoming request
*/
public class SetCharacterEncodingFilter
implements Filter {
public SetCharacterEncodingFilter()
{}
protected boolean debug = false;
protected String encoding = null;
protected FilterConfig filterConfig = null;
public void destroy() {
this.encoding = null;
this.filterConfig = null;
}

public void doFilter(ServletRequest request, ServletResponse response,
FilterChain chain) throws IOException, ServletException {
// if (request.getCharacterEncoding() == null)
// {
// String encoding = getEncoding();
// if (encoding != null)
// request.setCharacterEncoding(encoding);
//
// }
request.setCharacterEncoding(encoding);
if ( debug ){
System.out.println( ((HttpServletRequest)request).getRequestURI()+"setted to "+encoding );
}
chain.doFilter(request, response);
}

public void init(FilterConfig filterConfig) throws ServletException {
this.filterConfig = filterConfig;
this.encoding = filterConfig.getInitParameter("encoding");
this.debug = "true".equalsIgnoreCase( filterConfig.getInitParameter("debug") );
}

protected String getEncoding() {
return (this.encoding);
}
}

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;
import javax.servlet.http.HttpServletRequest;

/**
* Example filter that sets the character encoding to be u