0

I have this service in spring boot app:

@Service
public class FileStorageService {
    private final Path root = Paths.get("uploads");

    public void init() {
        try {
          Files.createDirectories(root);
        } catch (IOException e) {
          throw new RuntimeException("Could not initialize folder for upload!");
        }
      }
    
      public ResponseEntity<?> save(MultipartFile[] files) {
        if (files.length == 0) {
            //return ResponseEntity.badRequest();
        }

       
    
        Arrays.asList(files).stream().forEach(file -> {
            try {
                // Get the file and save it somewhere
                byte[] bytes = file.getBytes();
                Path path = Paths.get(root + File.separator + file.getOriginalFilename());
                Files.write(path, bytes);
    
            } catch (IOException e) {
                e.printStackTrace();
            }
        });

        
        return new ResponseEntity<>(null, HttpStatus.OK);
        
    }
    
}

There is some folder for saving files "uploads". And i am now wondering what is

spring.servlet.multipart.location=

setting used for. Is this just folder where raw data from request are stored before they are saved in "uploads" folder?

Sizor
  • 17
  • 4
  • Does this answer your question? [How does one specify a temp directory for file uploads in Spring Boot?](https://stackoverflow.com/questions/29923682/how-does-one-specify-a-temp-directory-for-file-uploads-in-spring-boot) – life888888 Jan 03 '23 at 14:01

1 Answers1

0

As mentioned in this document for spring.servlet.multipart.location,

location specifies the directory where uploaded files will be stored. When not specified, a temporary directory will be used.

I dig around some internal code and found out that, it depends on the spring.servlet.multipart.file-size-threshold configuration. As mentioned in the doc

specifies the size threshold after which files will be written to disk. The default is 0.

If you mention any non-zero value then it will attempt to store that much in memory first before, writing to disk. For your case, if you mention any location in spring.servlet.multipart.location the file will directly stored over that location only. You may also want to check spring.servlet.multipart.max-file-size and spring.servlet.multipart.max-request-size property.

Saurabh
  • 882
  • 1
  • 5
  • 16
  • ok, thanks, still wondering how to do it in my case, cause i have system on pretty small disk and there will app running and then i have connect large disk and i want all files go straight to that external connected disk, to avoid the situation that the file is too large. I will be uploading files about 2GB. Can you just hint me some solution exactly for my case? – Sizor Jan 04 '23 at 09:57
  • Sorry, my bad. I miss understood the internal code. For you just configured the `spring.servlet.multipart.location` keeping `spring.servlet.multipart.file-size-threshold` to default value to zero. It will store all files directly that place only. But as you mention you want to upload file about 2GB you have to change `max-file-size` and `max-request-size` as well accordingly. – Saurabh Jan 05 '23 at 10:56