How to convert multipartfile to blob type

Hi, I am working with saving the image in the database, I accept the image as multi-page and try to convert it to Blob type. I am doing this method:
 Blob blob=Hibernate.getLobCreator(sessionFactory.getCurrentSession()).createBlob(multipartFile.getInputStream(),multipartFile.getSize()); 

But we get a Nullpointer Exception While Executing . file It is not possible to convert multipart to Blob , any other method to save the image in the database.

+4
source share
2 answers
 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/ I followed the above tutorial

+3
source

You can use this approach. Get multi-part data and convert it to a byte arrangement, and then convert to Blob

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

This works for me :)

+1
source

Source: https://habr.com/ru/post/1495459/


All Articles