Multipart file maximum size exception - spring boot embbeded tomcat

后端 未结 6 1242
说谎
说谎 2021-01-04 20:03

I have set max file size to

multipart.maxFileSize: 1mb
multipart.maxRequestSize: 1mb

This is my controller :

@RequestMappi         


        
相关标签:
6条回答
  • 2021-01-04 20:27

    I wrote some lines in application.yml to solve this issue like:

    spring:
        http:
            multipart:
                max-file-size: 10MB
                max-request-size: 10MB
    

    but it didn't solve the problem. However, adding same lines in application.properties after formatting to single liner way i.e. properties file way then it worked.

    0 讨论(0)
  • 2021-01-04 20:38

    The below code worked for me. Add it to spring boot main class:

    import javax.servlet.MultipartConfigElement;
    
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    import org.springframework.boot.web.servlet.MultipartConfigFactory;
    import org.springframework.context.annotation.Bean;
    import org.springframework.scheduling.annotation.EnableAsync;
    import org.springframework.scheduling.annotation.EnableScheduling;
    
    
    
    @SpringBootApplication
    @EnableScheduling
    @EnableAsync
    public class Application {
    
        public static void main(String[] args) {
            SpringApplication.run(Application.class, args);
        }
        
        @Bean
        MultipartConfigElement multipartConfigElement() {
            MultipartConfigFactory factory = new MultipartConfigFactory();
            factory.setMaxFileSize("5MB");
            factory.setMaxRequestSize("5MB");
            return factory.createMultipartConfig();
        }
    }
    
    0 讨论(0)
  • 2021-01-04 20:39

    Started from Spring Boot 2

    spring.servlet.multipart.max-file-size=128KB
    spring.servlet.multipart.max-request-size=128KB

    See docs

    Spring Boot 1.x

    Properties should like:
    spring.http.multipart.max-file-size=128KB
    spring.http.multipart.max-request-size=128KB

    See spring boot guides

    0 讨论(0)
  • 2021-01-04 20:49

    Since @SeaBiscuit has provided the correct answer but with Spring boot 2.0.0.RELEASE the class org.springframework.boot.context.embedded.tomcat.TomcatEmbeddedServletContainerFactory has been removed and replaced by the class org.springframework.boot.web.embedded.tomcat.TomcatServletWebServerFactory. So the code for the new Spring boot 2.0.0 would be

    1. Create a class with any name and annotate it with @Configuration
    2. And put the below code inside that class
    @Bean
    public TomcatServletWebServerFactory containerFactory() {
        TomcatServletWebServerFactory factory = new TomcatServletWebServerFactory();
        factory.addConnectorCustomizers(new TomcatConnectorCustomizer() {
            @Override
            public void customize(Connector connector) {
                ((AbstractHttp11Protocol<?>) connector.getProtocolHandler()).setMaxSwallowSize(-1);
            }
        });
        return factory;
    }
    

    And if you've trouble configuring the maximum file upload size in Spring boot 2.0.0 put below code inside 'application.propertise' file with desired file size in 'MB's

    ## Image upload limitation properties
    spring.servlet.multipart.max-file-size=3MB
    spring.servlet.multipart.max-request-size=3MB
    
    0 讨论(0)
  • 2021-01-04 20:51

    This was tricky. Tomcat property MaxSwallowSize was causing this problem. Apparently it was introduced in one of the recent versions of Tomcat. The whole idea behind it was if Tomcat realized the request was going to be rejected, to terminate the connection anything higher than default 2mb (at least this was my interpretation). Simple overriding this property fixes things. I realize this is not perfect solution, but it is a whole lot better than just terminating connection.

    @Bean
    public TomcatEmbeddedServletContainerFactory containerFactory() {
        TomcatEmbeddedServletContainerFactory factory = new TomcatEmbeddedServletContainerFactory();
         factory.addConnectorCustomizers(new TomcatConnectorCustomizer() {
            @Override
            public void customize(Connector connector) {
             ((AbstractHttp11Protocol<?>) connector.getProtocolHandler()).setMaxSwallowSize(-1);
            }
         });
         return factory;
    }
    
    0 讨论(0)
  • 2021-01-04 20:51

    Please add the below lines in application.properties for spring boot version -2.0.1.RELEASE

    spring.servlet.multipart.max-file-size=128MB
    spring.servlet.multipart.max-request-size=128MB
    spring.servlet.multipart.enabled=true
    

    This resolved my issue.

    0 讨论(0)
提交回复
热议问题