-1

I have written a springboot application to upload files . while im trying to upload 5gb file im getting outofmemory error error console

Praveen s
  • 7
  • 3

2 Answers2

0

Without any code it is hard to say. But ... I guess that you load the whole file at once into your memory. That happens, when you use byte[]. So try to use streams instead of byte[].

Here are some examples

Spring WebClient -> Stream-uploading large files via Spring WebClient
Spring RestTemplate -> How to forward large files with RestTemplate?

akop
  • 5,981
  • 6
  • 24
  • 51
-1

spring.http.multipart.max - file - size = 6000 MB spring.http.multipart.max - request - size = 6000 MB

    public ResponseEntity<?> uploadFile (@RequestParam("file") MultipartFile multipartfile){
        ResponseEntity<String> response;
        multipartfile.getOriginalFilename();
        byte[] bufferedbytes = new byte[1024];
        File file = new File("/home/david/Music/" + multipartfile.getOriginalFilename());
        FileOutputStream outStream = null;
        int count = 0;
        try {
            BufferedInputStream fileInputStream = new BufferedInputStream(multipartfile.getInputStream());
            outStream = new FileOutputStream(file);
            while ((count = fileInputStream.read(bufferedbytes)) != -1) {
                outStream.write(bufferedbytes, 0, count);

            }
            outStream.close();
        } catch (IOException e) {
            response = new ResponseEntity<String>("File failed to upload" + multipartfile.getOriginalFilename(), HttpStatus.PAYLOAD_TOO_LARGE);

            return response;

        }

        response = new ResponseEntity<String>("File uploaded sucessifully" + multipartfile.getOriginalFilename(), HttpStatus.OK);

        return response;
    }
  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Dec 13 '22 at 17:26