springspring-mvcmultipart

How to Prevent Spring Multipart File Uploads from Being Stored in Memory and Stream Directly to Disk?


I'm developing a Spring web application where I need to handle large file uploads using multipart requests. Currently, the entire file is stored in memory, and I would like to configure the application to stream files directly to disk, avoiding storage in memory.

 @PostMapping(consumes = [MediaType.MULTIPART_FORM_DATA_VALUE])
 fun insert(
   @RequestPart
   file: MultipartFile
 )

During debugging, I found that memory usage occurs even before the request reaches the handler. This indicates that the file is fully loaded into memory at the early stages of request processing.

I tried using HttpServletRequest to directly access the stream and manage file uploads manually, but this still resulted in files being fully loaded into memory rather than in parts as they arrive over the network.

 @PostMapping(consumes = [MediaType.MULTIPART_FORM_DATA_VALUE])    
 fun insert(request: HttpServletRequest)

Solution

  • In my case, the problem was that Linux was trying to optimize file writing to the disk by writing them to RAM (so the RAM was used up not by my Java application). Therefore, I rewrote the solution so that it processes the file in a streaming manner instead of writing it to disk, and that helped.