Hi,

In my application, I would like to have an API endpoint for file uploads with a Content-Type: multipart/form-data that consumes the InputStream chunk by chunk and send it to an external storage service. I want the application to do this without having the files copied to the disk temporarily.

I'm relying on the MultipartResolver bean to handle incoming requests. In the end I expect the following method controller:

public void upload(MultipartFile file) {
    ...
}

to provide the file parameter without any extra code.

The solution that I found is to set the spring.servlet.multipart.file-size-threshold property to the maximum value possible, which is the limit of an integer, i.e. 17GB (measured in bytes). This means that the file is only saved to disk if it exceeds that size. I'm assuming that this hard limit exists because this configuration was not meant to be used for the use case I described above, i.e. streamed uploads.

Here is where the cast to int happens MultipartConfigFactory.

The other solution is to implement theFileItemFactory as an alternative to the DiskFileItemFactory. It would require a lot of configuration and potentially high maintenance, thus I would like to avoid it.

In case it helps, I'm using Spring Boot 3.1.0 with Spring Framework 6.0.9.

Thanks in advance!

Comment From: pedrodovale

Given this issue's comment, I'd like to add that my application is using the embedded Tomcat server.

Comment From: jhoeller

Since from a Spring Framework perspective, we entirely rely on the Servlet container doing multipart resolution for us these days, I'm afraid there is nothing we can do about it here.

MultipartConfigFactory is a Spring Boot configuration element - but even there, the int limit comes from the Servlet API MultipartConfigElement type.

Maybe this is a topic to be raised with the Tomcat project since you are referring to theFileItemFactory etc which is from Tomcat's embedded Commons FileUpload, I suppose?

Comment From: pedrodovale

thank you for the clarification @jhoeller