简体   繁体   中英

how to convert Hibernate Blob back to original File

I am using hibernate to store Zipped files in the database, I have created the Blob file with Hibernate.createBlob(input stream)

Now i want to reverse the process and convert the Blob to File so i can open it with Zip4j How exactly am i supposed to do that? i tried to do this but i always get an exception

  File file = new File("origfile.zip"); try { InputStream is = blob.getBinaryStream(); <- exception FileOutputStream opt = new FileOutputStream(file); int read = 0; byte[] bytes = new byte[1024]; while ((read = is.read(bytes)) != -1) { opt.write(bytes, 0, read); } 

and the exception is:

 2013-07-03 18:38:35,482 [DEBUG] [HibernateTransactionManager,doBegin(),569] - Exposing Hibernate transaction as JDBC transaction [com.mchange.v2.c3p0.impl.NewProxyConnection@69875a82] java.sql.SQLException: could not reset reader at org.hibernate.lob.BlobImpl.getBinaryStream(BlobImpl.java:106) at org.hibernate.lob.SerializableBlob.getBinaryStream(SerializableBlob.java:62) at com.gleeb.backbonetemplates.services.LivePreviewService.prepareDesignForLivePreview(LivePreviewService.java:35) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) 

Thanks.

You could follow this example solution in c# the code should be relatively similar here

I know it's not and ideal answer but it's a good starting point.

I have found a solution to my problem. The way i used to convert the blob to file is OK. the exception happens because of hibernate self issues you might call it.

To prevent the exception from happening i needed to do a refresh() after the save.

session.refresh(blog);

Thats it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM