首页 文章

在Java中将非常大的Blob对象(大约3GB)转换为bytearray

提问于
浏览
3

我已经阅读了几个网站,这些网站显示了将 java.sql.Blob 对象转换为 byte[] 并最终保存为文件的示例代码 .

例如easiest-way-to-convert-a-blob-into-a-byte-arrayimage-to-bytearray-to-blob-and-blob-to-bytearray-to-image-conversion-issues-in-j

大多数建议使用 blob.getBytes(pos, length)byte[] blobBytes = blob.getBytes(1, (int) blob.length());

但是,如果blob的大小超过整数的最大值(例如3 GB),则 blobBytes 对象将被截断,并且创建的文件将格式错误

无论如何要克服它的大小限制吗?

还是我必须使用 blob.getBinaryStream(1, blob.length()) 来获取InputStream并进一步处理它到byte []?

2 回答

  • 0

    最简单的方法是使用Apache IOUtils(您需要将其包含在项目中)并将流复制到磁盘 .

    InputStream blobInputStream = blob.getBinaryStream();
    OutputStream fileOutputStream = new FileOutputStream(new File("/path/to/file.out"));
    IOUtils.copyLarge(blobInputStream, fileOutputStream);
    

    copyLarge(InputStream input,OutputStream output)将字节从大(超过2GB)的InputStream复制到OutputStream .

    编辑:您也可以使用blob.getBytes,但是您需要在循环中一次拉出合理数量的字节(例如8192),然后写入FileOutputStream .

  • 1

    Oracle提供了一种从 oracle.sql.Blob 获取inputstram的方法:

    见:

    Reading and Writing BLOB and CLOB Data

相关问题