In this article we are going to discuss about making of a file downloader with help of java programming language.This will be a very simple downloader where you will define the download site address and the output file name in program code .The graphical user interface shows the current download status using the progress bar.
Now we will discuss the steps to be followed:
First, we need to import all the necessary classes required by our program. These are :
import java.awt.FlowLayout; //Defines the layout import java.io.*; //For input-output operations import java.net.HttpURLConnection; //For making a connection import java.net.URL; //Helps in making a URL object import javax.swing.JFrame; //Helps in making Swing Frame import javax.swing.JProgressBar; //Helps in implementing Progress Bar
Now we define a Class named Downloader and since we are using swing frame so we extend a JFrame.We also define a main function:
public class Downloader extends JFrame
{
public static final void main(String[] args) throws Exception
{
Now we define the site from where we are going to download the data from,this info is stored in variable named site. Also we store the name of the output file in variable name, the filename
String site="http://www.site.com/file.zip"; String filename="temp.zip";
Now we make a frame named frm and a progress bar which will show the current download status.
JFrame frm=new JFrame(); JProgressBar current = new JProgressBar(0, 100); //We are setting the size of progress bar current.setSize(50,50); //Initially the progress bar will be zero%. current.setValue(0); current.setStringPainted(true); //Adding the progress bar to the frame frm.add(current); frm.setVisible(true);//Making the frame visible frm.setLayout(new FlowLayout()); frm.setSize(400, 200); //EXIT_ON_CLOSE make sure that the application gets exited when frame close frm.setDefaultCloseOperation(EXIT_ON_CLOSE);
Now we write the important section which is responsible for downloading of data.
We connect to the site using the url object.
try {
URL url=new URL(site);
HttpURLConnection connection =
(HttpURLConnection) url.openConnection();
We retrieve the filesize and store it:
int filesize = connection.getContentLength();
Holds the number of bytes which have been downloaded:
float totalDataRead=0;
Now we make an object using filename variable which will be used to write data in a new file.
java.io.BufferedInputStream in = new java.io.BufferedInputStream(connection.getInputStream()); java.io.FileOutputStream fos = new java.io.FileOutputStream(filename); java.io.BufferedOutputStream bout = new BufferedOutputStream(fos,1024);
Now we define an object which will hold the temporary read data:
byte[] data = new byte[1024]; int i=0;
Now We start reading the download content. We read the data so that the maximum buffer read is 1024.
while((i=in.read(data,0,1024))>=0)
{
We store the total amount of data which have been read. Here I will contain the number of bytes read.
totalDataRead=totalDataRead+i;
We write this data into the output file:
bout.write(data,0,i);
Now we determine the download % and use that value to set the progress bar:
float Percent=(totalDataRead*100)/filesize; current.setValue((int)Percent); }
We Close object:
bout.close(); in.close(); }
We handle any exception:
catch(Exception e)
{In case of any exception the below frame gets opened showing error message:
javax.swing.JOptionPane.showConfirmDialog((java.awt.Component) null,e.getMessage(), "Error", javax.swing.JOptionPane.DEFAULT_OPTION); } } }
Output :


Now you can see the full source code of the article:
import java.awt.FlowLayout;
import java.io.*;
import java.net.HttpURLConnection;
import java.net.URL;
import javax.swing.JFrame;
import javax.swing.JProgressBar;
public class Downloader extends JFrame
{
public static final void main(String[] args) throws Exception
{
String site="http://www.mysite.com/file.zip";
String filename="temp.zip";
JFrame frm=new JFrame();
JProgressBar current = new JProgressBar(0, 100);
current.setSize(50,50);
current.setValue(43);
current.setStringPainted(true);
frm.add(current);
frm.setVisible(true);
frm.setLayout(new FlowLayout());
frm.setSize(400, 200);
frm.setDefaultCloseOperation(EXIT_ON_CLOSE);
try {
URL url=new URL(site);
HttpURLConnection connection =
(HttpURLConnection) url.openConnection();
int filesize = connection.getContentLength();
float totalDataRead=0;
java.io.BufferedInputStream in = new java.io.BufferedInputStream(connection.getInputStream());
java.io.FileOutputStream fos = new java.io.FileOutputStream(filename);
java.io.BufferedOutputStream bout = new BufferedOutputStream(fos,1024);
byte[] data = new byte[1024];
int i=0;
while((i=in.read(data,0,1024))>=0)
{
totalDataRead=totalDataRead+i;
bout.write(data,0,i);
float Percent=(totalDataRead*100)/filesize;
current.setValue((int)Percent);
}
bout.close();
in.close();
}
catch(Exception e)
{
javax.swing.JOptionPane.showConfirmDialog((java.awt.Component)
null,e.getMessage(), "Error",
javax.swing.JOptionPane.DEFAULT_OPTION);
}
}
}







See the prices for this post in Mr.Bool Credits System below: