[英]How to convert java.io.InputStream to java.sql.Blob
How do I convert a java.io.InputStream
to a java.sql.Blob
using pure Java?如何使用纯 Java 将java.io.InputStream
转换为java.sql.Blob
?
In response to tbodt's suggestions, I ran the following through the eclipse debugger.为了响应 tbodt 的建议,我通过 eclipse 调试器运行了以下内容。 The debugger shows that myinputstream
has content, but that blob
remains null
at the end of the code.调试器显示myinputstream
有内容,但该blob
在代码末尾保持为null
。 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
I also ran the IOUtils
approach through the debugger, but got the exact same results, with a null
blob
but a populated myinputstream
.我还通过调试器运行了IOUtils
方法,但得到了完全相同的结果,一个null
blob
但一个填充的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();}
In both cases, the debugger says myinputstream
has blksize
16384, buffcount
12742, and max
127394 at the indicated locations, despite blob
remaining null
.在这两种情况下,调试器都说myinputstream
在指定位置具有blksize
16384、 buffcount
12742 和max
127394,尽管blob
buffcount
null
。 I also checked the underlying MySQL database after running this code and confirmed that the blob field is empty.运行此代码后,我还检查了底层的 MySQL 数据库,并确认 blob 字段为空。
I then ran the following through the Eclipse debugger, which showed that the byte[]
called content
remained empty after the attempts to populate it.然后我通过 Eclipse 调试器运行以下命令,这表明在尝试填充content
后,称为content
的byte[]
仍然为空。 So the resulting blob
is empty, while the inputstream
does indeed continue to have the same content values as shown above:因此生成的blob
是空的,而输入inputstream
确实继续具有与上图相同的内容值:
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();}
The easy way:简单的方法:
contents = IOUtils.toByteArray(in);
But for that, you need Apache Commons IO, which might be hard to add.但是为此,您需要 Apache Commons IO,这可能很难添加。 If you don't want to use it, or can't, here's one way of doing it yourself;如果你不想使用它,或者不能使用它,这里有一种自己做的方法;
ByteArrayOutputStream output = new ByteArrayOutputStream();
byte[] buffer = new byte[1024];
int count;
while ((count = input.read(buffer)) != -1)
output.write(buffer, 0, count);
contents = output.toByteArray();
You can use the same:您可以使用相同的:
Blob xmlForSign=null;
xmlForSign.getBinaryStream();
this method getBinaryStream() return an Inputfilestream.此方法 getBinaryStream() 返回一个 Inputfilestream。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.