java - 如何将 spring 中的@Lob 数据保存到 Postgres 数据库中?

标签 java spring postgresql spring-data-jpa spring-data

我想使用 Spring 中的 @Lob 注释将编码图像保存到 Postgres 数据库中。测试应用程序时没有错误,我浏览图像然后保存它。但是当我打开数据库而不是图像列中的 base64 编码图像时,我只有几个数字(41417、41418 等等)。
这是我类(class)代码的一部分。

public class Product {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;

@NotNull
private String name;

@Lob
private String imageBase64;
这是我用来将对象保存到数据库中的函数。
public Product saveProduct(Product product, MultipartFile image) throws IOException {
    if (image != null && !image.getName().isEmpty()) {
        byte[] bytes = image.getBytes();
        String base64Image = String.format("data:%s;base64,%s", image.getContentType(), Base64.getEncoder().encodeToString(bytes));
        product.setImageBase64(base64Image);
    }
    return this.productRepository.save(product);
}
但是当它保存到数据库中时,它看起来像这样(列 image_base64)。
enter image description here

最佳答案

您的 @Lob 做了 正确保存。您看到的值 ( 41417 ) 表示其 OID。
要查看大对象的内容,可以使用 Postgres 的 lo_get功能(假设您使用的是 9.4+ 版本):

SELECT lo_get(cast(image_base64 as bigint)) FROM products WHERE id = 1;

关于java - 如何将 spring 中的@Lob 数据保存到 Postgres 数据库中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63431441/

相关文章:

java - 如何将图像放到 Jbutton 上

java - 使用 thymeleaf 将参数传递给 spring Controller

regex - Postgres 正则表达式问题

c# - Entity Framework 核心 1.0 PostgreSQL API

java - 如何访问 Spring Bean 名称?

java - 使用 Java String.format

spring - 如何在Spring Controller中定向到不同的JSP页面

postgresql - 使用 Google Cloud SQL 重置 pg_stat_statements

java - Java中的多线程和同步

java - Spring - 在 JPA 事务之后提交 JMS 事务