将 java.io.inputstream 转换为 java.sql.blob
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/27934293/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
convert java.io.inputstream to java.sql.blob
提问by CodeMed
How do I convert a java.io.inputstream
to a java.sql.blob
using pure java?
如何使用纯 Java将 a 转换java.io.inputstream
为 a java.sql.blob
?
EDIT:
编辑:
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?
为了响应 tbodt 的建议,我通过 eclipse 调试器运行了以下内容。调试器显示myinputstream
有内容,但blob
保留null
在代码的末尾。 我需要做什么来解决这个问题?
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
. How can I fix this?
我还IOUtils
通过调试器运行了该方法,但得到了完全相同的结果,null
blob
但是一个填充的myinputstream
. 我怎样才能解决这个问题?
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
. I also checked the underlying mysql
database after running this code and confirmed that the blob field is empty.
在这两种情况下,调试器说myinputstream
有blksize
16384,buffcount
12742和max
127394在指定的位置,尽管blob
剩余null
。mysql
运行此代码后,我还检查了底层数据库,并确认 blob 字段为空。
EDIT#2
编辑#2
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:
然后我通过 eclipse 调试器运行以下命令,这表明在尝试填充它之后byte[]
被调用的对象content
仍然是空的。所以结果blob
是空的,而inputstream
确实继续具有与上面编辑#1 中所示相同的内容值:
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();}
回答by tbodt
The easy way:
简单的方法:
contents = IOUtils.toByteArray(in);
But for that, you need Apache Commons IO, which might be hard to add. If you don't want to use it, or can't, here's one way of doing it yourself;
但为此,您需要 Apache Commons IO,这可能很难添加。如果你不想使用它,或者不能使用它,这里有一种自己做的方法;
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();
回答by Angel O. Flores Torres
You can use the same:
您可以使用相同的:
Blob xmlForSign=null;
xmlForSign.getBinaryStream();
this method getBinaryStream() return an Inputfilestream.
这个方法 getBinaryStream() 返回一个 Inputfilestream。