四种常见编码
ISO-8859-1 单字节编码,兼容ASCII码,相当于ASCII码的扩展,无法表示中文字符,系统默认编码 。
GBK 双字节编码,国标码,可以表示繁体字和简体字,而GB2312编码只能表示简体字,GBK兼容GB2312编码 。
UNICODE 定长双字节编码,可以表示所有语言的字符,不兼容任何编码,很多软件内部是使用unicode编码来处理的 。
UTF 不定长编码,可以表示所有语言的字符,常用UTF-8,兼容iso8859-1编码,UTF编码会比UNICODE编码节省。
1.解决post中文乱码问题
在web.xml配置
<!--post乱码过滤器-->
<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>
2.解决get中文乱码问题
(1)第一种方法:修改tomcat配置文件conf/server.xml文件, 添加URIEncoding=”UTF-8”,使编码与工程编码一致,如下:
<Connector port="8080" protocol="HTTP/1.1"
connectionTimeout="20000" port="8080" protocol="HTTP/1.1"
redirectPort="8443" URIEncoding="UTF-8" />
(2)第二种方法:对参数进行重新编码:
String str =new String(request.getParamter("str").getBytes("ISO8859-1"),"utf-8")
ISO8859-1是tomcat默认编码,需要将tomcat编码后的内容按utf-8编码
3.数据库中文乱码
在jdbc.properties文件内指定utf8格式
driver=com.mysql.jdbc.Driver url=jdbc:mysql://localhost:3306/ssm?useUnicode=true&characterEncoding=UTF-8
4.html乱码和浏览器支持
<!-- 页面meta -->
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
常用例子:
<meta http-equiv="X-UA-Compatible" content="IE=7">
// 以上代码告诉IE浏览器,无论是否用DTD声明文档标准,IE8/9都会以IE7引擎来渲染页面。
<meta http-equiv="X-UA-Compatible" content="IE=8">
// 以上代码告诉IE浏览器,IE8/9都会以IE8引擎来渲染页面。
<meta http-equiv="X-UA-Compatible" content="IE=edge">
// 以上代码告诉IE浏览器,IE8/9及以后的版本都会以最高版本IE来渲染页面。
<meta http-equiv="X-UA-Compatible" content="IE=7,IE=9">
<meta http-equiv="X-UA-Compatible" content="IE=7,9">
<meta http-equiv="X-UA-Compatible" content="IE=Edge,chrome=1">
// 以上代码IE=edge告诉IE使用最新的引擎渲染网页,chrome=1则可以激活Chrome Frame.
注意:
(1)据官网定义X-UA-compatible标头不区分大小写,但是,它必须写在网页中除了title元素和其他meta以外的所有其他元素之前,如果不是,这句话将不起作用
(2)content 里的内容应该书写为IE=8。而不是直接书写IE8
5.jsp中的中文乱码问题
<%@ page contentType="text/html;charset=utf-8" language="java"%>
6.解决@responsebody返回乱码问题
在springmvc配置文件中添加
<mvc:annotation-driven>
<!-- 消息转换器,解决responseBody返回中文乱码问题 -->
<mvc:message-converters register-defaults="true">
<bean class="org.springframework.http.converter.StringHttpMessageConverter">
<property name="supportedMediaTypes" value="text/plain;charset=UTF-8" />
</bean>
</mvc:message-converters>
</mvc:annotation-driven>
7.Response或者Servlet乱码问题
response乱码指的是:
服务器向浏览器发送的数据包含中文字符,浏览器中显示的是乱码。
PrintWriter out = new PrintWriter(new OutputStreamWriter(new FileOutputStream(), "UTF-8"));
8.request中文乱码问题
request乱码指的是:
浏览器向服务器发送的请求参数中包含中文字符,服务器获取到的请求参数的值是乱码。
方法一:请求加上方法
@RequestMapping(value="/getphone",produces = "text/plain;charset=utf-8")
方法二:springMVC.xml配置文件
<!-- 处理请求返回json字符串的中文乱码问题 -->
<mvc:annotation-driven>
<mvc:message-converters>
<bean class="org.springframework.http.converter.StringHttpMessageConverter">
<property name="supportedMediaTypes">
<list>
<value>application/json;charset=UTF-8</value>
</list>
</property>
</bean>
</mvc:message-converters>
</mvc:annotation-driven>