Reputation: 541
We have a large file stored on our temporary file system, what I would be needing is to read that huge file (probably couple or few gigs) using BufferedInputStream as below
InputStream is = is = new BufferedInputStream(new FileInputStream(file));
this will effectively decrease the time to read the file and wish to save the file to BLOB in DB (Oracle).
Here is the real problem:
I do not want the file to be completely read to my memory as Iam landing into Out of memory and want to read the chunks and push the file content as byte array to my DB BLOB column.
Basically, My idea was to kill the over head of loading the complete file to memory and to achieve saving the file to BLOB (May be appending to existing BLOB content or so)
Is there any ways to get this achieved ?
PS: We use Hibernates to save BLOB files
Upvotes: 4
Views: 5936
Reputation: 541
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
Upvotes: 3
Reputation: 35401
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.
Upvotes: 1