tangbinde 发表于 2016-11-16 09:43:38

DB2上传图片到数据库的Blob字段

一般情况下想将图片以二进制形式存入数据库字段我是这样做的:
第一:数据库:Db2
第二:字段类型:BLOB
第三:图片路径:Path 我这里是读取网站的图片,带http这种,这样我们就是用URL
第四:其他代码:
URL url = new URL(Path);
    BufferedInputStream in = new BufferedInputStream(url.openStream());
ByteArrayOutputStream out = new ByteArrayOutputStream(1024);
byte[] temp = new byte;
int size = 0;

while ((size = in.read(temp)) != -1) {
out.write(temp, 0, size);
}
in.close();
byte[] content = out.toByteArray();


PreparedStatement pstmt= db.prepareStatements("update Table set XXXX=? where XXXX=?");
pstmt.setBytes(1, content);
pstmt.setString(2, XXXX);
pstmt.executeUpdate();
pstmt.close();


这个是一个更新图片功能
页: [1]
查看完整版本: DB2上传图片到数据库的Blob字段