欢迎订阅关注公众号:赵KK日常技术记录
近期一个原业务图片上传的接口偶发会报异常,原有高可用方案是当上传七牛云失败上传腾讯云,都失败才会报异常,或者会存在目录创建失败的异常,此类异常重试即可解决,但近期异常如下
org.springframework.web.multipart.MaxUploadSizeExceededException: Maximum upload size exceeded; nested exception is java.lang.IllegalStateException: org.apache.tomcat.util.http.fileupload.FileUploadBase$FileSizeLimitExceededException: The field file exceeds its maximum permitted size of 10485760 bytes.
at org.springframework.web.multipart.support.StandardMultipartHttpServletRequest.handleParseFailure(StandardMultipartHttpServletRequest.java:121) ~[spring-web-5.1.4.RELEASE.jar!/:5.1.4.RELEASE]
at org.springframework.web.multipart.support.StandardMultipartHttpServletRequest.parseRequest(StandardMultipartHttpServletRequest.java:114) ~[spring-web-5.1.4.RELEASE.jar!/:5.1.4.RELEASE]
at org.springframework.web.multipart.support.StandardMultipartHttpServletRequest.<init>(StandardMultipartHttpServletRequest.java:87) ~[spring-web-5.1.4.RELEASE.jar!/:5.1.4.RELEASE]
at org.springframework.web.multipart.support.StandardServletMultipartResolver.resolveMultipart(StandardServletMultipartResolver.java:87) ~[spring-web-5.1.4.RELEASE.jar!/:5.1.4.RELEASE]
at org.springframework.web.servlet.DispatcherServlet.checkMultipart(DispatcherServlet.java:1175) ~[spring-webmvc-5.1.4.RELEASE.jar!/:5.1.4.RELEASE]
at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:1010) ~[spring-webmvc-5.1.4.RELEASE.jar!/:5.1.4.RELEASE]
at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:942) ~[spring-webmvc-5.1.4.RELEASE.jar!/:5.1.4.RELEASE]
at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:1005) ~[spring-webmvc-5.1.4.RELEASE.jar!/:5.1.4.RELEASE]
at org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:908) ~[spring-webmvc-5.1.4.RELEASE.jar!/:5.1.4.RELEASE]
at javax.servlet.http.HttpServlet.service(HttpServlet.java:665) ~[javax.servlet-api-4.0.1.jar!/:4.0.1]
at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:882) ~[spring-webmvc-5.1.4.RELEASE.jar!/:5.1.4.RELEASE]
at javax.servlet.http.HttpServlet.service(HttpServlet.java:750) ~[javax.servlet-api-4.0.1.jar!/:4.0.1]
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:231) ~[tomcat-embed-core-9.0.14.jar!/:9.0.14]
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) ~[tomcat-embed-core-9.0.14.jar!/:9.0.14]
at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53) ~[tomcat-embed-websocket-9.0.14.jar!/:9.0.14]
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193) ~[tomcat-embed-core-9.0.14.jar!/:9.0.14]
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) ~[tomcat-embed-core-9.0.14.jar!/:9.0.14]
at com.yh.csx.tms.core.security.TokenAuthenticationFilter.doFilter(TokenAuthenticationFilter.java:64) ~[csx-b2b-tms-core-3.1.1-SNAPSHOT.jar!/:3.1.1-SNAPSHOT]
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193) ~[tomcat-embed-core-9.0.14.jar!/:9.0.14]
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) ~[tomcat-embed-core-9.0.14.jar!/:9.0.14]
at org.springframework.boot.actuate.web.trace.servlet.HttpTraceFilter.doFilterInternal(HttpTraceFilter.java:90) ~[spring-boot-actuator-2.1.2.RELEASE.jar!/:2.1.2.RELEASE]
大意就是超过了限制10M大小从而引发报错,第一时间并没有点击源码进行查看,想的是针对此类异常进行catch后报正常错误,代码改造如下:
} catch (MaxUploadSizeExceededException e) {
log.error("上传图片超过限制:{}MB 操作人:{} 绝对路径:{} 异常:{} 轨迹:{}", size, userManager.getLocationCode() + userManager.getUserName() + userManager.getTelPhone(), tempFile.getAbsolutePath(), e.getMessage(), e);
ExceptionDetails details = ExceptionDetails.createBuilder(code(),msg())
.setBizLevel(5).setBizScope("上传图片超过限制")
.setBizDetails(msg()).build();
throw new Exception(details);
}
测试效果:
{
"code": 99990500,
"message": "未知异常"
}
仍旧报错,溯源源码,过程不在此啰嗦,大意是创建Http请求是在servlet请求的mock中会校验请求的大小
protected void handleParseFailure(Throwable ex) {
String msg = ex.getMessage();
if (msg != null && msg.contains("size") && msg.contains("exceed")) {
throw new MaxUploadSizeExceededException(-1, ex);
}
throw new MultipartException("Failed to parse multipart servlet request", ex);
}
也就是请求是发生在dofilter的前置阶段,尝试在filter阶段加catch仍然没效果,然后针对RestController层进行异常处理,RestControllerAdvice即使指定异常处理的,实现如下:
@RestControllerAdvice(annotations = {Controller.class, RestController.class})
public class FileUploadExceptionHandler {
@ExceptionHandler(MultipartException.class)
public CommonResponse uploadExcepttion(MultipartException e){
return CommonResponse.error(code(),msg());
}
}
配置如下:
#spring.servlet.multipart.max-file-size = 10MB
#spring.servlet.multipart.max-request-size = 10MB
#spring.servlet.multipart.resolve-lazily=true
#server.tomcat.max-swallow-size = 10MB
模拟请求如下:
测试效果:
查询日志调用链,原有业务是针对图片上传的,就算是市面上所有手机也不可能单张图片大于10M才对,溯源后发现,此类业务是由上游针对PDF调用的,但查询关联数据后发现PDF仅仅三行数据,外加电子签名超过10M导致失败,且用户无法感知此类错误,因为是由后台根据电子签名生成PDF的,此类错误即便报正常提示用户也体验不好。最终解决方案应有生成PDF端去压缩大小后控制在一定范围内,并根据调用解决来处理逻辑,这里的配置10M是不合理的,因为在特殊情况下的单据是有可能大于10M的,所有应由压缩端处理,调的挺好,下次别调了图片