Saving Images Using Spring Boot and MySQL

17,715

Solution 1

After researching a lot, I have come to a solution. I have used Amazon AWS S3 for storing the media data. Amazon AWS S3 comes with its own library. It gives you rich api to upload the media files and having control over them. I have just uploaded the file into the S3 Bucket and saved the corresponding URL into the MYSQL database. Please comment if you need the corresponding codes.

Solution 2

I have encountered this problem in Django application. What i did was just used amazon S3 bucket and store all the images. I get the location of the image which is in s3 and store it in mysql DB. when i need the image i just call the image from s3 and used it.

Solution 3

  • Accept Images as a zipstream(Faster)/binarystream or as a MultipartFile
  • Load the stream ZipInputStream zis = new ZipInputStream(inputStream);
  • load the stream to File using Java.IO.File class
  • Push the file data to file system(Same server machine or any non-sql Database)
  • save the Location in a DB using Spring data JPA

Solution 4

use s3 bucket for storing image and my sql for storing the address of the image

Solution 5

This is how I DID it.
I store my images as a LONGBLOB in my MySQL database so use columnDefinition = "LONGBLOB". In your HTML code for your upload button, make sure you include the multiple = "true" in your tag if you are using HTML.

In your Controller class make a multipart file that will get the images if you are using binding models and also, you need to have a Set/List of images so you can go through them all.

MultipartFile[] images = bindingModel.getImages();
Set<Image> newImages = new HashSet<>();

I have this neat for loop so I can go through all of my images:

    for (MultipartFile multipartFileFOREACH : images)
        {
            Image newImage = new Image(multipartFileFOREACH.getBytes(), postEntity);//Image is an Entity here, postEntity is an object
            newImages.add(newImage);
            this.imageRepository.saveAndFlush(newImage);
        }
        this.articleRepository.saveAndFlush(articleEntity);

So, here I store my bytes into the LONGBLOB in the SQL database

Then for the actual view

    Set<Image> imagesBytes = post.getImages();
    List<String> images = new ArrayList<>();
    for (Image image : imagesBytes)
    {
        images.add(Base64.getEncoder().encodeToString(image.getLink()));
    }

My Image Entity has a link variable of type byte[]

Then I simply add the view for the HTML:

model.addAttribute("images", images);
Share:
17,715
Purushotam Kumar
Author by

Purushotam Kumar

Software Engineer with a rich history of development in product based industry. Having a skill set of Flutter, Dart, Java, Kotlin, Typescript, AWS.

Updated on June 29, 2022

Comments

  • Purushotam Kumar
    Purushotam Kumar almost 2 years

    I am creating a web app using Spring Boot and JPA. I want to upload many images. I want those images to save in a storage and save the location of the file into database. I am not understanding how to achieve this?

  • Purushotam Kumar
    Purushotam Kumar over 6 years
    What will be the location of the storage where the file will be saved? And How Can I fetch this location to save inside the database?
  • karthi
    karthi over 6 years
    1. Just define a destination-folder property in your application.properties. 2. Method Files.copy needs a destination-path. For this you can use this destination-folder/fileName . 3. This you can save in your local variable and update this in the database.
  • user3444718
    user3444718 almost 6 years
    Yes that will help. Also how your rest controller handled data. e.g if we want to pass json payload and file together in one request to controller.