How to upload image using Spring Boot?

2

I'm learning to use 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 load the photo. Instead of saving the image to the database, how do I save only the directory that will load the image into view ? Adding, and if possible, how to load this image by the control class?

Please, Before leaving negative for my question , I saw some answers, so far in stackoverflow , but I still have doubts about how to proceed within the domain classes. Domain class example:

Note: I'm 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*/
}

Thank you in advance! ^ _ ^

    
asked by anonymous 23.10.2018 / 04:30

3 answers

0

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

    
25.10.2018 / 01:22
1

I believe it is not ideal to write 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 pre-defined prefix and the object ID, so when I want to fetch the image I make one request to the server for this prefix + ID. I hope this helps you to 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");
}
    
08.11.2018 / 17:46
0
    @Lob   //representa um campo do tipo blob no bd
    private byte[] img;  //armazena arquivo como um byte array
    
23.10.2018 / 14:57