-->

使用Java在PostgreSQL存储和检索图像[关闭](Store and retrieve im

2019-07-20 16:55发布

我是新来的Java编程,我正在寻找Java代码来存储图像,PostgreSQL和检索图像。

在PostgreSQL我用BYTEA数据类型。 图像储存。 但是当我找回我得到空。 我不能得到的图像。

对于这个或任何其他建议的任何实例将是有益的。

Answer 1:

第7章与存储二进制数据,并使用图像(* .gif文件)的一个例子的PostgreSQL的JDBC文档交易。 你可能想读它。

插入图像插入分贝(从上方连结)

File file = new File("myimage.gif");
FileInputStream fis = new FileInputStream(file);
PreparedStatement ps = conn.prepareStatement("INSERT INTO images VALUES (?, ?)");
ps.setString(1, file.getName());
ps.setBinaryStream(2, fis, (int)file.length());
ps.executeUpdate();
ps.close();
fis.close();

从该数据库读出的图像(也从上方连结)

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

PreparedStatement ps = conn.prepareStatement("SELECT imgoid FROM imageslo WHERE imgname = ?");
ps.setString(1, "myimage.gif");
ResultSet rs = ps.executeQuery();
while (rs.next()) {
    // Open the large object for reading
    int oid = rs.getInt(1);
    LargeObject obj = lobj.open(oid, LargeObjectManager.READ);

    // Read the data
    byte buf[] = new byte[obj.size()];
    obj.read(buf, 0, obj.size());
    // Do something with the data read here

    // Close the object
    obj.close();
}
rs.close();
ps.close();


Answer 2:

去throught的Java数据库连接 API,这里是一个例子 ,如何连接到Postgres数据库。 如果存在任何疑问..跟进!



文章来源: Store and retrieve images in Postgresql using Java [closed]