[原创]SpringBoot上传图片踩的坑

最近项目里面有个需求,要上传图片到阿里云的OSS服务。所以需要写个上传图片的接口给前端。
这个简单的接口本来就给分配了1个工时,感觉也蛮简单的。但编码过程中遇到了好几个问题,现在一一记录下来,避免再次踩坑。

  • 1、图片不能超过1M
    报错信息:
org.springframework.web.multipart.MultipartException: Could not parse multipart servlet request; nested exception is java.lang.IllegalStateException: org.apache.tomcat.util.http.fileupload.FileUploadBase$FileSizeLimitExceededException: The field file exceeds its maximum permitted size of 1048576 bytes.

分析原因:
图片大小超过了最大限制大小1048576 bytes=1MB,在org.springframework.boot.autoconfigure.web.MultipartProperties中可以看到默认的maxFileSize为1MB。
解决方案:
在application.properties中配置

spring.http.multipart.maxFileSize = 10MB
  • 2、图片不能超过10M,连接重置
    报错信息:
org.springframework.web.multipart.MultipartException: Could not parse multipart servlet request; nested exception is java.lang.IllegalStateException: org.apache.tomcat.util.http.fileupload.FileUploadBase$SizeLimitExceededException: the request was rejected because its size (10559924) exceeds the configured maximum (10485760)

前端报错:
Status:(failed)net::ERR_CONNECTION_RESET

分析原因:
请求大小超过了最大限制大小10485760 bytes=10MB,在org.springframework.boot.autoconfigure.web.MultipartProperties中可以看到默认的maxRequestSize为10MB。

解决方案:

@RestControllerAdvice
@ResponseStatus(HttpStatus.BAD_REQUEST)
public class MultipartExceptionConfigHandler {

    private final Logger logger = LoggerFactory.getLogger(getClass());

    @ExceptionHandler(MultipartException.class)
    public MsCommonResult handleMultipartException(MultipartException icEx, HttpServletRequest request) {
        logger.warn("请求业务报错 uri: {}, message: {} ", request.getRequestURI(), icEx.getMessage());
        return MsCommonResult.fail("禁止上传大文件到服务器");
    }
}

配置ExceptionHandler后,但是并没有返回给前端400的状态码。继续研究后注意到org.apache.coyote.http11.AbstractHttp11Protocol的maxSwallowSize为2097152=2MB。
继续解决:

@Bean
public TomcatEmbeddedServletContainerFactory tomcatEmbedded() {
    TomcatEmbeddedServletContainerFactory tomcat = new TomcatEmbeddedServletContainerFactory();
    tomcat.addConnectorCustomizers((TomcatConnectorCustomizer) connector -> {
        if ((connector.getProtocolHandler() instanceof AbstractHttp11Protocol<?>)) {
            // -1 means unlimited
            ((AbstractHttp11Protocol<?>) connector.getProtocolHandler()).setMaxSwallowSize(-1);
        }
    });
    return tomcat;
}

至此,前台收到返回,返回信息如下:

HTTP/1.1 400
X-Application-Context: ms.maker.company:8071
Content-Type: application/json;charset=UTF-8
Transfer-Encoding: chunked
Date: Wed, 05 Dec 2018 09:37:58 GMT
Connection: close

{"result":500,"detail":"禁止上传大文件到服务器","data":null}

猜你喜欢

转载自www.cnblogs.com/Candies/p/10072411.html