2013-08-06 36 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); 
    } 

它的工作对我来说:)

相关问题