2013-08-06 32 views
4

嗨,我正在將圖像保存到數據庫中,我將圖像作爲多部分並嘗試轉換爲Blob類型。 我這樣做的方法:如何將多部分文件轉換爲Blob類型

Blob blob=Hibernate.getLobCreator(sessionFactory.getCurrentSession()).createBlob(multipartFile.getInputStream(),multipartFile.getSize()); 

但要Nullpointer Exception While Executing。 該文件無法將多部分轉換爲Blob,任何其他將圖像保存到數據庫的方法。

回答

3
MultipartFile savedFile; 
savedFile=itemView.getImgFile();//file from model attribute 
Blob blob=Hibernate.createBlob(savedFile.getInputStream()); // hibernate method for create blob 
//Save method will be call here 

http://viralpatel.net/blogs/tutorial-save-get-blob-object-spring-3-mvc-hibernate/ 我跟着上面的教程

+1

我得到這個錯誤'的方法createBlob(InputStream的)是未定義的類型Hibernate'時我使用'createBlob()'方法,因爲它已被棄用,並且在Hibernate版本4中。 – Lucky

1

您可以使用此方法。 獲取多數據並將其轉換成字節arrray,然後轉換成團塊

for (MultipartFile file : productsBean.getData()) { 
    byte[] bytes = file.getBytes(); 
    Blob blob = new javax.sql.rowset.serial.SerialBlob(bytes); 
    } 

它的工作對我來說:)

相關問題