2

I have a spring boot web application which will handle large file (max size of 5g) upload and then save it to s3. The request and response may last for a long time.

So what is the best practice to handle the upload and download like this? How to make a good performance to prevent my server down when download or upload large files?

2 Answers 2

1

you can use multipart/form-data

 @RequestMapping(value = "/agency/create", method = RequestMethod.POST, consumes = "multipart/form-data")
    public ResponseEntity<List<String>> createAgency(
            @RequestParam(value = "username", required = true) String username,
            @RequestParam(value = "pic1", required = true)MultipartFile pic1File,
            MultipartHttpServletRequest request, ModelAndView modelAndView) {
        List<String> requestKeys=new ArrayList<String>();
        List<String> originalFileName=new ArrayList<String>();

        request.getFileNames().forEachRemaining(requestKeys::add);
        for(String multipartFile:requestKeys) {
            originalFileName.add(request.getFile(multipartFile).getOriginalFilename());
        }
        storageService.store(pic1File);
        return new ResponseEntity<List<String>>(originalFileName, HttpStatus.CREATED);
    }
Sign up to request clarification or add additional context in comments.

3 Comments

I don't have a frontend, all of them used as a restful api.
i will modify the answer for restful API
MultiPartFile javadocs says A representation of an uploaded file received in a multipart request. The file contents are either stored in memory or temporarily on disk. In either case, the user is responsible for copying file contents to a session-level or persistent store as and if desired. The temporary storage will be cleared at the end of request processing So it means file is uploaded first in memory, I think the requirement is to avoid that.
0

Posting in case someone finds this useful in the future. This works with a REST controller as of Spring Boot 2.4.2.

Class annotations:

@RestController
@RequestMapping("/api")

Method declaration:

@RequestMapping(path = "/file-upload/{depot}/{fileName}", method = {RequestMethod.POST, RequestMethod.PUT})
        public ResponseEntity<String> fileUpload(
                @PathVariable(name = "depot") String depot,
                @PathVariable(name = "fileName") String fileName,
                InputStream inputStream,
                HttpServletRequest request,
                HttpServletResponse response)

The above is the Spring Boot configuration for a REST Controller that worked for me for large file upload. The key was adding InputStream inputStream directly.

1 Comment

How large of a file were you generally working with? I have concerns that Spring might still try to put the entirety of the file in memory, which for multi-gigabyte files would blow out my server memory allocation.

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.