download Image from URL - Java 2D Graphics

Java examples for 2D Graphics:BufferedImage Create

Description

download Image from URL

Demo Code


import java.awt.*;
import java.awt.event.*;
import java.awt.image.*;
import javax.swing.*;
import javax.swing.event.*;
import java.io.*;
import java.net.*;
import java.util.*;
import javax.imageio.*;
import java.lang.reflect.*;

public class Main{
    public static BufferedImage downloadImage(URL url) throws IOException {
        DownloaderUtils.debug("downloading image: " + url);

        HttpURLConnection conn = (HttpURLConnection) url.openConnection();
        if (conn.getResponseCode() == HttpURLConnection.HTTP_NOT_FOUND)
            return (null);

        InputStream in = conn.getInputStream();

        BufferedImage image = ImageIO.read(in);

        return (image);
    }//from w w w  .j a  v  a  2s .com
    public static void debug(String message) {
        System.out.println("DEBUG: " + message);
    }
}

Related Tutorials