【问题标题】:Spring Boot - Multipart file maximum upload size exceptionSpring Boot - 多部分文件最大上传大小异常
【发布时间】:2019-07-24 06:52:52
【问题描述】:

我正在使用 Spring Boot 2.1.3(使用标准 Tomcat 嵌入式 Web 服务器)开发一个端点来上传图像,我想限制分段上传的大小。我可以通过属性轻松做到这一点:

spring:
    servlet:
        multipart:
            max-file-size: 2MB
            max-request-size: 2MB

但我总是得到一个 Spring 无法捕获的 500,因为是 Tomcat 引发了异常并且请求没有到达我在 RestController 中的代码。

2019-03-02 10:12:50.544 ERROR [] [   o.a.c.c.C.[.[.[/].[dispatcherServlet]] [] Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is org.springframework.web.multipart.MaxUploadSizeExceededException: Maximum upload size exceeded; nested exception is java.lang.IllegalStateException: org.apache.tomcat.util.http.fileupload.FileUploadBase$SizeLimitExceededException: the request was rejected because its size (3917946) exceeds the configured maximum (2097152)] with root cause 
org.apache.tomcat.util.http.fileupload.FileUploadBase$SizeLimitExceededException: the request was rejected because its size (3917946) exceeds the configured maximum (2097152)

我的 ExceptionHandler 是这样的,但显然不管注释中的异常是什么都不起作用:

@ExceptionHandler(MaxUploadSizeExceededException.class)
public ResponseEntity handleMaxSizeException(Exception e) {
    logger.warn(e.getMessage());
    ...
    return status(HttpStatus.PAYLOAD_TOO_LARGE).body(...);
}

我已经尝试过使用已经提到的属性,但没有任何效果:

@Bean
public TomcatServletWebServerFactory containerFactory() {
    TomcatServletWebServerFactory factory = new TomcatServletWebServerFactory();
    factory.addConnectorCustomizers((TomcatConnectorCustomizer) connector -> 
        ((AbstractHttp11Protocol<?>) connector.getProtocolHandler()).setMaxSwallowSize(-1));
    return factory;
}

我已经检查了以下问题(以及其他问题......)的答案,但它们大多已过时或根本不起作用:

有人在为此苦苦挣扎吗?

【问题讨论】:

    标签: java spring spring-boot


    【解决方案1】:

    有点晚了。您需要在 application.properties 或 application.yml 中设置 server.tomcat.max-swallow-size=-1 有了它,tomcat 将不会干扰上传请求,并且操作将完全由 spring 处理,您的异常控制器将完美运行。 References

    【讨论】:

      【解决方案2】:

      请将此添加到您的application.properties:

      spring.servlet.multipart.max-file-size=50MB
      spring.servlet.multipart.max-request-size=50MB
      

      您可以在上面的配置中根据需要更改大小

      【讨论】:

      • 感谢您的帮助,但您应该仔细阅读问题。在第一段中,我已经说过我已经尝试过这些属性。
      • 50Mb 对我不起作用。我得到failed to convert java.lang.String to org.springframework.util.unit.DataSize
      • 它应该是 'MB' 而不是 'Mb'
      • 这对我有类似的问题很有用,但仅限于大写 B,如前所述
      【解决方案3】:

      遇到了类似的问题。 Tomcat 一直抱怨请求超出了配置的最大值 (2097152),即使我在全局 web.xml 中有默认设置:

            <max-file-size>52428800</max-file-size>
            <max-request-size>52428800</max-request-size>
      

      终于想通了:我正在开发的servlet类扩展了一个带有@MultipartConfig注解的servlet类,但是我的本地类没有@MultipartConfig注解。所以请求没有被视为多部分,这导致了这个错误。

      引发错误的代码在这些行的超类中:

      //Search through the parts for an uploaded file
      for (Part part : request.getParts()) {
      

      【讨论】:

        猜你喜欢
        • 2019-11-10
        • 1970-01-01
        • 2018-03-30
        • 2015-12-28
        • 2014-10-31
        • 2014-05-01
        • 1970-01-01
        • 1970-01-01
        • 2019-04-10
        相关资源
        最近更新 更多