首页  |  知识库  |  资源下载  |  在线工具  |  A-Z  •  JAR  •  名词查         

通过spring-web包的CharacterEncodingFilter字节编码过滤器实现服务端请求字符集编码强制转换处理过滤

标签:spring-web,CharacterEncodingFilter,utf编码,字符编码转换,springframework     发布时间:2018-10-06   

一、前言

通过在项目的web.xml配置springframeworkspring-web包中的org.springframework.web.filter.CharacterEncodingFilter类,将中文请求入参强制转换为指定的字符集编码如UTF-8,参见配置详情。

二、配置说明

1. 项目web.xml配置增加如下filter、filter-mapping项

	<filter>@b@		<filter-name>Set Character Encoding</filter-name>@b@		<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>@b@		<init-param>@b@			<param-name>encoding</param-name>@b@			<param-value>UTF-8</param-value>@b@		</init-param>@b@	</filter>@b@	<filter-mapping>@b@		<filter-name>Set Character Encoding</filter-name>@b@		<url-pattern>/*</url-pattern>@b@	</filter-mapping>

2. CharacterEncodingFilter类源码

package org.springframework.web.filter;@b@@b@import java.io.IOException;@b@import javax.servlet.FilterChain;@b@import javax.servlet.ServletException;@b@import javax.servlet.http.HttpServletRequest;@b@import javax.servlet.http.HttpServletResponse;@b@import org.springframework.util.Assert;@b@@b@public class CharacterEncodingFilter extends OncePerRequestFilter@b@{@b@  private String encoding;@b@  private boolean forceRequestEncoding;@b@  private boolean forceResponseEncoding;@b@@b@  public CharacterEncodingFilter()@b@  {@b@    this.forceRequestEncoding = false;@b@@b@    this.forceResponseEncoding = false;@b@  }@b@@b@  public CharacterEncodingFilter(String encoding)@b@  {@b@    this(encoding, false);@b@  }@b@@b@  public CharacterEncodingFilter(String encoding, boolean forceEncoding)@b@  {@b@    this(encoding, forceEncoding, forceEncoding);@b@  }@b@@b@  public CharacterEncodingFilter(String encoding, boolean forceRequestEncoding, boolean forceResponseEncoding)@b@  {@b@    this.forceRequestEncoding = false;@b@@b@    this.forceResponseEncoding = false;@b@@b@    Assert.hasLength(encoding, "Encoding must not be empty");@b@    this.encoding = encoding;@b@    this.forceRequestEncoding = forceRequestEncoding;@b@    this.forceResponseEncoding = forceResponseEncoding;@b@  }@b@@b@  public void setEncoding(String encoding)@b@  {@b@    this.encoding = encoding;@b@  }@b@@b@  public String getEncoding()@b@  {@b@    return this.encoding;@b@  }@b@@b@  public void setForceEncoding(boolean forceEncoding)@b@  {@b@    this.forceRequestEncoding = forceEncoding;@b@    this.forceResponseEncoding = forceEncoding;@b@  }@b@@b@  public void setForceRequestEncoding(boolean forceRequestEncoding)@b@  {@b@    this.forceRequestEncoding = forceRequestEncoding;@b@  }@b@@b@  public boolean isForceRequestEncoding()@b@  {@b@    return this.forceRequestEncoding;@b@  }@b@@b@  public void setForceResponseEncoding(boolean forceResponseEncoding)@b@  {@b@    this.forceResponseEncoding = forceResponseEncoding;@b@  }@b@@b@  public boolean isForceResponseEncoding()@b@  {@b@    return this.forceResponseEncoding;@b@  }@b@@b@  protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain)@b@    throws ServletException, IOException@b@  {@b@    String encoding = getEncoding();@b@    if (encoding != null) {@b@      if ((isForceRequestEncoding()) || (request.getCharacterEncoding() == null))@b@        request.setCharacterEncoding(encoding);@b@@b@      if (isForceResponseEncoding())@b@        response.setCharacterEncoding(encoding);@b@    }@b@@b@    filterChain.doFilter(request, response);@b@  }@b@}