Create a spring boot application that stores images
Have you ever wondered how the images on your instagram post get saved everytime you log in into the application?
In this article, I will talk about how to create a spring boot application that handles saving/retrieving images.
Before starting on this tutorial, please complete the tutorial specified in Setting your own spring boot server via JDBC part 1 | by Michael Tong | Dev Genius in order to have a spring boot base application setup.
I personally set up my project to be called com.example.imageProject instead of com.example.Demo but that should not make a huge difference.
Step 1: Under com.example.Demo, create 4 packages: controller, service, model, repository.
Step 2: Inside the repository package, create a file called ImageHolderRepository.java:
This file is what our service is going to use store/retrieve images.
Step 3: Inside the model package, create a file called File.java:
Here we have a file id, a file, filetype, and filename. When the user makes a request to store an image, the image get serialized into a byte array, which will then be stored into the database. The fileType will tell what kind of file it is, whether it is a png or jpg or some other file. Filename is relatively self-explanatory so I am not going to dig too deep here.
Step 4: Inside the service package, create an interface class called ImageHolderService.java:
Afterward, in the same package, create a class called ImageHolderServiceImpl.java:
Over here, we support a few functionalities: getImageHolderById, createImageHolder, updateImageHolder, deleteImageHolder. With getImageHolderById, we return the file that are associated with the passed in Id.
With createImageHolder, We take an array of Multipartfile files. These files are the files that were passed by the api call as an attempt to store them into the system. But we don’t directly store this multipartfile array into the system.
First, we create an arraylist, which will then store all the files that will be processed. We take each Multipartfile and parse its bytes, filename and content type and create a new File object. Afterward, we add this file object back into the arraylist and then save it into the database(imageRepository.saveAll(fileList).
At last, we have a deleteImageholder function that simply deletes the image. I haven’t implemented updateImageHolder so we can leave that out for now.
Now that most of the application is setup, let’s go ahead and set up the endpoints for this.
Step 5: Inside the controller package, create a class called ImageController.java with the following content:
Over here, we have four api endpoints: one that returns a response entity with the image file, one that creates the file and then return it in the form of a file in a list. We also have two other calls, one updating the actual file in the system and another one that deletes the image file associated with a particular user id.
That’s it! You just learned how to set up an application that saves images in the database.
Well…how are we going to test it out?
We can use postman, which is a tool to test APIs. If you have not already installed in your machine, feel free to check this url to download the tool: Postman API Platform | Sign Up for Free.
Step 1: Go ahead and create a new collections called Images:
Over here, we have two api calls setup: one for upload image and get image. Let’s add a request(right click into images and you will see an option) that looks like this:
Over here, we create a post call that simply uploads the image. For the body section, hover over where the value placeholder is at and you can choose to select a file. After that, click “select file” and choose an image. Afterward, you can click to save the image into the server.
We can also a get image calls that retrieves the image:
That’s it! you have all the information you need to test out these endpoints.