请问 hibernate postgresql Bytea oid 保存图片问题

leagion 发布于 2013/07/08 22:43
阅读 685
收藏 0

@beming 你好,想跟你请教个问题:

我用hibernate postgresql 保存图片( Bytea 和 oid  )
,请问怎样写 ?有示例吗?网上找了很久没有找到,帮帮忙,感谢啊。

http://jdbc.postgresql.org/documentation/80/binary-data.html 看到:

// All LargeObject API calls must be within a transaction block
conn.setAutoCommit(false);

// Get the Large Object Manager to perform operations with
LargeObjectManager lobj = ((org.postgresql.PGConnection)conn).getLargeObjectAPI();

// Create a new large object
int oid = lobj.create(LargeObjectManager.READ | LargeObjectManager.WRITE);

// Open the large object for writing
LargeObject obj = lobj.open(oid, LargeObjectManager.WRITE);

// Now open the file
File file = new File("myimage.gif");
FileInputStream fis = new FileInputStream(file);

// Copy the data from the file to the large object
byte buf[] = new byte[2048];
int s, tl = 0;
while ((s = fis.read(buf, 0, 2048)) > 0) {
    obj.write(buf, 0, s);
    tl += s;
}

// Close the large object
obj.close();

// Now insert the row into imageslo
PreparedStatement ps = conn.prepareStatement("INSERT INTO imageslo VALUES (?, ?)");
ps.setString(1, file.getName());
ps.setInt(2, oid);
ps.executeUpdate();
ps.close();
fis.close();

// Finally, commit the transaction.
conn.commit();

怎么改改呢?





加载中
0
kenyon_君羊
kenyon_君羊
为毛喜欢把图片存数据库里?
kenyon_君羊
kenyon_君羊
回复 @leagion : 怎么会变来变去的,要有一个统一的规划,后期扩展和性能提升有帮助
l
leagion
存路径吗? 但是图片路径变来变去就不好了
0
b
beming
我不喜欢将图片存数据库,最佳实践也不建议这样子做啦
返回顶部
顶部