Increase Http Post Maxpostsize in Spring Boot

Increase HTTP Post maxPostSize in Spring Boot

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.

Java Spring max. size limit for upload

So i found a working solution. I need to combine your two solutions.

Adding this to the initial class:

// 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(100000000); // 100 MB
}
);
}
};
}

and this to the application properties:

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


Related Topics



Leave a reply



Submit