一、在web.xml中的配置
<!-- characterEncodingFilter字符编码过滤器 --> <filter> <filter-name>characterEncodingFilter</filter-name> <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class> <init-param> <!--要使用的字符集,一般我们使用UTF-8(保险起见UTF-8最好)--> <param-name>encoding</param-name> <param-value>UTF-8</param-value> </init-param> <init-param> <!--是否强制设置request的编码为encoding,默认false,不建议更改--> <param-name>forceRequestEncoding</param-name> <param-value>false</param-value> </init-param> <init-param> <!--是否强制设置response的编码为encoding,建议设置为true,下面有关于这个参数的解释--> <param-name>forceResponseEncoding</param-name> <param-value>true</param-value> </init-param> </filter> <filter-mapping> <filter-name>characterEncodingFilter</filter-name> <!--这里不能留空或者直接写 ' / ' ,否者不起作用--> <url-pattern>/*</url-pattern> </filter-mapping>
二、CharacterEncodingFilter过滤器类浅析
打开该类源码,可以看到该类有三个类属性
private String encoding; //要使用的字符集,一般我们使用UTF-8(保险起见UTF-8最好) private boolean forceRequestEncoding = false; //是否强制设置request的编码为encoding private boolean forceResponseEncoding = false; //是否强制设置response的编码为encoding
主要方法只有一个,也就是下面这个,代码逻辑很简单,入注释所解释
@Override protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain) throws ServletException, IOException { String encoding = getEncoding(); if (encoding != null) { //如果设置了encoding的值,则根据情况设置request和response的编码 //若设置request强制编码或request本身就没有设置编码 //则设置编码为encoding表示的值 if (isForceRequestEncoding() || request.getCharacterEncoding() == null) { request.setCharacterEncoding(encoding); } //若设置response强制编码,则设置编码为encoding表示的值 if (isForceResponseEncoding()) { //请注意这行代码,下面有额外提醒 response.setCharacterEncoding(encoding); } } filterChain.doFilter(request, response); }
# 额外提醒
if (isForceResponseEncoding()) {
response.setCharacterEncoding(encoding);
}
是在
filterChain.doFilter(request, response);
之前执行的,这也就是说这段代码的作用是设置response的默认编码方式,在之后的代码里是可以根据需求设置为其他编码的,即这里设置的编码可能不是最终的编码
对于get请求中文参数出现乱码解决方法有两个:
修改tomcat配置文件添加编码与工程编码一致,如下:
<Connector URIEncoding="utf-8" connectionTimeout="20000" port="8080" protocol="HTTP/1.1" redirectPort="8443"/>
另外一种方法对参数进行重新编码:
String userName = new String(request.getParamter("userName").getBytes("ISO8859-1"),"utf-8")
ISO8859-1是tomcat默认编码,需要将tomcat编码后的内容按utf-8编码
三、可以自定义拦截器(在web.xml中配置)
<filter> <filter-name>SetCharacterEncodingFilter</filter-name> <filter-class>com.itwang.filter.SetCharacterEncodingFilter</filter-class> <init-param> <param-name>encoding</param-name> <param-value>UTF-8</param-value> </init-param> </filter> <filter-mapping> <filter-name>SetCharacterEncodingFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping>
public class SetCharacterEncodingFilter implements Filter { private FilterConfig filterConfig; public void init(FilterConfig filterConfig) throws ServletException { this.filterConfig = filterConfig; } public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain) throws IOException, ServletException { HttpServletRequest request; HttpServletResponse response; try { request = (HttpServletRequest) req; response = (HttpServletResponse) res; } catch (ClassCastException e) { throw new ServletException("non-HTTP request or response"); } String encoding = filterConfig.getInitParameter("encoding"); if(encoding==null){ encoding = "UTF-8"; } //POST: request.setCharacterEncoding(encoding); response.setCharacterEncoding(encoding); response.setContentType("text/html;charset="+encoding); chain.doFilter(request, response); } public void destroy() { } }
参考:https://blog.csdn.net/lianjunzongsiling/article/details/77926370