Simplest and Easy way to Upload and Download Files in Java with Spring Boot

Uploading and Downloading Files is one of the core functionality that any Enterprise Application wants to incorporate. In this article, we will see How to Upload and Download Files in Java with Spring Boot.

Softwares used

  • Spring Boot 2.0.3.RELEASE
  • Thymeleaf
  • Java 8
  • Maven
  • Eclipse

Maven Dependencies

Below are the key dependencies that we are using.

Our code is built on top of Web Starter Application. We are using WebJars for Bootstrap and Jquery.

At the core of this application will be our service class – FileSystemStorageService.java. We will look at each of its functions in the rest of the article.

Initialization

This code is executed after the service class object is created. In this init method, we try to create the directory where we want to upload our files.

Storing the file

This method will get a MultipartFile from Spring controller. The file name is then resolved relative to our upload directory and copied there.

File as Resource

Above code, converts a file that we want to download into a Resource. This resource is later pushed to download via the controller.

Now let us look at few controller methods which utilize above service class to achieve the functionality.

Handle File Upload

Above method will kick off, when you upload a file from UI. The Spring controller receives a MultipartFile, which is then sent to storage service class.

Downloading a File

Downloading a file is 2 step process. First, we have to list all the files in the URL form and when the user clicks on any of the links, we will send the actual file.

Listing of files uses MvcUriComponentsBuilder to prepare the URL based on the method which is going to actually serve the file for download. When a user clicks on a file name headers and attachments is sent to the client.

Demo: Upload and Download Files in Java

Upload and Download Files in Java

Notice that we are allowing only text files to be uploaded. The upload button is enabled only when a user selects text file. The code for this is available in upload.js.

Upload and Download Files in Java

Once you select a text file and click on Upload, you will see the message that file is uploaded successfully.

You can check the files which we uploaded and listed on our page are physically available on our servers.

Conclusion

In this article, we have seen how Spring Boot has made Upload and Download Files in Java easy to implement.

The complete code is available at our GitHub repo. Please feel free to download and try.

Download Code
12 Comments
  1. November 12, 2020 | Reply
  2. rathin
    April 2, 2020 | Reply
  3. Dhiaa
    January 14, 2020 | Reply
  4. Dhiaa
    January 14, 2020 | Reply
    • Ankit
      January 22, 2020 | Reply
  5. Saras
    November 17, 2019 | Reply
  6. Juan
    October 28, 2019 | Reply
    • Pavan
      November 15, 2019 | Reply
  7. Andi
    July 31, 2019 | Reply
    • Pavan
      August 1, 2019 | Reply
  8. lucario
    April 13, 2019 | Reply
    • Pavan
      April 14, 2019 | Reply

Add a Comment

Your email address will not be published. Required fields are marked *