Increase HTTP Post maxPostSize in Spring Boot

后端 未结 14 2238
无人共我
无人共我 2020-11-28 06:48

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:

14条回答
  •  星月不相逢
    2020-11-28 07:31

    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.
    

提交回复
热议问题