如何把inputstream转化成blob存入数据库中
1个回答
2015-11-25
展开全部
private byte[] toByteArray(Blob fromBlob) throws SQLException, IOException {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
return toByteArrayImpl(fromBlob, baos);
}
private byte[] toByteArrayImpl(Blob fromBlob, ByteArrayOutputStream baos) throws SQLException, IOException{
byte[] buf = new byte[10*1024*1024];
InputStream is = fromBlob.getBinaryStream();
try {
for (;;) {
int dataSize = is.read(buf);
if (dataSize == -1)
break;
baos.write(buf, 0, dataSize);
}
}
finally { if (is != null) {
try { is.close(); }
catch (IOException ex) { }
}
}
return baos.toByteArray();
}
ByteArrayOutputStream baos = new ByteArrayOutputStream();
return toByteArrayImpl(fromBlob, baos);
}
private byte[] toByteArrayImpl(Blob fromBlob, ByteArrayOutputStream baos) throws SQLException, IOException{
byte[] buf = new byte[10*1024*1024];
InputStream is = fromBlob.getBinaryStream();
try {
for (;;) {
int dataSize = is.read(buf);
if (dataSize == -1)
break;
baos.write(buf, 0, dataSize);
}
}
finally { if (is != null) {
try { is.close(); }
catch (IOException ex) { }
}
}
return baos.toByteArray();
}
本回答被网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询