CharacterEncodingFilter编码过滤器

CharacterEncodingFilter编码过滤器楔子Spring编码过滤器对post提交没乱码,但是GET提交存在乱码,可以修改避免乱码问题packageorg.springframework.web.filter;importjava.io.IOException;importjava.nio.charset.StandardCharsets;importjava.util.Map;import…

楔子

Spring 编码过滤器 对post 提交没乱码,但是GET提交存在乱码,可以修改避免乱码问题。
在WEB.XML还是按照之前的配置方式配置。下面代码单独提取出来,写在org.springframework.web.filter.CharacterEncodingFilter 中,放在源码目录下

package org.springframework.web.filter;

import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.Map;

import javax.servlet.FilterChain;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletRequestWrapper;
import javax.servlet.http.HttpServletResponse;

import org.springframework.util.Assert;

import cn.zhuzi.sys.SysConstant;

public class CharacterEncodingFilter extends OncePerRequestFilter { 
   

    private String encoding;

    private boolean forceRequestEncoding = false;

    private boolean forceResponseEncoding = false;

    public CharacterEncodingFilter() {
    }

    public CharacterEncodingFilter(String encoding) {
        this(encoding, false);
    }

    public CharacterEncodingFilter(String encoding, boolean forceEncoding) {
        this(encoding, forceEncoding, forceEncoding);
    }

    public CharacterEncodingFilter(String encoding, boolean forceRequestEncoding, boolean forceResponseEncoding) {
        Assert.hasLength(encoding, "Encoding must not be empty");
        this.encoding = encoding;
        this.forceRequestEncoding = forceRequestEncoding;
        this.forceResponseEncoding = forceResponseEncoding;
    }

    public void setEncoding(String encoding) {
        this.encoding = encoding;
    }

    public String getEncoding() {
        return this.encoding;
    }

    public void setForceEncoding(boolean forceEncoding) {
        this.forceRequestEncoding = forceEncoding;
        this.forceResponseEncoding = forceEncoding;
    }

    public void setForceRequestEncoding(boolean forceRequestEncoding) {
        this.forceRequestEncoding = forceRequestEncoding;
    }

    public boolean isForceRequestEncoding() {
        return this.forceRequestEncoding;
    }

    public void setForceResponseEncoding(boolean forceResponseEncoding) {
        this.forceResponseEncoding = forceResponseEncoding;
    }

    public boolean isForceResponseEncoding() {
        return this.forceResponseEncoding;
    }

    @Override
    protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain) throws ServletException, IOException {

        // --响应乱码解决
        response.setCharacterEncoding(encoding);
        response.setContentType("text/html;charset=" + encoding);
        // --利用装饰设计模式改变request对象和获取请求参数相关的方法,从而解决请求参数乱码问题
        filterChain.doFilter(new MyHttpServletRequest((HttpServletRequest) request), response);
    }

    private class MyHttpServletRequest extends HttpServletRequestWrapper { 
   
        private HttpServletRequest request = null;
        private boolean isNotEncode = true;

        public MyHttpServletRequest(HttpServletRequest request) {
            super(request);
            this.request = request;
        }

        @Override
        public Map<String, String[]> getParameterMap() {
            try {
                //POST提交
                if (request.getMethod().equalsIgnoreCase(SysConstant.HTTP_METHOD_POST)) {
                    request.setCharacterEncoding(encoding);
                    return request.getParameterMap();
                } else if (request.getMethod().equalsIgnoreCase(SysConstant.HTTP_METHOD_GET)) {
                    Map<String, String[]> map = request.getParameterMap();
                    if (isNotEncode) {
                        for (Map.Entry<String, String[]> entry : map.entrySet()) {
                            String[] vs = entry.getValue();
                            for (int i = 0; i < vs.length; i++) {
                                vs[i] = new String(vs[i].getBytes(StandardCharsets.ISO_8859_1), encoding);
                            }
                        }
                        // 这个判读很重要
                        isNotEncode = false;
                    }
                    return map;
                } else {
                    return request.getParameterMap();
                }
            } catch (Exception e) {
                e.printStackTrace();
                throw new RuntimeException(e);
            }
        }

        @Override
        public String[] getParameterValues(String name) {
            return getParameterMap().get(name);
        }

        @Override
        public String getParameter(String name) {
            return getParameterValues(name) == null ? null : getParameterValues(name)[0];
        }

    }
}

Tomcat

tomcat的server.xml中的UrIEncoding编码设置
是针对get编码的,需要配置2个位置

<Connector port="8080" protocol="HTTP/1.1" connectionTimeout="20000" redirectPort="8443" URIEncoding="UTF-8" />

<Connector port="8009" protocol="AJP/1.3" redirectPort="8443" URIEncoding="UTF-8" />

针对post可以使用spring自带的编码过滤器。

参考

https://www.cnblogs.com/seabird1979/p/4837237.html

https://blog.csdn.net/abc19900828/article/details/39478391

今天的文章CharacterEncodingFilter编码过滤器分享到此就结束了,感谢您的阅读。

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。
如需转载请保留出处:http://bianchenghao.cn/24064.html

(0)
编程小号编程小号

相关推荐

发表回复

您的电子邮箱地址不会被公开。 必填项已用*标注