JGet, something like wget

By Alvin J. Alexander, devdaily.com

Here's the source for a program named JGet, which acts similar to the wget program ... except I didn't have wget installed when I need it, so I wrote this.

/*
 * simple class to provide functionality similar to Wget.
 *
 * Note: Could also strip out all of the html w/ jtidy.
 */

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

public class JGet
{

  public static void main(String[] args)
  {

    if ( (args.length != 1) )
    {
      System.err.println( "\nUsage: java JGet [urlToGet]" );
      System.exit(1);
    }

    String url = args[0];

    URL u;
    InputStream is = null;
    DataInputStream dis;
    String s;

    try
    {
      u = new URL(url);
      is = u.openStream();
      dis = new DataInputStream(new BufferedInputStream(is));
      while ((s = dis.readLine()) != null)
      {
        System.out.println(s);
      }
    }
    catch (MalformedURLException mue)
    {
      System.err.println("Ouch - a MalformedURLException happened.");
      mue.printStackTrace();
      System.exit(2);
    }
    catch (IOException ioe)
    {
      System.err.println("Oops- an IOException happened.");
      ioe.printStackTrace();
      System.exit(3);
    }
    finally
    {
      try
      {
        is.close();
      }
      catch (IOException ioe)
      {
      }
    }

  }

}

This program downloads the contents of a URL. I put a little "usage" statement in the code to help just a little.


devdaily logo