How to upload image using Spring Boot?

Asked

Viewed 1,908 times

2

I’m learning to use the Spring Boot and I need to create a field in the form that loads the image to be used. How do I map the directory and save to a particular project folder?

Example: through the form, the user will upload the photo. Instead of saving the image in the database, how do I save only the directory that will load the image in the view? Adding, and if possible, how to load this image by the control class?

Please, Before you go negative on my question, I’ve seen some answers, so far on stackoverflow, but I’m still having doubts about how to proceed within the domain classes. Example of domain class:

Observing: am using MySQL.

package com.ptestes.models;

import java.io.Serializable;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;

@Entity
public class HotSite implements Serializable{

    private static final long serialVersionUID = 1L;

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

    private String pageTitle;

    @?????
    private <?????> img;

    /*Getters and Setters*/
}

From now on, thank you very much! ^_^

  • 1

    I believe that that can help you.

  • The article is cool, but it teaches you to save directly in the BD. I would like to save the directory and upload the image using the controller.

3 answers

1

I believe that it is not ideal to record the bitmap of an image in the database. Below is a code snippet where I upload an image to the Amazon server (S3), in the file name I use a predefined prefix and the object ID, so when I want to fetch the image I make a request to the server seeking this prefix + ID. I hope it helps you get an idea.

public URI uploadProfilePicture(MultipartFile multipartFile) {
    UserSS user = UserService.authenticated();
    if (user == null)
        throw new AuthorizationException("Acesso negado!");

    BufferedImage jpgImage = imageService.getJpgImageFromFile(multipartFile);
    jpgImage = imageService.cropSquare(jpgImage);
    jpgImage = imageService.resize(jpgImage, size);

    String fileName = prefix + user.getId() + ".jpg";

    return s3Service.uploadFile(imageService.getInputStream(jpgImage, "jpg"), fileName, "image");
}
  • Thank you! I’ll test!

0


I received the answer through the same question in another forum. If you have the same question and want to read the full discussion, read here.

0

    @Lob   //representa um campo do tipo blob no bd
    private byte[] img;  //armazena arquivo como um byte array
  • Hello, Lucas! Thank you so much for answering! Maybe I misexpressed my question, I will correct. But reading around, and I may be wrong, which is likely, I saw some people saying that uploading images directly into the database can make you heavy. So I wanted to know how to map the URL or directory of a folder within the project?

  • 1

    I who read wrong even rs, actually I was curious too, honestly I always saved in the comics my files, but I believe that to save the way also have no secret,in my humble opinion the field would be a string, you could save the path in the directory of your choice using Files.Move() and saving the directory, and to recover the file you could simply read with buffered image, eg: Bufferedimage img = Imageio.read(img);

  • I’ll try! _ Thank you!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.