Download a Webpage Java Program

By | June 8, 2016

Java Program To Download A Webpage using Command Line

Learn How To Write a Java Program To Download A Webpage using Command Line Arguments on the Console Screen. This Code To Download a Website Page in Java makes use of Advanced Java Programming concepts. The java.net package plays a very important role in the code.

This Java Code To Fetch the Webpage saves the webpage in an HTML format in a text file. It, therefore, helps you to Save a Webpage for Offline Reading.

How To Enter Command Line Arguments

Syntax
java class_name complete_url file_name

Example
java DownloadWebpage https://www.codingalpha.com CodingAlpha

The First Argument needs to be have the complete URL of the Webpage to be downloaded. It has to even the HTTP or HTTPS. The Second URL is used for saving the webpage in the system. Therefore, you can give it a name along with its extension such as .HTML, .HTM, .TXT and many others. The default extension is .HTML.

Also Read: Java Program To Take User Input using Scanner Class

Code To Download Website in Advanced Java Programming

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

public class DownloadWebpage
{
    public static void get(String tURL_String,String tSaveFilename) throws MalformedURLException, IOException 
    {
        int tByteCount = 0;     
        BufferedInputStream tURL_Input = null;
        byte tInputByteData[] = new byte[1024];
        FileOutputStream tLocalSavedFile  = null;
        try
        {
            tURL_Input = new BufferedInputStream(new URL(tURL_String).openStream());
            tLocalSavedFile = new FileOutputStream(tSaveFilename);
            while ((tByteCount = tURL_Input.read(tInputByteData, 0, 1024)) != -1) 
            {
                tLocalSavedFile.write(tInputByteData, 0, tByteCount);
            }
        }
        catch(MalformedURLException a)
        {

        }
        catch(IOException b)
        {
            if (!(tURL_Input == null))
            {
                tURL_Input.close();
            }   
            else
            {
                throw b; 
            }
        }
        if(tLocalSavedFile != null) 
        {
            tLocalSavedFile.close();
        }
        else
        {
            throw new IOException();
        }
    }
    public static void usage()
    {
        System.out.println("Parameters To Download The Webpage: Complete_URL File_Name");
    }
    public static void main(String argument[])
    {
        StringBuilder tStringBuilder = new StringBuilder(240);
        if(argument.length != 2)
        {
            DownloadWebpage.usage();
            return;   
        }
        try
        {
            DownloadWebpage.get(argument[0],argument[1]);
        }
        catch(Exception c)
        {
            c.printStackTrace();
            return;
        }
        tStringBuilder.append("nThe Webpage is downloaded successfully.nnFile is Saved: ").append(argument[1]);
        System.out.println(tStringBuilder.toString());
    }
}

Also Read: How To Connect C and Java for Java Network Interface

 

Output

Advanced Java Tutorial To Download a Webpage in Java using Command Line Arguments

If you have any Compilation Error or Doubt about this Java Program To Download A Webpage, let us know about it in the Comment Section below.

2 thoughts on “Download a Webpage Java Program

  1. Pankaj Dhende

    Such an amazing program! Thanks! Now, I can download and save webpages for offline reading! 😀

    Reply

Let's Discuss