显示来自 PostgreSQL 数据库、bytea 的图像

发布于 2024-11-09 03:51:18 字数 538 浏览 0 评论 0原文

此代码获取图片 test.gif 并将图片位值放入 bytea 列中。

public void addImage() throws SQLException, IOException {
    Connection con = openConnection();

    File file = new File("test.gif");
    FileInputStream fis = new FileInputStream(file);
    PreparedStatement ps = con.prepareStatement("INSERT INTO image VALUES (?, ?, ?)");
    ps.setString(1, file.getName());
    ps.setBinaryStream(2, fis, (int)file.length());
    ps.setInt(3,1);
    ps.executeUpdate();
    fis.close();

}

我的下一个目标是以字节为单位显示保存在数据库中的图片。这怎么办?

This code takes picture test.gif and puts the picture bit value into a bytea coloumn.

public void addImage() throws SQLException, IOException {
    Connection con = openConnection();

    File file = new File("test.gif");
    FileInputStream fis = new FileInputStream(file);
    PreparedStatement ps = con.prepareStatement("INSERT INTO image VALUES (?, ?, ?)");
    ps.setString(1, file.getName());
    ps.setBinaryStream(2, fis, (int)file.length());
    ps.setInt(3,1);
    ps.executeUpdate();
    fis.close();

}

My next goal is to display the picture that is saved in the database in bytes. How can this be done ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

〗斷ホ乔殘χμё〖 2024-11-16 03:51:18

像这样的东西:

// Run a query again the IMAGE table to fetch the image with the given id
public Image readImage(Connection conn, int id) throws SQLException {
  PreparedStatement pstmt = null;
  try {
    pstmt = conn.prepareStatement("SELECT contents FROM image WHERE id = ?");
    pstmt.setInt(1, id); 
    ResultSet rs = ps.executeQuery();
    if (rs.next()) {
      InputStream is = rs.getBinaryStream(1);
      return ImageIO.read(is);
    } else {
      return null;
    }
  } finally {
    if (pstmt != null) {
      try {
        pstmt.close();
      } catch (SQLException ignored) {
      }
    }
  }
}

// Display the given Image on a Swing frame
public void showImage(final Image img) {
  JFrame frame = new JFrame("Image");
  frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  frame.setSize(img.getWidth(), img.getHeight());
  frame.add(new JPanel() {
    public void paint(Graphics g) {
      g.drawImage(img, 0, 0, null);
    }
  });
  frame.show();
}

可能对你有用。

Something like:

// Run a query again the IMAGE table to fetch the image with the given id
public Image readImage(Connection conn, int id) throws SQLException {
  PreparedStatement pstmt = null;
  try {
    pstmt = conn.prepareStatement("SELECT contents FROM image WHERE id = ?");
    pstmt.setInt(1, id); 
    ResultSet rs = ps.executeQuery();
    if (rs.next()) {
      InputStream is = rs.getBinaryStream(1);
      return ImageIO.read(is);
    } else {
      return null;
    }
  } finally {
    if (pstmt != null) {
      try {
        pstmt.close();
      } catch (SQLException ignored) {
      }
    }
  }
}

// Display the given Image on a Swing frame
public void showImage(final Image img) {
  JFrame frame = new JFrame("Image");
  frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  frame.setSize(img.getWidth(), img.getHeight());
  frame.add(new JPanel() {
    public void paint(Graphics g) {
      g.drawImage(img, 0, 0, null);
    }
  });
  frame.show();
}

might work for you.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文