Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring WebFlux: set max file(s) size for uploading files

I'm using flux of FileParts to upload files @RequestPart(FILES) Flux<FilePart> files

And trying to limit maximum size of files. Looks like old way does not work:

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

And I dont have any methods to get file size in FilePart interface. So is ther any way to limit max size of uploaded file in webflux, whithout copying it? I know that there are headers like Content-Length, but it does not look secure way.

like image 547
xander27 Avatar asked Aug 09 '19 06:08

xander27


People also ask

How to handle file upload in Spring Boot?

Spring Boot file uploader Create a Spring Boot application and include the Spring Web facilities; Create a Spring @Controller class; Add a method to the controller class which takes Spring's MultipartFile as an argument; Save the uploaded file to a directory on the server; and.

What is spring servlet multipart Max-file-size?

spring. servlet. multipart. max-file-size is set to 128KB, meaning total file size cannot exceed 128KB.

What is multipart file size threshold?

Class MultipartProperties max-request-size specifies the maximum size allowed for multipart/form-data requests. The default is 10MB. file-size-threshold specifies the size threshold after which files will be written to disk. The default is 0.


1 Answers

The Web on Reactive Stack documentation states the following:

For file parts written to disk, there is an additional maxDiskUsagePerPart property to limit the amount of disk space per part. There is also a maxParts property to limit the overall number of parts in a multipart request. To configure all 3 in WebFlux, you’ll need to supply a pre-configured instance of MultipartHttpMessageReader to ServerCodecConfigurer.

I was able to apply it via this configuration class:

@Configuration
@EnableWebFlux
public class WebConfig implements WebFluxConfigurer {

    @Override
    public void configureHttpMessageCodecs(ServerCodecConfigurer configurer) {
        SynchronossPartHttpMessageReader partReader = new SynchronossPartHttpMessageReader();
        partReader.setMaxParts(1);
        partReader.setMaxDiskUsagePerPart(10L * 1024L);
        partReader.setEnableLoggingRequestDetails(true);

        MultipartHttpMessageReader multipartReader = new MultipartHttpMessageReader(partReader);
        multipartReader.setEnableLoggingRequestDetails(true);

        configurer.defaultCodecs().multipartReader(multipartReader);
    }

}
like image 109
amoebob Avatar answered Sep 27 '22 22:09

amoebob