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

Convert very large Blob object (about 3GB) into bytearray in Java

我浏览了几个显示将 java.sql.Blob 对象转换为 byte[] 对象并最终保存为文件的示例代码的网站。

例如 easiest-way-to-convert-a-blob-into-a-byte-array 要么

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

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

有什么方法可以克服它的大小限制吗?

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

最简单的方法是使用 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) Copies bytes from a large (over 2GB) InputStream to an OutputStream.

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

Oracle 提供了一种从 oracle.sql.Blob :

获取输入流的方法

参见:

Reading and Writing BLOB and CLOB Data