请帮助我检索我在#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/