Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Increase HTTP Post maxPostSize in Spring Boot

I've got a fairly simple Spring Boot web application, I have a single HTML page with a form with enctype="multipart/form-data". I'm getting this error:

The multi-part request contained parameter data (excluding uploaded files) that exceeded the limit for maxPostSize set on the associated connector.

I'm using Spring Boot's default embedded tomcat server. Apparently the default maxPostSize value is 2 megabytes. Is there any way to edit this value? Doing so via application.properties would be best, rather than having to create customized beans or mess with xml files.

like image 741
Jordan Avatar asked Oct 20 '15 09:10

Jordan


People also ask

How do I change the header size in spring boot?

Solution. We can increase the value of the max-http-header-size property in our application. properties file as per our requirements. In the above program, we can upgrade its value from the default 8kb to 40KB, which will resolve the problem.

How do you handle MaxUploadSizeExceededException?

And to handle the MaxUploadSizeExceededException you can add ControllerAdvice with ExceptionHandler. otherwise that exception will be triggered before the request is mapped to controller.

What is the use of spring boot framework?

Spring Boot helps developers create applications that just run. Specifically, it lets you create standalone applications that run on their own, without relying on an external web server, by embedding a web server such as Tomcat or Netty into your app during the initialization process.


2 Answers

In application.properties file write this:

# Max file size. spring.http.multipart.max-file-size=1Mb # Max request size. spring.http.multipart.max-request-size=10Mb 

Adjust size according to your need.


Update

Note: As of Spring Boot 2, however you can now do

# Max file size. spring.servlet.multipart.max-file-size=1MB # Max request size. spring.servlet.multipart.max-request-size=10MB 

Appendix A. Common application properties - Spring

like image 171
Sanjay Singh Rawat Avatar answered Oct 11 '22 12:10

Sanjay Singh Rawat


Found a solution. Add this code to the same class running SpringApplication.run.

// Set maxPostSize of embedded tomcat server to 10 megabytes (default is 2 MB, not large enough to support file uploads > 1.5 MB) @Bean EmbeddedServletContainerCustomizer containerCustomizer() throws Exception {     return (ConfigurableEmbeddedServletContainer container) -> {         if (container instanceof TomcatEmbeddedServletContainerFactory) {             TomcatEmbeddedServletContainerFactory tomcat = (TomcatEmbeddedServletContainerFactory) container;             tomcat.addConnectorCustomizers(                 (connector) -> {                     connector.setMaxPostSize(10000000); // 10 MB                 }             );         }     }; } 

Edit: Apparently adding this to your application.properties file will also increase the maxPostSize, but I haven't tried it myself so I can't confirm.

multipart.maxFileSize=10Mb # Max file size. multipart.maxRequestSize=10Mb # Max request size. 
like image 27
Jordan Avatar answered Oct 11 '22 11:10

Jordan