Image image = null;
try 
    URL url = new URL("http://www.yahoo.com/image_to_read.jpg");
    image = ImageIO.read(url);
} catch (IOException e) {
}

See javax.imageio package for more info. That’s using the AWT image. Otherwise you could do:

 URL url = new URL("http://www.yahoo.com/image_to_read.jpg");
InputStream in = new BufferedInputStream(url.openStream());
ByteArrayOutputStream out = new ByteArrayOutputStream();
byte[] buf = new byte[1024];
int n = 0;
while (-1!=(n=in.read(buf)))
{
   out.write(buf, 0, n);
}
out.close();
in.close();
byte[] response = out.toByteArray();

And you may then want to save the image so do:

 

 

FileOutputStream fos = new FileOutputStream("C://borrowed_image.jpg");
    fos.write(response);
    fos.close();
arrow
arrow
    文章標籤
    Java
    全站熱搜

    techdrew 發表在 痞客邦 留言(0) 人氣()