how to insert a huge file to BLOB (Oracle) without loading the complete file to memory?

淺唱寂寞╮ 提交于 2019-12-05 20:16:39

For the ones out there...

Here is the process to get it done:

stmt.execute ("INSERT INTO my_blob_table VALUES ('row1', empty_blob())");
BLOB blob;
cmd = "SELECT * FROM my_blob_table WHERE X='row1' FOR UPDATE";
ResultSet rset = stmt.executeQuery(cmd);
rset.next();
BLOB blob = ((OracleResultSet)rset).getBLOB(2);
File binaryFile = new File("john.gif");
System.out.println("john.gif length = " + binaryFile.length());
FileInputStream instream = new FileInputStream(binaryFile);
OutputStream outstream = blob.setBinaryStream(1L);
int size = blob.getBufferSize();
byte[] buffer = new byte[size];
int length = -1;

Source: http://docs.oracle.com/cd/B19306_01/java.102/b14355/oralob.htm#CHDFHHHG

Is not using Hibernate an option ?

You can use UTL_FILE to read from a file

http://docs.oracle.com/cd/B19306_01/appdev.102/b14258/u_file.htm#i1003849

And DBMS_LOB to write to a LOB

http://docs.oracle.com/cd/B19306_01/appdev.102/b14258/d_lob.htm#i999705

By reading/writing in chunks you can avoid having the whole blob in memory at any one time.

For UTL_FILE to work, the database does need to be able to read the file from the filesystem (ie the filesystem containing the file must be accessible to the database server). You can also read from an HTTP stream or TCP data stream if the file is available for HTTP/FTP etc.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!