Saving the uploaded file

In this section, we will discuss how to save the uploaded file to the server's local file system, as well as save it to AWS S3.

The main API of file saving is the FileStorage interface, which has three methods:

  • saveAsTempFile(String, MultipartFile): TempFile
  • saveTempFile(TempFile)
  • saveUploaded(String, MultipartFile): String

The first method, saveAsTempFile(), is used to save the MultipartFile instance to the server's temp folder, which is specified in application.properties. This is useful when we need to create a thumbnail image before saving the file to S3. The second method, saveTempFile(), is used to save the temporary file to the final location, and the last method, saveUploaded(), is used to save the uploaded MultipartFile to the final location.

There are two implementations of FileStorage. They are LocalFileStorage and S3FileStorage. Figure 13.4 shows the relationships between FileStorage and its implementations:

Figure 13.4: File storage diagram

FileStorageResolver is responsible for finding what is the active file storage and returning the corresponding FileStorage instance. It will look for the app.file-storage.active property to decide the active file storage. You can find details of the file saving in the commit history on GitHub.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.189.171.125