项目中自定义了一个过滤器继承 OncePerRequestFilter
public class OnceFilter extends OncePerRequestFilter {
@Override
protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain) throws ServletException, IOException {
ServletRequest requestWrapper = null;
setRequestChainInfo(request);
if(request instanceof HttpServletRequest) {
requestWrapper = new RequestWrapper( request);
}
// 让下一个filter生效
if(requestWrapper == null) {
filterChain.doFilter(request, response);
} else {
filterChain.doFilter(requestWrapper, response);
}
// 清除MDC
MDC.clear();
}
/**
* 设置整个请求链路信息
* @param req HttpServletRequest
*/
private void setRequestChainInfo(HttpServletRequest req) {
String traceId = req.getHeader(CoreCommonConstant.TRACE_ID);
if (StringUtils.isBlank(traceId)) {
traceId = CsRandomUtil.randomUUID(true,true);
}
req.setAttribute(CoreCommonConstant.TRACE_ID,traceId);
MDC.put(CoreCommonConstant.TRACE_ID, traceId);
// 设置用户信息
MDC.put(CoreCommonConstant.USER_INFO,req.getHeader(CoreCommonConstant.USER_INFO));
}
}
但是上传文件的时候报错,
org.springframework.web.multipart.MultipartException: Failed to parse multipart servlet request; nested exception is java.io.IOException: org.apache.tomcat.util.http.fileupload.FileUploadException: Stream closed
然后我把这个过滤其注释后就可以上传了,
我想保留这个过滤器,还有其它办法可以解决上传的问题吗??
yml配置项:
spring:
mvc:
hiddenmethod:
filter:
enabled: true
servlet:
multipart:
enabled: true
location: /upload_tmp
max-file-size: 10MB #设置单个上传的文件大小
max-request-size: 20MB #设置单次请求的文件的总大小
过滤器你不会是读一次文件,然后接口你又读一次文件吧?