Setting max-file-size for multipart uploads in spring boot does not work with 50MB limit

Viewed 205

I'm using SpringBoot v.2.3.0.RELEASE

I have a multipart RESTapi and I need to limit the size of the uploaded file.

So, I added these properties into my application.properties file:

spring.servlet.multipart.max-file-size=10MB
spring.servlet.multipart.max-request-size=10MB

This works fine using 10MB limit but, If I change the values from 10MB to 50MB this do not works. In this case I did't receive any errors and the API works fine but, obviously I need to limit this upload.

I noticed that the limits sill works with values less 50MB (for example with 49MB are ok).

Is there some reason? Need I to add some other properties?

1 Answers

In my current Spring project I configure a bean with a custom MultipartResolver which explicitly sets the maximum upload size. For a file limit of 10MB and a total upload size limit of 50MB, you could use:

@Bean
public MultipartResolver multipartResolver() {
    CommonsMultipartResolver multipartResolver = new CommonsMultipartResolver();
    multipartResolver.setMaxUploadSizePerFile(10000000);
    multipartResolver.setMaxUploadSize(50000000);

    return multipartResolver;
}

Define this bean in Spring config class, which you may already have created somewhere (and if not, then create one to hold you bean definitions).

Related