Learn how to create an application to upload files using Spring Boot

Madhusudhanan
Level Up Coding
Published in
3 min readOct 7, 2019

--

Hey folks, in this blog we are going to build an application using Spring Boot, that will be used to upload and process it. This can be any file, but I’m gonna explain it with a simple text file. Let’s get started.

But before going further, I assume you already know the below points.

1. Gradle
2. Spring Boot
3. Java 8
4. REST API

Let’s create a new project from start.spring.io. I have added only two dependencies, web and Lombok.

build.gradle

We will start by writing a simple REST Controller. In this Controller, we are having one method, which handles POST request, and it will ask Spring to give the uploaded file, in the client, as MultipartFile object. Upon receiving the file, we will read each line in the file and print them just for this tutorial purpose.

UploadController.java

We autowire the service class, which will be given by Spring as bean, and calls the consumeFile() method. That’s it. Let’s write our service class which will handle this part.

UploadService.java

We have only one method that takes the uploaded file as a MultipartFile object and processes it.

  1. Create an InputStream of the multipart file. We are using Java 7’s try-with-resource to automatically close the InputStream.
  2. Next is copying the contents in the MultipartFile object to a text file in the temp directory.
  3. Next, we read all the lines from the text file that we just created in the previous step and print it.
  4. Finally, we are deleting the text file, created at temp directory, because it is meaningless in storing it on our disk.

There is another way of consuming the uploaded file and operate on it. Yes, instead of creating a temp file, we can directly use the inputStream by buffering it. This method is more efficient than the previous one. Below is the screenshot that does so. We create a BufferedReader and use lines() method to get each line as Stream<String> and then processing it which in this case is just printing them out.

UploadService.java

Voila. We are done. This is a short example. But you can extend this with any of your logic. For example, if you want to upload a master data, which is in a spreadsheet document, you can do this easily. Or you may create a “file storage service” that will internally store the uploaded file to cloud storage and we can return the reference of the file(maybe a short URL) in response. With this reference, we can get the file anytime and anywhere.

Let’s run and test the application. I’m using the Gradle wrapper to build the jar and run it as a standalone application as Spring will provide the embedded Tomcat server by default. Run the below command to build up the jar and to run the application.

./gradlew clean build

java -jar ./build/libs/upload-demo-0.0.1-SNAPSHOT.jar

To test the API, we can use Postman. Below is the output displayed in the console.

Simple, right? Share your feedback. Thank you for reading!

--

--