环境:
JDK1.5
TOMCAT5.0.30
%TOMCAT_HOME%:F:\Tomcat 5.0
统一使用utf-8
一。URL:
修改%TOMCAT_HOME%\conf\server.xml文件
找到如下内容:
<Connector
port="80" maxThreads="150" minSpareThreads="25" maxSpareThreads="75"
enableLookups="false" redirectPort="8443" acceptCount="100"
debug="0" connectionTimeout="20000"
disableUploadTimeout="true" />
添加:URIEncoding="utf-8"
修改后内容如下:
<Connector
port="80" maxThreads="150" minSpareThreads="25" maxSpareThreads="75"
enableLookups="false" redirectPort="8443" acceptCount="100"
debug="0" connectionTimeout="20000"
disableUploadTimeout="true" URIEncoding="utf-8" />
在客户段需要对URL中的中文进行编码
eg:
String t=URLEncoder.encode("help中文", "utf8");
urlString="http://61.144.47.228/sendSeveninfo.do?msisdn=8615602387092&content="+t+"&username=seven&password=seven777";
URL url = new URL(urlString);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
String coo = conn.getResponseMessage();
在服务器端用URLDecoder解码
String de=URLDecoder.decode(request.getParameter("content"), "utf8");
String content = new String(request.getParameter("content").getBytes(),"utf8");
百度URL用GBK编码, displaytag 和google用的是utf8编码.
二。处理表单乱码:
用过滤器吧。
下面是用于处理表单的一个filter
package hrl.util.filter;
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
this.ignore=false;
}
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
// TODO 自动生成方法存根
if (ignore || (request.getCharacterEncoding() == null)) {
String encoding = selectEncoding(request);
if (encoding != null){
request.setCharacterEncoding(encoding);
}else
System.out.println("encoding has not set");
}
chain.doFilter(request, response);
}
public void destroy() {
// TODO 自动生成方法存根
this.encoding = null;
this.filterConfig = null;
}
protected String selectEncoding(ServletRequest request) {
return (this.encoding);
}
}
在web.xml里配置此filter,将其影射到所有页面上
<filter>
<filter-name>encoding filter</filter-name>
<filter-class>hrl.util.filter.SetCharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>utf-8</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>encoding filter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
三。处理jsp&servlet页面显示乱码
JSP中:
最顶端加入如下代码:
<%@ page language="java" contentType="text/html;charset=utf-8"%>
关键:charset=gb2312
servlet中:
在doGet 或doPost方法中加入如下代码:
response.setCharacterEncoding("utf-8");
四。处理文件下载时提示保存文件名乱码的解决方法
在处理下载的JSP或servlet里修改响应头部如下:
response.setContentType("application/octet-stream;charset=utf8");
将文件名进行编码转换:
response.addHeader("Content-Disposition","attachment;filename="+hrl.util.text.DealString.toUtf8String(file.getName())+"\"");
DealString类的toUtf8String方法如下:
public static String toUtf8String(String src)
{
byte[] b = src.getBytes();
char[] c = new char[b.length];
for(int i=0;i<b.length;i++)
{
c[i] = (char)(b[i]&0x00FF);
}
return new String(c);
}
五。读取properties文件时出现乱码
如果properties文件里含有中文时不可以用ResourceBundle度取,需先对properties文件进行编码转换
在JDK中提供了native2ascii命令 他能够实现字符编码的转换。
假设message.properties 文件在目录WEB-INF/resource目录下
在命令行模式下进入WEB-INF/resource目录,然后执行如下命令
native2ascii –encoding utf-8 message.properties message_zh_CN.properties
参数-encoding表示要转换成的编码格式
执行以上命令以后将生成message_zh_CN.properties文件 现在就可以用ResourceBundle类进行读取了
也可以通过制定读取文件的编码格式来读取文件
isReader = new InputStreamReader(is,GlobalConst.STR_ENCODE_GBK);
bufferedReader = new BufferedReader(isReader);
while ((strLine = bufferedReader.readLine()) != null) {
..........
}
六。AJAX乱码
在向服务器传参前用函数encodeURIComponent(String str)对参数进行编码
七。数据库编码
拿MySQL举例
在建表和建库时限定编码格式:
eg: CREATE database <库> CHARACTER SET UTF8
create table <表>(
.........
)engine=InnoDB character set utf8;
文章出处:http://www.diybl.com/course/3_program/java/javaxl/200861/118552_2.html
posted on 2008-12-11 17:37
波 阅读(3111)
评论(0) 编辑 收藏