小编典典

SpringBoot的@MultipartConfig maxFileSize未生效

spring-boot

我有一个带有MultipartConfig注解的控制器(其片段如下所示):

@RestController
@RequestMapping("packages")
@MultipartConfig(maxFileSize = 1024*1024*1024, maxRequestSize = 1024*1024*1024)
public class PackagesController
{
    @RequestMapping(method = RequestMethod.POST)
    public String create(@RequestParam("package") MultipartFile uploadedPackage)
    {
        // do stuff to the file
        return "create";
    }
}

但是,当我将文件上传到此端点时,它似乎正在使用默认的多部分配置值:

Caused by: org.apache.tomcat.util.http.fileupload.FileUploadBase$FileSizeLimitExceededException: The field package exceeds its maximum permitted size of 1048576 bytes.
    at org.apache.tomcat.util.http.fileupload.FileUploadBase$FileItemIteratorImpl$FileItemStreamImpl$1.raiseError(FileUploadBase.java:633) ~[tomcat-embed-core-8.0.32.jar:8.0.32]
    at org.apache.tomcat.util.http.fileupload.util.LimitedInputStream.checkLimit(LimitedInputStream.java:76) ~[tomcat-embed-core-8.0.32.jar:8.0.32]
    at org.apache.tomcat.util.http.fileupload.util.LimitedInputStream.read(LimitedInputStream.java:135) ~[tomcat-embed-core-8.0.32.jar:8.0.32]
    at java.io.FilterInputStream.read(FilterInputStream.java:107) ~[na:1.8.0_45]
    at org.apache.tomcat.util.http.fileupload.util.Streams.copy(Streams.java:98) ~[tomcat-embed-core-8.0.32.jar:8.0.32]
    at org.apache.tomcat.util.http.fileupload.util.Streams.copy(Streams.java:68) ~[tomcat-embed-core-8.0.32.jar:8.0.32]
    at org.apache.tomcat.util.http.fileupload.FileUploadBase.parseRequest(FileUploadBase.java:293) ~[tomcat-embed-core-8.0.32.jar:8.0.32]
    at org.apache.catalina.connector.Request.parseParts(Request.java:2776) ~[tomcat-embed-core-8.0.32.jar:8.0.32]
    ... 30 common frames omitted

我的应用程序如下所示:

@SpringBootApplication
public class Application {

    public static void main(String[] args) {
        ApplicationContext ctx = SpringApplication.run(Application.class, args);
    }

}

我缺少使@MultipartConfig注解生效的东西吗?


阅读 1034

收藏
2020-05-30

共1个答案

小编典典

如果只想控制多部分属性,则可以使用multipart.max-file-sizemultipart.max-request- size属性。例如,您可以100Mb通过在application.properties文件中添加以下配置来将最大大小增加到:

multipart.max-file-size=100MB
multipart.max-request-size=100MB

值可以使用后缀MBKB表示 兆字节千字节 大小。

引擎盖下,spring启动将创建一个MultipartConfigElement基于MultipartPropertiesMultipartConfigElement会在Servlet的登记使用,因为在规定Spring
MVC的文档
。您可以查看MultipartAutoConfigurationDispatcherServletConfiguration和Checkout
Spring Boot文档以获取更多信息。

2020-05-30