Java 过滤器UTF-8_Spring 配置请求过滤器,编码格式设为UTF-8,避免中文乱码

本文介绍了如何使用Spring的CharacterEncodingFilter来配置请求过滤器,确保编码格式为UTF-8,避免中文乱码问题。通过设置filter的`encoding`属性为'UTF-8'和`forceEncoding`属性为'true',可以在请求和响应中强制应用UTF-8编码,简化编码管理。
摘要由CSDN通过智能技术生成

1

2

3 springUtf8Encoding

4 org.springframework.web.filter.CharacterEncodingFilter

5

6 encoding

7 UTF-8

8

9

10 forceEncoding

11 true

12

13

14

15 springUtf8Encoding

16 /*

17

注解配置:

@Bean

CharacterEncodingFilter characterEncodingFilter(){

CharacterEncodingFilter filter = new CharacterEncodingFilter();

filter.setEncoding("UTF-8");

filter.setForceEncoding(true);

return filter;

}

spring源码

1 public classCharacterEncodingFilterextends OncePerRequestFilter {2

3 privateString encoding;4

5 private boolean forceEncoding = false;6

7

8 /**

9 * Set the encoding to usefor requests. This encoding will be passed into a10 * {@linkjavax.servlet.http.HttpServletRequest#setCharacterEncoding} call.11 *

Whether this encoding will overrideexisting request encodings12 * (and whether it will beapplied as default response encoding as well)13 * depends on the {@link#setForceEncoding "forceEncoding"} flag.14 */

15 public voidsetEncoding(String encoding) {16 this.encoding =encoding;17 }18

19 /**

20 * Set whether theconfigured {@link#setEncoding encoding} of this filter21 * is supposed to overrideexisting request and response encodings.22 *

Default is "false", i.e. do notmodify the encoding if23 * {@linkjavax.servlet.http.HttpServletRequest#getCharacterEncoding()}24 * returns a non-null value.Switch this to "true" to enforce the specified25 * encoding in any case,applying it as default response encoding as well.26 *

Note that the response encoding will onlybe set on Servlet 2.4+27 * containers, sinceServlet 2.3 did not provide a facility for setting28 * a default responseencoding.29 */

30 public void setForceEncoding(booleanforceEncoding) {31 this.forceEncoding =forceEncoding;32 }33

34

35 @Override36 protected voiddoFilterInternal(37 HttpServletRequest request, HttpServletResponse response,FilterChain filterChain)38 throwsServletException, IOException {39

40 if (this.encoding != null && (this.forceEncoding || request.getCharacterEncoding() == null)) {41 request.setCharacterEncoding(this.encoding);42 if (this.forceEncoding) {43 response.setCharacterEncoding(this.encoding);44 }45 }46 filterChain.doFilter(request, response);47 }48 }

该字符集过滤器有两个重要参数,分别是encoding和forceEncoding

setEncoding

public void setEncoding(java.lang.String encoding)

Set the encodingto use for requests. This encoding will be passed into aServletRequest.setCharacterEncoding(java.lang.String) call.

setForceEncoding

public void setForceEncoding(boolean forceEncoding)

Set whether theconfigured encoding of this filter is supposed to override existing request andresponse encodings.

通过参考文档,我们可以知道:

l.第一个方法setEncoding()相当于:ServletRequest.setCharacterEncoding(java.lang.String),即设置request编码格式。

2.第二个方法setForceEncoding()的作用是:同时设置ServletResponse和ServletRequest的编码格式。

配置相当于代码中:

resp.setCharacterEncoding("UTF-8");req.setCharacterEncoding("UTF-8");

在请求处理的过程中我们可以不用考虑编码方面的问题,上面两句代码可以省略,编码统一交给Spring过滤器去处理。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值