Multipart file maximum size exception - spring boot embbeded tomcat

后端 未结 6 1271
说谎
说谎 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: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;
    }
    

提交回复
热议问题