I have set max file size to
multipart.maxFileSize: 1mb
multipart.maxRequestSize: 1mb
This is my controller :
@RequestMappi
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.
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();
}
}
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
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
@Configuration
@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
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;
}
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.