如何在不将完整文件加载到内存的情况下将大文件插入BLOB(Oracle)? [英] how to insert a huge file to BLOB (Oracle) without loading the complete file to memory?

查看:177
本文介绍了如何在不将完整文件加载到内存的情况下将大文件插入BLOB(Oracle)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们的临时文件系统中存储了一个大文件,我需要的是使用BufferedInputStream读取那个巨大的文件(可能是几个或几个演出),如下所示

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));

这将有效减少读取文件的时间,并希望将文件保存到DB中的BLOB( Oracle)。

this will effectively decrease the time to read the file and wish to save the file to BLOB in DB (Oracle).

这是真正的问题:

我不希望文件被完全读取到我的记忆因为Iam登陆内存不足并希望读取块并将文件内容作为字节数组推送到我的DB BLOB列。

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.

基本上,我的想法是杀死将整个文件加载到内存并实现将文件保存到BLOB(可能附加到现有的BLOB内容等)的空头

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:我们使用Hibernates来保存BLOB文件

PS: We use Hibernates to save BLOB files

推荐答案

对于那些人......

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;

资料来源: http://docs.oracle.com/cd/B19306_01/java.102/b14355/oralob.htm#CHDFHHHG

这篇关于如何在不将完整文件加载到内存的情况下将大文件插入BLOB(Oracle)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆