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

前端 未结 2 1528
半阙折子戏
半阙折子戏 2020-12-05 10:57

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

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

This should be pretty easy, but

2条回答
  •  天涯浪人
    2020-12-05 11:44

    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 :

    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.

    Edit

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

提交回复
热议问题