将java.io.inputstream转换为java.sql.blob [英] convert java.io.inputstream to java.sql.blob

查看:146
本文介绍了将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.

编辑#2

然后我通过eclipse调试器运行以下内容,这表明在尝试填充它之后, byte [] 名为 content 仍为空。因此,结果 blob 为空,而输入流确实继续具有与编辑#中所示相同的内容值上面的1:

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 in edit#1 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();}


推荐答案

你可以使用相同的:

Blob xmlForSign=null;
xmlForSign.getBinaryStream();

此方法getBinaryStream()返回一个Inputfilestream。

this method getBinaryStream() return an Inputfilestream.

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

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