package com.iailab.module.data.framework.filter; import org.apache.commons.lang3.StringUtils; import org.springframework.core.annotation.Order; import org.springframework.stereotype.Component; import javax.servlet.*; import javax.servlet.annotation.WebFilter; import javax.servlet.http.HttpServletRequest; import java.io.IOException; import java.util.Objects; import java.util.Optional; @Component @WebFilter(filterName = "RewriteRequestFilter", urlPatterns = "/*") @Order(1) public class RewriteRequestFilter implements Filter { @Override public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException { //文件上传类型 不需要处理,否则会报java.nio.charset.MalformedInputException: Input length = 1异常 if (Objects.isNull(request) || Optional.ofNullable(request.getContentType()).orElse(StringUtils.EMPTY).startsWith("multipart/")) { chain.doFilter(request, response); return; } //自定义wrapper 处理流,必须在过滤器中处理,然后通过FilterChain传下去, 否则重写后的getInputStream()方法不会被调用 MyHttpServletRequestWrapper requestWrapper = new MyHttpServletRequestWrapper((HttpServletRequest)request); chain.doFilter(requestWrapper,response); } }