<meta http-equiv='Content-Type' content='text/html; charset=GBK'>
知识库总目录: No.0 WebApplication开发知识库
目前已总结Html、Jsp、Rtf的中文乱码解决方法,继续搜集中......(本文涉及字符集的均以GBK为例)
1.html中文乱码?
在head区加入此句即可
2.JSP中文乱码?
行首可有此行?
<%@ page contentType="text/html; charset=GBK"%>
3.使了上面一招JSP还显示乱码?
OK,该经典的SetCharacterEncodingFilter上场了:)
package amosryan.web.control;
import java.io.IOException;
import javax.servlet.*;
public class SetCharacterEncodingFilter implements Filter {
protected String encoding;
protected FilterConfig filterConfig;
protected boolean ignore;
public SetCharacterEncodingFilter() {
encoding = null;
filterConfig = null;
ignore = true;
}
public void destroy() {
encoding = null;
filterConfig = null;
}
public void doFilter(ServletRequest request, ServletResponse response,
FilterChain chain) throws IOException, ServletException {
if (ignore || request.getCharacterEncoding() == null) {
String encode = selectEncoding(request);
if (encode != null)
request.setCharacterEncoding(encode);
}
chain.doFilter(request, response);
}
public void init(FilterConfig filterConfig2) throws ServletException {
filterConfig = filterConfig2;
encoding = filterConfig2.getInitParameter("encoding");
String value = filterConfig2.getInitParameter("ignore");
if (value == null)
ignore = true;
else if (value.equalsIgnoreCase("true"))
ignore = true;
else if (value.equalsIgnoreCase("yes"))
ignore = true;
else
ignore = false;
}
protected String selectEncoding(ServletRequest request) {
return encoding;
}
}
web.xml中的配置
<filter>
<filter-name>Set Character Encoding</filter-name>
<filter-class>
amosryan.web.control.SetCharacterEncodingFilter</filter-class>
<init-param>
<param-name>ignore</param-name>
<param-value>true</param-value>
</init-param>
<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>
4.写RTF中文乱码?
用这个方法过滤下
private static final String LINE_BREAK = "{\\rtlch\\fcs1 \\af0 \\ltrch\\fcs0 \\loch\\af0\\hich\\af0\\dbch\\af31505\\insrsid15663546 \\par }";
private static final String CN_HEAD = "\\loch\\af0\\hich\\af0\\dbch\\f31505";
private static char[] digital = "0123456789ABCDEF".toCharArray();
/**
* 字符串转换为rtf编码
*
* @param content
* @return
*/
public static String strToRtf(String content) throws Exception {
if (content == null) {
return "";
}
boolean includeChinese = false;
StringBuilder sb = new StringBuilder(1024);
for (char c : content.toCharArray()) {
if (!includeChinese) {
int intC = (int) c;
if (intC > 256) {
includeChinese = true;
}
}
sb.append(convert(c));
}
if (includeChinese) {
sb.insert(0, CN_HEAD);
}
String rtfStr = sb.toString();
rtfStr = rtfStr.replace("\\'0D\\'0A", LINE_BREAK);
return rtfStr;
}
private static String convert(char c) throws Exception {
byte[] bs = String.valueOf(c).getBytes("GBK");
StringBuilder sb = new StringBuilder(256);
for (int i = 0; i < bs.length; i++) {
int bit = (bs[i] & 0x0f0) >> 4;
sb.append("\\'");
sb.append(digital[bit]);
bit = bs[i] & 0x0f;
sb.append(digital[bit]);
}
return sb.toString();
}