Store Image As Blob And Retrieve Blob As Image Using Spring

Image As Blob And Blob As Image

Here in this example I am going to show you how to store image as blob into database. While retrieving blob from database I am going to display blob as image. While storing image as blob into database I am going to display a page which will be used for uploading the image. I am also going to show you how to download blob as an image from database. You can also delete the image you don’t want to keep.

In this example I am going to use spring data jpa to save image as blob into database or retrieve blob as image. I am using Thymeleaf framework for front-end or user interface (ui) pages.


Java 19, Maven 3.8.5, Spring Boot 3.2.0, MySQL 8.1.0

Project Setup

You can use the following pom.xml file for your project reference:

<?xml version="1.0" encoding="UTF-8"?>

<project xmlns=""









Data Source

The following content is written into src/main/resources/ file for data source configuration purpose.

As I have configured the data source details with standard naming convention so I don’t need to create a bean for data source. An instance of the data source will be created by spring container automatically.


Entity Class

The entity class is mapped with the corresponding database table.

I have the same Java attribute names and column names except the one date. So I don’t need to specify the class attributes with @Column annotations.

Notice in the following class the blob type of column in database table has been annotated with @Lob annotation and the data type has been specified as array of byte.

package com.roytuts.spring.image.blob.entity;

@Table(name = "images")
public class Image implements Serializable {

	private static final long serialVersionUID = 1L;

	@GeneratedValue(strategy = GenerationType.IDENTITY)
	private Integer id;

	private String name;

	private byte[] content;

	@Column(name = "save_date")
	private String date;



I am here using spring data jpa repository so I won’t write any query and I will use the built-in methods to query the database table.

package com.roytuts.spring.image.blob.repository;

public interface ImageRepository extends JpaRepository<Image, Integer>{


Service Class

The service class is responsible for performing business logic.

package com.roytuts.spring.image.blob.service;

public class ImageService {

	private ImageRepository imageRepository;

	public void saveImage(Image image) {;

	public void deleteImage(Integer id) {

	public Image getImage(Integer id) {
		return imageRepository.findById(id).get();

	public List<Image> imageList() {
		return imageRepository.findAll();


Controller Class

The controller class is responsible for handling requests/responses for clients or end users. This controller class also validates input fields. The controller class defines methods for uploading, displaying and downloading images.

In the following controller class I have validated the input file whether it is an image or not.

The downloaded image is saved into the default location or it will ask where to save the image.

package com.roytuts.spring.image.blob.controller;

public class ImageController {

	private ImageService imageService;

	public String imageList(ModelMap modelMap) {
		List<Image> images = imageService.imageList();

		modelMap.addAttribute("imageList", images);

		return "images";

	public String uploadImage() {
		return "upload";

	public String uploadImage(@RequestParam("image") MultipartFile image, ModelMap modelMap) throws IOException {
		String imageName = image.getOriginalFilename();

		String ext = Objects.requireNonNull(imageName).substring(imageName.lastIndexOf('.') + 1).toLowerCase();

		if (ext.equals("jpeg") || ext.equals("jpe") || ext.equals("jpg") || ext.equals("png") || ext.equals("gif")) {
			byte[] content = image.getBytes();

			DateTimeFormatter dtf = DateTimeFormatter.ofPattern("dd-MM-yyyy hh:mm:ss");

			Image img = new Image();


			modelMap.addAttribute("msg", "Image '" + imageName + "' Successfully Uploaded");
		} else {
			modelMap.addAttribute("msg", "Image '" + imageName + "' must be an image file (jpeg/png/gif)");

		return "upload";

	public ResponseEntity<Resource> downloadImages(@PathVariable int id) {
		Image img = imageService.getImage(id);

		ByteArrayResource resource = new ByteArrayResource(img.getContent());

		return ResponseEntity.ok().contentType(MediaType.valueOf(img.getMimeType()))
				.header(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=\"" + img.getName() + "\"")

	public String deleteImage(@PathVariable int id, ModelMap modelMap) {

		List<Image> images = imageService.imageList();

		modelMap.addAttribute("imageList", images);
		modelMap.addAttribute("msg", "Image deleted successfully");

		return "redirect:/";


Spring Boot Main Class

A class with main method and @SpringBootApplication annotation is required to deploy the application into the embedded Tomcat server.

package com.roytuts.spring.image.blob;

public class App {

	public static void main(String[] args) {, args);


User Interface Pages

There are mainly two pages – one for uploading image and another for displaying, downloading and deleting images.

The content of the pages is written using Thymeleaf framework, so I am using HTML pages for building UI pages.


The following page images.html will provide links for downloading and deleting image(s).

<html xmlns:th="">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Display Image Files</title>
	<p><a th:href="@{'/uploadImage'}" title="Upload A New Image">Upload An Image</a></p>

	<div th:if="${msg != null}" th:text="${msg}"></div>
	<div th:if="${imageList.size() > 0}">
		<h2>List of Images</h2>
		<table cellpadding="5" cellspacing="5" border="1">
				<tr th:each="img : ${imageList}">
					<td><img th:src="@{'data:${img.getMimeType()};base64,'+${img.generateBase64Image()}}" alt="picture" height="200" width="250"></td>
						<a th:href="@{'/download/image/' + ${}}" title="Download">Download</a>
						<a th:href="@{'/delete/image/' + ${}}" onclick="return confirm('Do you really want to delete?')" title="Delete">Delete</a>


The upload.html page is required to upload an image by selecting it using the browse button from any disk space or any other place.

<html xmlns:th="">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Upload Image File</title>
	<p><a th:href="@{'/'}" title="Images">List Of Images</a></p>
    <h2>Upload An Image File</h2>

	<div th:if="${msg != null}" th:text="${msg}"></div>

	<form enctype="multipart/form-data"	th:action="@{/upload/image}" method='POST'>
		<p>Select Image: <input type="file" name="image"/></p>
		<p><input value="Upload" type="submit"/></p>

That’s all about coding part. Now let’s test the application.

Testing Image Application

You can now run the main class and once the application deployed successfully in Tomcat server you can access the URL http://localhost:8080/ in the browser and you will find the following page for uploading an image.

spring image blob

Initially you won’t find any image displayed on the page as there is no uploaded image.

Now you can click on the Upload An Image link to upload an image using the following page:

spring blob image

You can upload as many images as you wish, here I have uploaded two images and those are displayed as follows:

image blob spring

You can either download or delete the image from the list of images. When you try to delete an image you will need to confirm before deletion to prevent the image from accidentally deleted:

image blob database

Hope you got an idea how to store an image into database as a blob type and how to retrieve blob type data as an image from database.

Source Code


Leave a Reply

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