[英]How to display @Lob image from mysql database using spring mvc and jsp
[英]How to save @Lob data from spring in to a Postgres database?
我想使用 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;
這是我用來將 object 保存到數據庫中的 function。
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)。
您的@Lob
確實保存正確。 您看到的值 ( 41417
) 表示其 OID。
要查看大 object 的內容,可以使用 Postgres 的lo_get
function(假設您使用的是 9.4+ 版本):
SELECT lo_get(cast(image_base64 as bigint)) FROM products WHERE id = 1;
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.