【问题标题】:Java Spring max. size limit for uploadJava 弹簧最大值上传大小限制
【发布时间】:2018-06-25 14:33:57
【问题描述】:

我尝试将文件上传到我的控制器。

这是我的控制器的上部。它一直有效,直到我达到 +- 2MB 的最大请求大小

> @RequestMapping(value = {"/runlocalfiles"}, method =
> RequestMethod.POST, produces = "application/json")
>     @ResponseBody
>     @CrossOrigin(origins = "http://localhost:4200")
>     public ResponseEntity run( HttpServletRequest request) {
>         String jsonBase64Files = request.getParameter("base64files");
>         String jsonChecks = request.getParameter("checks");

浏览器中的错误信息:

> Failed to load http://localhost:5001/runlocalfiles: No
> 'Access-Control-Allow-Origin' header is present on the requested
> resource. Origin 'http://localhost:4200' is therefore not allowed
> access. The response had HTTP status code 500.

Java 控制台出错:

> java.lang.IllegalStateException: The multi-part request contained
> parameter data (excluding uploaded files) that exceeded the limit for
> maxPostSize set on the associated connector   at
> org.apache.catalina.connector.Request.parseParts(Request.java:2893)
> ~[tomcat-embed-core-8.5.20.jar:8.5.20]...

我尝试通过向 application.properties 添加新行来增加上传大小。还尝试将 -1 更改为例如100MB

> spring.servlet.multipart.max-file-size= -1
> spring.servlet.multipart.max-request-size= -1
> spring.http.multipart.max-file-size = -1
> spring.http.multipart.max-request-size= -1

任何帮助都可以得到。

【问题讨论】:

    标签: java spring upload-max-filesize


    【解决方案1】:

    所以我找到了一个可行的解决方案。我需要结合你的两个解决方案。

    将此添加到初始类:

    // 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
                        }
                );
            }
        };
    }
    

    这是应用程序属性:

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

    【讨论】:

      【解决方案2】:

      试试这个,它对我有用,将它添加到应用程序属性中:

      server.tomcat.max-http-post-size=-1
      

      【讨论】:

        【解决方案3】:

        如果你使用的是 Spring Boot 并嵌入了 tomcat,你可以试试这个:

        @SpringBootApplication  
        public class SpringBootWebApplication {  
        
            //private int maxUploadFileSize = 10 * 1024 * 1024;  
        
            public static void main(String[] args) throws Exception {  
                SpringApplication.run(SpringBootWebApplication.class, args);  
            }  
        
        
            //Tomcat large file upload connection reset  
            @Bean  
            public TomcatEmbeddedServletContainerFactory tomcatEmbedded() {  
        
                TomcatEmbeddedServletContainerFactory tomcat = new TomcatEmbeddedServletContainerFactory();  
        
                tomcat.addConnectorCustomizers((TomcatConnectorCustomizer) connector -> {  
                    if ((connector.getProtocolHandler() instanceof AbstractHttp11Protocol<?>)) {  
                        //-1 means unlimited  
                        ((AbstractHttp11Protocol<?>) connector.getProtocolHandler()).setMaxSwallowSize(-1);  
                    }  
                });  
        
                return tomcat;  
            }  
        }  
        

        【讨论】:

        • 使用上面我得到的代码:org.apache.tomcat.util.http.fileupload.FileUploadBase$FileSizeLimitExceededException:base64files 字段超出了其最大允许大小 1048576 字节。
        【解决方案4】:

        我遵循了以下spring boot file upload tutorial 并且如教程中所述,以下属性非常适合我:

        spring.servlet.multipart.max-file-size=300MB
        spring.servlet.multipart.max-request-size=300MB
        spring.http.multipart.enabled=false
        

        【讨论】:

        • 如果我在 application.properties 中使用它,控制器接受请求,但请求的内容为空。
        猜你喜欢
        • 1970-01-01
        • 2013-10-19
        • 1970-01-01
        • 2011-08-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-06-23
        • 1970-01-01
        相关资源
        最近更新 更多