java - 如何检索保存为 blob 的图像

标签 java mysql swing netbeans-8

请帮助我检索我在#mysql数据库中保存为#blob的图像

这是我用来保存图像的代码

    JFileChooser chooser = new JFileChooser();
    chooser.showOpenDialog(null);
    File f = chooser.getSelectedFile();

    filename =f.getAbsolutePath();
    ImageIcon imageIcon = new ImageIcon(new ImageIcon(filename).getImage().getScaledInstance(lbl_img.getWidth(), lbl_img.getHeight(), Image.SCALE_DEFAULT));
    lbl_img.setIcon(imageIcon);
  try {

        File image = new File(filename);
        FileInputStream fis = new FileInputStream (image);
        ByteArrayOutputStream bos= new ByteArrayOutputStream();
        byte[] buf = new byte[1024];

        for(int readNum; (readNum=fis.read(buf))!=-1; ){

            bos.write(buf,0,readNum);
        }
        person_image=bos.toByteArray();
    }

    catch(Exception e){
        JOptionPane.showMessageDialog(null,e.getMessage());

    }

但是当我尝试使用检索图像时

          byte[] img = rs.getBytes("IMAGE");
          ImageIcon imageIcon = new ImageIcon(new ImageIcon(img).getImage().getScaledInstance(lbl_image.getWidth(), lbl_image.getHeight(), Image.SCALE_SMOOTH));
          lbl_image.setIcon(imageIcon);

它没有给出任何错误,但它没有检索图像。请帮我解决问题出在哪里

最佳答案

使用ImageIO.read将这些字节读取到BufferedImage

关于java - 如何检索保存为 blob 的图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56157937/

相关文章:

java - 接受来自 Retrofit API 请求的多个有效负载

java - Android拖放+自定义绘图

mysql - inner join更新sql出错

java - 如何在 NetBeans 中设置 JFrame 的 setDefaultCloseOperation() 值?

java - Spring Boot - 使用 ddl-auto=update 进行数据初始化

java - 从计数循环与迭代器中的列表中删除元素

mysql - 有没有办法计算一组 mysql 列中的值

mysql - sql - 如何获取 module=5 的类(class)

swing - Switch 语句有问题吗?

java - JLayeredPane绘制图片