Java BufferedImage from URL downloadImage(URL url)

Here you can find the source of downloadImage(URL url)

Description

download Image

License

Open Source License

Declaration

public static BufferedImage downloadImage(URL url) 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

import java.awt.image.BufferedImage;
import java.io.BufferedInputStream;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;

import java.io.IOException;
import java.io.InputStream;
import java.net.URL;

import javax.imageio.ImageIO;

public class Main {
    public static BufferedImage downloadImage(URL url) {
        try {// ww w  .  java2 s.  c om
            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[] byteArray = out.toByteArray();
            ByteArrayInputStream inByte = new ByteArrayInputStream(byteArray);
            BufferedImage read = ImageIO.read(inByte);
            return read;
        } catch (IOException ioe) {
            ioe.printStackTrace();
        }
        return null;
    }
}

Related

  1. downloadImage(String url)
  2. getFileAndDownload(String urlString, String folder)
  3. httpGetImage(String url)
  4. Img2ByteByUrl(String strUrl)
  5. urlToImage(String urlstring)