Welcome to the world of “Spring Boot File Upload and Download with database.” 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 with database, 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 with database” 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 with database” 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
In this article we will cover the second way where we will store the direct image into the file system and store the file system path into the database. In the previous article, we already discussed the first 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 with database: 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.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
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;
private String imagePath;
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 String getImagePath() {
return imagePath;
}
public void setImagePath(String imagePath) {
this.imagePath = imagePath;
}
}
- 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 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 store the file into the file system and store the file path into the database. Similarly while retrieving the image from the database we will read that file into byte[] and return it. Create a class ProductImageService.java
under src/main/java
and add the following content
package in.bushansirgur.springbootimageupload.service;
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
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;
@Service
public class ProductImageService {
@Autowired
private ProductImageRepository imageRepo;
private final String PATH = "C:\\workspace\\";
public ProductImage uploadImage(MultipartFile file) throws IOException {
String fullPath = PATH+file.getOriginalFilename();
ProductImage pImage = new ProductImage();
pImage.setName(file.getOriginalFilename());
pImage.setType(file.getContentType());
pImage.setImagePath(fullPath);
file.transferTo(new File(fullPath));
return imageRepo.save(pImage);
}
public byte[] downloadImage(String fileName) throws IOException{
Optional<ProductImage> imageObject = imageRepo.findByName(fileName);
String fullPath = imageObject.get().getImagePath();
return Files.readAllBytes(new File(fullPath).toPath());
}
}
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) throws IOException {
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.
Conclusion
In conclusion, mastering “Spring Boot File Upload and Download with database” is a significant step toward building modern, user-centric applications. Throughout this comprehensive guide, we’ve explored the intricacies of seamlessly integrating file management with database systems in Spring Boot. You’ve learned best practices for uploading, storing, retrieving, and securely delivering files, all while ensuring data integrity and a stellar user experience.
With this newfound knowledge, you’re well-equipped to tackle the challenges of handling files within your Spring Boot projects. Whether you’re building content-rich websites, e-commerce platforms, or any application that involves file management, you now have the tools and insights to create efficient and scalable solutions.
As you continue to develop your Spring Boot applications, remember that effective file management is not only a technical accomplishment but also a way to enhance user satisfaction and streamline data workflows. So, go ahead and implement the techniques and strategies you’ve learned here to elevate your Spring Boot projects and delight your users with seamless file uploads and downloads, all backed by a robust 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.