0

I tried to upload files to my controller.

This is the upper part pf my controller. It works till i reach i maximum request size of +- 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");

Error message in browser:

> 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.

Error in Java console:

> 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]...

I have tried to increase the upload size by adding new lines to the application.properties. Also tried to change -1 to e.g. 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

Any help is apriciated.

4 Answers 4

3

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
Sign up to request clarification or add additional context in comments.

Comments

1

Try this,it's working for me,add this to the application properties:

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

Comments

0

if you're using Spring Boot and embed tomcat, you can try this:

@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;  
    }  
}  

1 Comment

Using the code above i got: org.apache.tomcat.util.http.fileupload.FileUploadBase$FileSizeLimitExceededException: The field base64files exceeds its maximum permitted size of 1048576 bytes.
0

I followed through the following spring boot file upload tutorial and as mentioned in the tutorial, following properties worked perfectly for me:

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

1 Comment

If i use this within the application.properties the controller accept the request, but the content of the request is null.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.