如何将java.io.InputStream转换为java.sql.Blob [英] How to convert java.io.InputStream to java.sql.Blob

查看:40
本文介绍了如何将java.io.InputStream转换为java.sql.Blob的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何使用纯Java将 java.io.InputStream 转换为 java.sql.Blob ?

How do I convert a java.io.InputStream to a java.sql.Blob using pure Java?

为响应tbodt的建议,我通过Eclipse调试器进行了以下测试.调试器显示 myinputstream 具有内容,但是 blob 在代码末尾仍为 null .我该怎么做才能解决此问题?

In response to tbodt's suggestions, I ran the following through the eclipse debugger. The debugger shows that myinputstream has content, but that blob remains null at the end of the code. What do I need to do to fix this?

byte[] contents;
ByteArrayOutputStream output = new ByteArrayOutputStream();
byte[] buffer = new byte[1024];
int count;
while ((count = myinputstream.read(buffer)) != -1){output.write(buffer, 0, count);}//debugger says myinputstream has blksize 16384, buffcount 12742, and max 127394 here
contents = output.toByteArray();
Blob blob = null;
try {blob = new SerialBlob(contents);} 
catch (SerialException e) {e.printStackTrace();}
catch (SQLException e) {e.printStackTrace();}

someObject.setSomeBlobProperty(blob);//debugger says blob is null here

我还通过调试器运行了 IOUtils 方法,但是得到了完全相同的结果,其中 null blob 但填充了 myinputstream .我该如何解决?

I also ran the IOUtils approach through the debugger, but got the exact same results, with a null blob but a populated myinputstream. How can I fix this?

Blob blob = null;
try {
    blob = new SerialBlob(IOUtils.toByteArray(myinputstream));//debugger says myinputstream has contents here.
    someObject.setSomeBlobProperty(blob);//debugger says blob is null here
} 
catch (SerialException e) {e.printStackTrace();}
catch (SQLException e) {e.printStackTrace();}

在两种情况下,调试器都说 myinputstream 在以下位置具有 blksize 16384, buffcount 12742和 max 127394指定的位置,尽管 blob 仍然为 null .运行此代码后,我还检查了基础MySQL数据库,并确认blob字段为空.

In both cases, the debugger says myinputstream has blksize 16384, buffcount 12742, and max 127394 at the indicated locations, despite blob remaining null. I also checked the underlying MySQL database after running this code and confirmed that the blob field is empty.

然后,我通过Eclipse调试器运行了以下命令,该调试器显示了尝试填充它的 byte [] (称为 content )仍然为空.因此,生成的 blob 为空,而 inputstream 确实确实继续具有与上面所示相同的内容值:

I then ran the following through the Eclipse debugger, which showed that the byte[] called content remained empty after the attempts to populate it. So the resulting blob is empty, while the inputstream does indeed continue to have the same content values as shown above:

Blob blob = null;
byte[] content = IOUtils.toByteArray(myinputstream);
try {
    blob = new SerialBlob(content);//debugger says content is empty here
    someObject.setSomeBlobProperty(blob);//debugger says blob is empty here.
}//debugger says myinputstream has the same values as in edit#1
catch (SerialException e) {e.printStackTrace();}
catch (SQLException e) {e.printStackTrace();}

推荐答案

您可以使用相同的内容:

You can use the same:

Blob xmlForSign=null;
xmlForSign.getBinaryStream();

此方法getBinaryStream()返回Inputfilestream.

this method getBinaryStream() return an Inputfilestream.

这篇关于如何将java.io.InputStream转换为java.sql.Blob的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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