(throws IOException) Image image = null; try { URL url = new URL("http://www.yahoo.com/image_to_read.jpg"); image = ImageIO.read(url); } catch (IOException e) { }
See the javax.imageio package for more javax.imageio . This is the use of an AWT image. Otherwise, you can 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 then you can save the image like this:
FileOutputStream fos = new FileOutputStream("C://borrowed_image.jpg"); fos.write(response); fos.close();
planetjones May 04 '11 at 10:32 2011-05-04 10:32
source share