从 Java 中 blob 的内容创建文件的代码段

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1076972/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 23:15:02  来源:igfitidea点击:

Snippet to create a file from the contents of a blob in Java

javajdbcblob

提问by OscarRyz

I have some files stored in a database blob column in Oracle 9.

我有一些文件存储在 Oracle 9 的数据库 blob 列中。

I would like to have those files stored in the file system.

我想将这些文件存储在文件系统中。

This should be pretty easy, but I don't find the right snipped.

这应该很容易,但我没有找到正确的剪断。

How can I do this in java?

我怎么能在java中做到这一点?

 PreparedStatement ptmst = ...
 ResutlSet rs = pstmt.executeQuery();
 rs.getBlob();
 // mistery 
 FileOutputStream out = new FileOutputStream();
 out.write(); // etc et c

I know it should be something like that... what I don't know is what is commented as mistery

我知道它应该是这样的......我不知道被评论为神秘的东西

Thanks

谢谢

EDIT

编辑

I finally got this derived from David's question.

我终于从大卫的问题中得到了这个。

This is my lazy implementation:

这是我的懒惰实现:

PreparedStatement pstmt = connection.prepareStatement("select BINARY from MYTABLE");
ResultSet rs = pstmt.executeQuery();
while( rs.next() ) {
    Blob blob = rs.getBlob("BINARY");
    System.out.println("Read "+ blob.length() + " bytes ");
    byte [] array = blob.getBytes( 1, ( int ) blob.length() );
    File file = File.createTempFile("something-", ".binary", new File("."));
    FileOutputStream out = new FileOutputStream( file );
    out.write( array );
    out.close();
}

采纳答案by David Winslow

You'd want to get the blob as an inputstream and dump its contents to the outputstream. So 'misery' should be something like:

您希望将 blob 作为输入流并将其内容转储到输出流。所以“痛苦”应该是这样的:

Blob blob = rs.getBlob(column);
InputStream in = blob.getBinaryStream();
OutputStream out = new FileOutputStream(someFile);
byte[] buff = new byte[4096];  // how much of the blob to read/write at a time
int len = 0;

while ((len = in.read(buff)) != -1) {
    out.write(buff, 0, len);
}

If you find yourself doing a lot of IO work like this, you might look into using Apache Commons IOto take care of the details. Then everything after setting up the streams would just be:

如果您发现自己做了很多这样的 IO 工作,您可能会考虑使用Apache Commons IO来处理细节。然后设置流后的一切都将是:

IOUtils.copy(in, out);

回答by GiantSteps

There is another way of doing the same operation faster. Actually the answer above works fine but like IOUtils.copy(in,out)it takes a lot of time for big documents. The reason is you are trying to write your blob by 4KB iteration. Simplier solution :

还有另一种方法可以更快地执行相同的操作。实际上,上面的答案效果很好,但是IOUtils.copy(in,out)对于大文档来说需要花费很多时间。原因是您试图通过 4KB 迭代来编写 blob。更简单的解决方案:

Blob blob = rs.getBlob(column);
InputStream in = blob.getBinaryStream();
OutputStream out = new FileOutputStream(someFile);
byte[] buff = blob.getBytes(1,(int)blob.getLength());
out.write(buff);
out.close();

Your outputStream will write the blob in one shot.

您的 outputStream 将一次性写入 blob。

Edit

编辑

Sorry didn't see the Edit section on the intial Post.

抱歉没有看到初始帖子的编辑部分。