Welcome to the world of “Spring Boot File Upload and Download REST API” In today’s data-driven era, handling files within your Spring Boot applications, while seamlessly integrating them with a database, is essential. This article will guide you through the process of efficiently managing file uploads and downloads in your Spring Boot projects, with a strong emphasis on database integration. By the end, you’ll have the knowledge and skills to create a robust and responsive file management system that ensures data integrity and a superb user experience.
In today’s data-driven world, handling file uploads and downloads within your Spring Boot applications is a crucial aspect of building feature-rich and user-friendly platforms. In this comprehensive guide: Effortless Spring Boot File Upload and Download REST API, we will embark on a journey to demystify and streamline the process of effortlessly managing file uploads and downloads within Spring Boot applications, with a special focus on database integration. By the end of this guide, you’ll possess the knowledge and skills needed to handle files in your Spring Boot projects efficiently.
Spring Boot offers an array of tools and features that simplify the process of handling files, and when combined with database integration, it opens up a world of possibilities. From storing and retrieving files to managing metadata and access control, this guide will equip you with the expertise needed to implement a robust and scalable file management system within your Spring Boot application. Whether you’re dealing with images, documents, or any other file type, understanding the intricacies of “Spring Boot File Upload and Download REST API” is vital for delivering a seamless and responsive user experience.
Throughout this article, we will explore various techniques and best practices for integrating file handling with a database using Spring Boot. We will delve into the mechanics of uploading files, storing them efficiently, and enabling users to download their files securely. Additionally, we’ll discuss how to manage file metadata, handle concurrency issues, and ensure data integrity within your database. So, if you’re ready to enhance your Spring Boot skills and build feature-rich applications with effortless file upload and download capabilities, you’re in the right place. Let’s dive into the world of “Spring Boot File Upload and Download REST API” and unlock its full potential.
- First way is that compress the image and store into the database
- Second way is store the direct image into the file system and store the file system path into the database
Spring boot upload image is one of the most commonly used feature while developing enterprise application using Spring boot. Whenever we have upload/download feature we will create a seprate Spring boot upload image rest api then we will call this inside another function.
In this article we will cover the first way where we will compress and decompress the image. In the next article, we will discuss the second approach.
Just to keep this article simple, we will use H2 in-memory database instead of Real database but you can connect to any database for production. You just need to add the database dependency that you are using and data source properties that’s it.
Read More:
- Check the Complete JUnit 5 Tutorial
- Check the Complete JavaServer Faces (JSF) Tutorial
- Check the Spring Boot JdbcTemplate Tutorials
- Check the Complete Spring Boot and Data JPA Tutorials
- Check the Complete Spring MVC Tutorials
- Check the Complete JSP Tutorials
- Check the Complete Spring Boot Tutorials [100+ Examples]
- Check the Complete Spring Boot and Thymeleaf Tutorial
- Check the Complete AWS Tutorial
- Check the Complete JavaServer Faces (JSF) Tutorial
- Check the Complete Spring Data JPA Tutorial
- Check the Complete Spring Security Tutorial
- Check the Javascript Projects for Beginners
Table of Contents
Spring Boot File Upload and Download REST API: Complete Example
We will create this example step by step, follow this tutorial till the end
Create spring boot project
There are many different ways to create a spring boot application, you can follow the below articles to create one –
>> Create spring boot application using Spring initializer
>> Create spring boot application in Spring tool suite [STS]
>> Create spring boot application in IntelliJ IDEA
Add maven dependencies
Open pom.xml
and add the following dependencies –
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.7.3</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>in.bushansirgur</groupId>
<artifactId>image-upload-download</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>image-upload-download</name>
<description>Spring boot image upload and download project</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<excludes>
<exclude>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</exclude>
</excludes>
</configuration>
</plugin>
</plugins>
</build>
</project>
- We have added the
spring-boot-starter-web
dependency to create web application and REST APIs
- We have added the
spring-boot-starter-data-jpa
dependency to perform the database operations. - We added an
h2
dependency for storing the data in the database, it is a in-memory database. - Also we have added the
spring-boot-devtools
dependency which is for development purpose
Configure Data source
As we discussed above, we will use H2 in-memory database instead of Real database such as MySQL, PostgreSQL or Oracle. Open application.properties
file and the following content
spring.datasource.url=jdbc:h2:mem:testdb
spring.datasource.driverClassName=org.h2.Driver
spring.datasource.username=sa
spring.datasource.password=password
spring.jpa.database-platform=org.hibernate.dialect.H2Dialect
spring.h2.console.enabled=true
spring.jpa.hibernate.ddl-auto=update
Create a JPA Entity
We will create a JPA entity to map the database table with Java object. Create ProductImage.java
inside src/main/java
and add the following content
package in.bushansirgur.springbootimageupload.entity;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Lob;
import javax.persistence.Table;
@Entity
@Table(name = "tbl_product_image")
public class ProductImage {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
private String name;
private String type;
@Lob
@Column(name = "imagedata")
private byte[] imageData;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public byte[] getImageData() {
return imageData;
}
public void setImageData(byte[] imageData) {
this.imageData = imageData;
}
}
- We have added
@Entity
annotation to mark this class as JPA entity - We have added
@Table
annotation to provide the database table information.@Table
annotation indicates that this class is mapped with database tabletbl_product_image
- We have also added couple more annotataions
@Id
and@GeneratedValue
annotation. - We also added the
@Lob
annotation for the fieldimageData
which indicate that a persistent property or field should be persisted as a large object to a database-supported large object type.
Create JPA Repository for ProductImage
We will create a Interface and extend it with JpaRepository
so the we can use the JpaRepository
predefined methods to perform the database operations. Create a interface ProjectImageRepository.java
inside src/main/java
and add the following content
package in.bushansirgur.springbootimageupload.repository;
import java.util.Optional;
import org.springframework.data.jpa.repository.JpaRepository;
import in.bushansirgur.springbootimageupload.entity.ProductImage;
public interface ProductImageRepository extends JpaRepository<ProductImage, Long> {
Optional<ProductImage> findByName(String fileName);
}
We have created a JPA finder method findByName
to get the Product image from the database by its image name.
Create a Utility class to expose the Compress and Decompress image
We will create Utility class, it will expose two static methods compressImage()
and decompressImage()
. We can use these util methods to compress and decompress the image before saving and retrieving the image from the database. Create class ImageUtil.java
under src/main/java
and add the following content
package in.bushansirgur.springbootimageupload.util;
import java.io.ByteArrayOutputStream;
import java.util.zip.Deflater;
import java.util.zip.Inflater;
public class ImageUtil {
public static byte[] compressImage(byte[] data) {
Deflater deflater = new Deflater();
deflater.setLevel(Deflater.BEST_COMPRESSION);
deflater.setInput(data);
deflater.finish();
ByteArrayOutputStream outputStream = new ByteArrayOutputStream(data.length);
byte[] tmp = new byte[4*1024];
while (!deflater.finished()) {
int size = deflater.deflate(tmp);
outputStream.write(tmp, 0, size);
}
try {
outputStream.close();
} catch (Exception ignored) {
}
return outputStream.toByteArray();
}
public static byte[] decompressImage(byte[] data) {
Inflater inflater = new Inflater();
inflater.setInput(data);
ByteArrayOutputStream outputStream = new ByteArrayOutputStream(data.length);
byte[] tmp = new byte[4*1024];
try {
while (!inflater.finished()) {
int count = inflater.inflate(tmp);
outputStream.write(tmp, 0, count);
}
outputStream.close();
} catch (Exception ignored) {
}
return outputStream.toByteArray();
}
}
Create a Service class for Product Image
We will create Service class for Product image where will call the JpaRepository
methods to save and retrieve the image. While calling the repository methods we will make use of the Utility methods to compress and decompress the image. Create a class ProductImageService.java
under src/main/java
and add the following content
package in.bushansirgur.springbootimageupload.service;
import java.io.IOException;
import java.util.Optional;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;
import in.bushansirgur.springbootimageupload.entity.ProductImage;
import in.bushansirgur.springbootimageupload.repository.ProductImageRepository;
import in.bushansirgur.springbootimageupload.util.ImageUtil;
@Service
public class ProductImageService {
@Autowired
private ProductImageRepository imageRepo;
public ProductImage uploadImage(MultipartFile file) throws IOException {
ProductImage pImage = new ProductImage();
pImage.setName(file.getOriginalFilename());
pImage.setType(file.getContentType());
pImage.setImageData(ImageUtil.compressImage(file.getBytes()));
return imageRepo.save(pImage);
}
public byte[] downloadImage(String fileName){
Optional<ProductImage> imageData = imageRepo.findByName(fileName);
return ImageUtil.decompressImage(imageData.get().getImageData());
}
}
Create Controller to expose REST end points for Upload and Download Image
We will create a Controller for ProductImage
where we will expose 2 REST end points /upload
for uploading the image and /download/{fileName}
for downloading the image. Create ProductImageController.java
under src/main/java
and add the following content
package in.bushansirgur.springbootimageupload.controller;
import java.io.IOException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import in.bushansirgur.springbootimageupload.service.ProductImageService;
@RestController
public class ProductImageController {
@Autowired
private ProductImageService productImageService;
@ResponseStatus(value = HttpStatus.OK)
@PostMapping("/upload")
public void uploadImage(@RequestParam("productImage")MultipartFile file) throws IOException{
productImageService.uploadImage(file);
}
@GetMapping("/download/{fileName}")
public ResponseEntity<byte[]> downloadImage(@PathVariable String fileName) {
byte[] image = productImageService.downloadImage(fileName);
return ResponseEntity.status(HttpStatus.OK).contentType(MediaType.valueOf("image/png")).body(image);
}
}
Run the app
Run the application using the below maven command –
mvn spring-boot:run
Once the tomcat server is up, open Postman make a HTTP POST request to upload image to the URL http://localhost:8080/upload
. In order to send image, inside the HTTP body select form-data, enter productImage as Key and select image which you want to upload as Value.
When you send the request, you will see the HTTP status code 200 and you can verify in the database that a new entry has been inserted to the database.
Next send the another HTTP GET request to download the image to the URL http://localhost:8080/download/101.png
. You will see the image in the response body downloaded from the database.
Additional Resources
That’s it for this post, if you like this post, share this with your friends and colleagues or you can share this within your social media platform. Thanks, I will see you in our next post.
Getting
2023-07-14T16:31:22.591+05:30 WARN 3480 — [io-8082-exec-10] .w.s.m.s.DefaultHandlerExceptionResolver : Resolved [org.springframework.web.multipart.support.MissingServletRequestPartException: Required part ‘productImage’ is not present.]