1.Tomcat配置:
<Connector port="8080" protocol="HTTP/1.1"
connectionTimeout="20000"
redirectPort="8443" URIEncoding="UTF-8" useBodyEncodingForURI="true"/>
2.文件:必须设置统一编码
JSP:<%@ page language="java" pageEncoding="UTF-8"%>
HTML:<meta http-equiv="Content-Type" content="text/html;charset=utf-8">
ServletRequest:
request.setCharacterEncoding("UTF-8")是把提交内容的字符集设为UTF-8
ServletResponse:
response.setCharacterEncoding("UTF-8")
3. MySQL
default-character-set=utf-8
mysql里数据库和表也都设为utf8_unicode_ci
jdbc:mysql://localhost/mydb?useUnicode=true&characterEncoding=utf-8
4. PostgreSQL
postgresql.conf文件中的client_encoding改成UTF-8,
5. 注意:使用 FileInputStream 读取文件时,使用的是系统默认的字符编码。如果用在UTF-8系统中,很容易出现问题。
这时,可以使用:
FileInputStream in = new FileInputStream("g:\\yufa.txt");
InputStreamReader reader = new InputStreamReader (in,"GBK"); // 用这个方法读取,并指定编码
6. Java中的 properties 文件,如果是使用 utf-8 文件格式的,这个格式是无 BOM 格式的UTF-8的。切记
7. Spring
<filter>
<filter-name>CharacterEncodingFilter</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>utf-8</param-value>
</init-param>
<init-param>
<param-name>forceEncoding</param-name>
<param-value>true</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>CharacterEncodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>