Download image from URL in Java

Downloading an image from a URL involves retrieving the file over the internet and saving it to your local system. Let’s break this process into simple, detailed steps to ensure you understand both the concept and its practical implementation.

First Understand the Core Concepts :-

  1. URL (Uniform Resource Locator):
    A URL points to a specific resource on the web, such as an image, webpage, or file. In Java, the java.net.URL class allows you to connect to this resource and retrieve its content.

  2. InputStream and OutputStream:

    • InputStream is used to read data from a source, such as a URL.
    • OutputStream is used to write data to a destination, such as a file on your computer. These streams help in transferring the image data byte by byte.
  3. Buffering:
    To improve performance, we use a buffer (a temporary storage area) to read and write data in chunks, rather than handling each byte individually.

Step by Step explanation of code :-

By Using Java’s Built-In Classes (java.net and java.io)

Step 1: Define the Image URL and Destination File-

String imageUrl = "https://codespeedy.com/image.jpg"; // The URL of the image
String destinationFile = "downloaded_image.jpg";  // Local file name where the image will be saved

Step 2: Open a Connection to the URL-

InputStream inputStream = new URL(imageUrl).openStream();


Step 3:Create an Output Stream to Save the File-

FileOutputStream outputStream = new FileOutputStream(destinationFile);

Step 4: Read and Write Data Using a Buffer-

byte[] buffer = new byte[1024]; // Buffer size
int bytesRead;

while ((bytesRead = inputStream.read(buffer)) != -1) {
    outputStream.write(buffer, 0, bytesRead);
}

 

Complete Example:-

import java.io.*;
import java.net.URL;

public class DownloadImage {
    public static void main(String[] args) {
        String imageUrl = "https://codespeedy.com/image.jpg"; // Image URL
        String destinationFile = "downloaded_image.jpg";  // Destination file name

        try (InputStream inputStream = new URL(imageUrl).openStream();
             FileOutputStream outputStream = new FileOutputStream(destinationFile)) {

            byte[] buffer = new byte[1024]; // Buffer size
            int bytesRead;

            while ((bytesRead = inputStream.read(buffer)) != -1) {
                outputStream.write(buffer, 0, bytesRead);
            }

            System.out.println(" Codespeedy Image downloaded successfully to: " + destinationFile);
        } catch (IOException e) {
            System.out.println("Error downloading the image: " + e.getMessage());
        }
    }
}

 

Use Cases for Downloading Images

  1. Data Scraping: Download images from a webpage.
  2. Media Applications: Automate downloading of profile pictures or thumbnails.
  3. Batch Processing: Download multiple images in a batch for analysis or archiving.

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top