How to show an image from ms access to jpanel in java netbeans

后端 未结 1 1958
时光说笑
时光说笑 2020-12-12 06:30

The code I\'ve used:

private void okActionPerformed(java.awt.event.ActionEvent evt)        
{                                   
    try {
        String Upd         


        
相关标签:
1条回答
  • 2020-12-12 07:07

    This section of code doesn't make sense.

    Blob blob = rs.getBlob("Image");
    int b;
    InputStream bis = rs.getBinaryStream("Image");
    
    FileOutputStream f = new FileOutputStream("Image.jpg");
    while ((b = bis.read()) >= 0) {
        f.write(b);
    }
    f.close();
    bis.close();
    
    icon = new ImageIcon(blob.getBytes(1L, (int) blob.length()));
    

    You basically read the BLOB from result set to a file and then try and read it again to construct your image. It's possible that you've exhausted the stream.

    Why not just read the image?

    icon = new ImageIcon("Image.jpg");
    

    Better yet, why not take advantage of the ImageIO API and read the stream directly, for-going the need to write out a temp file?

    BufferedImage image = ImageIO.read(bis); 
    icon = image == null ? null : new ImageIcon(image);
    
    0 讨论(0)
提交回复
热议问题