In this program we will see how we can download image from an URL in java language.
Java Program to Download Image From an URL
For that we will be using this image at the URL
Image Source: https://cdn.pixabay.com/photo/2015/04/23/22/00/tree-736885__480.jpg
Explanation:
To download an image from the internet we first need the URL of the image. After that we need to read the image from the site and then write it into our system storage.
Method-1: Java Program to Download Image From an URL By Using Input and OutputStream
Approach:
- Use a try catch block to catch any IO errors caused .
- Store the URL of the image in an URL object and store the path to save the file in a string.
- Create an
InputStream
object on the URL provided to fetch the image by usingopenStream()
method and anOutputStream
object on the download path to store the object. - Use a while loop till the EOF to copy the image from the
InputStream
object to theFileOutputStream
object. - Close the stream objects and print the path of the image on successful download.
Program:
package btechgeeks; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.net.URL; public class Main { public static void main(String[] args) { //Try catch block to catch any IO error thrown try { //URL of the image URL link = new URL ("https://cdn.pixabay.com/photo/2015/04/23/22/00/tree-736885__480.jpg"); //The path to store the downloaded image into String downloadPath = "D:\\ImageProgram/downloadedImage.jpg"; //Using inputStream to read the image from the URL InputStream is = link.openStream(); //Saving the image into the path specified OutputStream fos = new FileOutputStream(downloadPath); int ch; //Loop until all the data from the image is copied and we reach the EOF while ((ch = is.read()) != -1) { fos.write(ch); } //Output statement to execute upon successful download System.out.println("Your image has been downloaded successfully and is stored at " +downloadPath); //Close the objects. is.close(); fos.close(); } //Catch block to catch IO errors catch(IOException e) { e.printStackTrace(); } } }
Output:
Your image has been downloaded successfully and is stored at D:\ImageProgram/downloadedImage.jpg
In File Explorer-
Method-2: Java Program to Download Image From an URL By Using ImageIO
Syntax:
ImageIo.write(imageObj,extension,image,FileObject )
Where,
- imageObj – Image object is the object we have stored the fetched image in
- Extension – Extension is the extension of the file i.e. jpg, jpeg, png etc
- FileObject- It is the file we are storing the file in.
Approach:
- Create an object of
BufferedImage
. - Inside a try catch block, store the image url in an URL object.
- Fetch the image from the url using
ImageIO.read( )
method. - Write to the file using
ImageIo.write( )
method.
Program:
package btechgeeks; import java.io.FileOutputStream; import java.io.IOException; import javax.imageio.ImageIO; import java.awt.image.BufferedImage; import java.io.File; import java.net.URL; public class Main { public static void main(String[] args) { // Create an BufferedImage object to store the image BufferedImage img = null; // Try catch block to catch any IO error thrown try { // Image Link URL link = new URL("https://cdn.pixabay.com/photo/2015/04/23/22/00/tree-736885__480.jpg"); // Read image from the link img = ImageIO.read(link); // Write the image to the folder ImageIO.write(img, "jpg",new File("D:\\ImageProgram/downloadedImage.jpg")); System.out.println("Your image has been downloaded successfully"); } // Catch block to catch IO erroes catch(IOException e) { e.printStackTrace(); } } }
Output:
Your image has been downloaded successfully
In File Explorer-
Have you mastered basic programming topics of java and looking forward to mastering advanced topics in a java programming language? Go with these ultimate Advanced java programs examples with output & achieve your goal in improving java coding skills.