收录日期:2020/01/29 12:52:12 时间:2009-02-18 14:30:26 标签:java

I am having the following requirement. When the user cliock the buton the image should be displayed.The following code that i have tried gives me error

    Toolkit tk = Toolkit.getDefaultToolkit();
    Image ima = Toolkit.getImage("C:\\DB.jpg");
    MediaTracker mt = new MediaTracker(new Canvas());
    mt.addImage(ima, 0);
    try 
    {
        mt.waitForAll();
    }
    catch(Exception x)  {}
    OutputStream os = res.getOutputStream();
    // Now create a writable image,
    BufferedImage bi = new BufferedImage(ima.getWidth(null), ima.getHeight(null), BufferedImage.TYPE_INT_RGB);
    bi.drawImage(ima, 0, 0, null); // Draw the loaded image onto a writable thing
    JPEGImageEncoder je = JPEGCodec.createJPEGEncoder(os);
    je.encode(ima);
    os.close();
}

Before calling res.getOutputStream() have you first made sure you have called res.setContentType("image/jpeg")?