尝试将图像base64保存到spring boot数据库中的byte []中

发布于 2025-01-10 04:00:59 字数 1253 浏览 0 评论 0原文

我使用的 https://codebeautify.org/image-to-base64-converter 网站将图像转换为 base64 https://unsplash.com/photos/KaK2jp8ie8s 我正在使用图像,

因此请正确使用图像和网站多于。我正在将此图像转换为 base64,并尝试将其保存到我的 spring boot h2 数据库中。它不会让我说它不是一个 byte[]。我怎样才能做到呢?


import lombok.Data;

import javax.persistence.*;
import java.io.Serializable;

@Entity
@Data
public class Fromage implements Serializable {

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

    private String nom;
    private double prix;
    private int vieillese;
    private int quantite;

    @Lob
    private byte[] data;

    public Fromage(){}

    public Fromage(String nom, double prix, int vieillese, int quantite, byte[] data) {
        this.nom = nom;
        this.prix = prix;
        this.vieillese = vieillese;
        this.quantite = quantite;
        this.data = data;
    }
}
        byte[] bob = {1,2,3};
        Fromage fromage = new Fromage("Chevre",50.15,10,1,bob);
        fromageRepository.save(fromage);

尝试执行此操作,但将 bob 替换为图像 base64 字符串

https://codebeautify.org/image-to-base64-converter website I use to convert image to base64
https://unsplash.com/photos/KaK2jp8ie8s image I am using

so using the image and website right above. I am converting this image into base64 and I am trying to save it into my spring boot h2 database. It won't let me saying it is not a byte[]. How could I do it?


import lombok.Data;

import javax.persistence.*;
import java.io.Serializable;

@Entity
@Data
public class Fromage implements Serializable {

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

    private String nom;
    private double prix;
    private int vieillese;
    private int quantite;

    @Lob
    private byte[] data;

    public Fromage(){}

    public Fromage(String nom, double prix, int vieillese, int quantite, byte[] data) {
        this.nom = nom;
        this.prix = prix;
        this.vieillese = vieillese;
        this.quantite = quantite;
        this.data = data;
    }
}
        byte[] bob = {1,2,3};
        Fromage fromage = new Fromage("Chevre",50.15,10,1,bob);
        fromageRepository.save(fromage);

trying to do this but replace bob with image base64 string

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文