Home > Mobile >  Spring boot uploading file error when try bigger file
Spring boot uploading file error when try bigger file

Time:01-08

I have this upload method:

try {
        Files.createDirectories(filesPath);
    } catch (IOException e1) {
        e1.printStackTrace();
        return null;
    }

    for (MultipartFile file : Arrays.asList(files)) {
        try {
            // Get the file and save it somewhere
            byte[] bytes = file.getBytes();
            Path path = Paths.get(filesPath   File.separator   file.getOriginalFilename());
            Files.write(path, bytes);
        } catch (IOException e) {
            e.printStackTrace();
            return null;
        }
    }

It work well, but when i try upload bigger file around 1,5GB i get this error:

Invalid string length

How can i fix it?

CodePudding user response:

Try to use data-type LONGBLOB for your 1.5-GB file storage into database.

CodePudding user response:

First you need to adjust two properties if you want to allow uploads of such big files.

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

Then it is better to use MultipartFile.getInputStream() to read the contents of the file.

You might also use IOUtils.copy() from Apache Commons IO to simplify your job.

  • Related