Download File from Website using Java

This is a tutorial in which we will going to create a program that can download an image file or any files in a website using Java. So, now let's start this tutorial! 1. Open JCreator or NetBeans and make a java program with a file name of imgFromURL.java. 2. Import the following packages:
  1. import java.io.*; //used to access the DataInputStream and FileOutputStream class that will get and save the file to the directory program
  2. import java.net.*; //used to access the URL and URL connection class to access websites
3. Initialize your variable in your Main, variable p for byte data type which will save memory in a large array since this data type is 4 times smaller than int data type and variable url for the URL class that will open the world wide web in a specific website as instantiated.
  1. byte[] b = new byte[1];
  2. URL url = new URL("http://www.sourcecodester.com/sites/default/files/print_0.png");
We access an image file as we have a png extension file from the sourcecodester site. Now create a variable named urlConnect for the URLConnection class that we will open the connection using the openConnection method of the variable url above. And with the urlConnect variable, have its connect method to perform connectivity to the site. But take note that we will need the internet on this to download the file.
  1. URLConnection urlConnect = url.openConnection();
  2. urlConnect.connect();
3. Create now a variable di for our DataInputStream class to get the the inputted url from the URLConnection class.
  1. DataInputStream di = new DataInputStream(urlConnect.getInputStream());
Now, to get the file we will use the FileOutputStream and have it instantiated with the name of the file to be saved. Have this code below:
  1. FileOutputStream fo = new FileOutputStream("print_0.png");
4. To download the file, have this code below using the FileOutputStream, DataInputStream variable and the byte variable, then close the di and fo variable for streaming.
  1. while (-1 != di.read(b, 0, 1))
  2. fo.write(b, 0, 1);
  3. di.close();
  4. fo.close();
Output: download image Here's the full code of this tutorial:
  1. import java.io.*; //used to access the DataInputStream and FileOutputStream class that will get and save the file to the directory program
  2. import java.net.*; //used to access the URL and URL connection class to access websites
  3.  
  4. public class imgFromURL {
  5. public static void main(String args[]) throws Exception {
  6.  
  7. byte[] b = new byte[1]; //data type to save memory space of the file
  8. URL url = new URL("http://www.sourcecodester.com/sites/default/files/print_0.png"); // url of the site that we will going to download the image file
  9.  
  10. URLConnection urlConnect = url.openConnection();
  11. urlConnect.connect(); // get the connection to the site
  12.  
  13.  
  14. DataInputStream di = new DataInputStream(urlConnect.getInputStream()); //have the input to the connection
  15.  
  16. FileOutputStream fo = new FileOutputStream("print_0.png"); //download the file and have it named print_0.png
  17.  
  18. while (-1 != di.read(b, 0, 1)) //download the image file
  19. fo.write(b, 0, 1);
  20. di.close(); // close DataInputStream
  21. fo.close(); // close FileOutputStream
  22. }
  23. }
Best Regards, Engr. Lyndon Bermoy IT Instructor/System Developer/Android Developer/Freelance Programmer If you have some queries, feel free to contact the number or e-mail below. Mobile: 09488225971 Landline: 826-9296 E-mail:[email protected] Add and Follow me on Facebook: https://www.facebook.com/donzzsky Visit and like my page on Facebook at: https://www.facebook.com/BermzISware

Add new comment