如何将multipartfile转换为Blob类型

时间:2013-08-06 07:03:05

标签: hibernate

您好我正在将图像保存到数据库中,我将图像作为多部分并尝试转换为Blob类型。 我这样做的方法:

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

但获得Nullpointer Exception While Executing。 该文件无法将多部分转换为Blob,以及将图像保存到数据库中的任何其他方法。

2 个答案:

答案 0 :(得分: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 :(得分:1)

您可以使用此方法。 获取多部分数据并将其转换为字节arrray,然后转换为Blob

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

这对我有用:)