网上好多关于getinputSteam()重复读取的报错都是和@RequestBody 注解的冲突,都是通过extends HttpServletRequestWrapper 类重写getReader() 和getInputStream() 解决的。
但是我的竟然在使用 multipart/form-data 方式传递参数时候,老是报getInputStream() has already been called for this request。。。
我就对contentType 为 multipart/form-data 方式单独拿出来做了特殊处理,
public class MyCorsFilter extends OncePerRequestFilter {
private CommonsMultipartResolver multipartResolver = new CommonsMultipartResolver();
@Override
protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain) throws ServletException, IOException {
if (request.getContentType().contains("multipart/form-data")) {
MultipartHttpServletRequest multiReq = multipartResolver.resolveMultipart(request);
Map<String,String[]> map = multiReq.getParameterMap();
for (Map.Entry<String, String[]> entry : map.entrySet()) {
for (String a: entry.getValue()) {
errString = a;
if (isSpecialChar(errString)) {
b = 1;
break;
}
}
if (b == 1){
break;
}
}
if (b == 1) {
response.getWriter().write(JsonUtils.obj2String(Output.error("("+errString+")中含有特殊字符")));
} else {
filterChain.doFilter(request, response);
}
}
}