【发布时间】:2020-12-05 10:39:17
【问题描述】:
我想在 Spring 中使用 @Lob 注释将编码图像保存到 Postgres 数据库中。测试应用程序时没有错误,我浏览图像然后保存它。但是当我打开数据库而不是图像列中的 base64 编码图像时,我只有几个数字(41417、41418 等)。
这是我课堂上的部分代码。
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)。
【问题讨论】:
标签: java spring postgresql spring-data-jpa spring-data