中文乱码分为GET乱码和POST乱码
GET乱码在Tomcat中配置编码
<Connector port="8080" protocol="HTTP/1.1" connectionTimeout="20000" edirectPort="8443" URIEncoding="UTF-8" />
或者
String str=new String("str".getBytes("UTF-8"),"ISO-8859-1");
POST乱码在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>
</filter>
<filter-mapping>
<filter-name>CharacterEncodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
数据库连接配置
jdbc:mysql://localhost:3306/db?useUnicode=true&characterEncoding=UTF-8
时间: 2024-11-06 13:26:57