Downloading Content from the Internet

Let us write a Simple Downloader in this techincal tip by making use of the classes with package. URL stands for Uniform Resource Locator and it is used to locate a resource in the Web in a standard fashion. A resource in the Web can be anything; it can be as simple as Html Document or can be a complex multimedia content such as audio/video file. For example, represents a URL which is a simple Html Resource. Likewise, represents a URL which is a downloadable file resource.

also read:

In Java, an URL is represented by the URL class. Another API that is closely associated with the URL class is the URLConnection object. It represents an open connection to the target resource. Given below is the complete code listing for the Simple Downloader.



public class ContentDownloader {

    private String url;
    private static final int SIZE = 1024;
    private String destinationFile;

    public ContentDownloader(String url){
        this.url = url;

    public void downloadContentsTo(String destinationFile){
        this.destinationFile = destinationFile;

        URL urlObject = null;
        try {
            urlObject = new URL(url);
        }catch (MalformedURLException e) {

        URLConnection urlConnection = null;
        InputStream inputStream = null;
        BufferedInputStream bufferedInput = null;

        FileOutputStream outputStream = null;
        BufferedOutputStream bufferedOutput = null;

            urlConnection = urlObject.openConnection();
            inputStream = urlConnection.getInputStream();
            bufferedInput = new BufferedInputStream(inputStream);

            outputStream = new FileOutputStream(this.destinationFile);
            bufferedOutput = new BufferedOutputStream(outputStream);

            byte[] buffer = new byte[SIZE];
            while (true){
                int noOfBytesRead =, 0, buffer.length);
                if (noOfBytesRead == -1){
                bufferedOutput.write(buffer, 0, noOfBytesRead);
        }catch (IOException e) {
            closeStreams(new InputStream[]{bufferedInput, inputStream},
                new OutputStream[]{bufferedOutput, outputStream});
        System.out.println("Downloading completed");

    private void closeStreams(
        InputStream[] inputStreams, OutputStream[] outputStreams){

            for (InputStream inputStream : inputStreams){
                if (inputStream != null){
            for (OutputStream outputStream : outputStreams){
                if (outputStream != null){
        }catch(IOException exception){

The above class has a constructor that accepts the URL of the resource whose content we wish to download. The section of interest is the downloadContentsTo() method which accepts the name of the file in the local machine to which the content of the URL has to be stored. Now, let us look into the implementation of the method. The code tries to represent the URL string that we passed as a URL object. If the URL string is not valid, then a MalformedURLException will be thrown.

It then tries to open a connection to the URL object by calling the URL.openConnection() method which returns a InputStream object. We have decorated the returned input stream to the BufferedInputStream object for faster reading from the underlying stream. We read the contents of the input stream and at the same time write the contents to the output stream as pointed by the destination file.

Now, let us run the above program, by having the following test class,


public class ContentDownloaderTest {

    public static void main(String[] args) {

        String url = "";
        String destinationFile = 'C:\googleHomePage.html';

        ContentDownloader downloader = new ContentDownloader(url);

A file called googleHomePage.html will be generated in the specified location with the content same as that of the home page of google.



About Krishna Srinivasan

He is Founder and Chief Editor of JavaBeat. He has more than 8+ years of experience on developing Web applications. He writes about Spring, DOJO, JSF, Hibernate and many other emerging technologies in this blog.

Speak Your Mind