Java wget - JGet, something like wget

Java FAQ: Can you share some source code for a “Java wget” program, i.e., a Java program that works like the Unix wget or curl commands?

Here's the source for a program I've named JGet, which acts similar to the wget or curl programs. I didn't have wget installed when I needed it (and my client wouldn't let me install it), so I wrote this Java wget replacement program.

NOTE: This is a simple app, without the bells and whistles of wget and curl.

NOTE: See the Comments section below for a nice simplification/improvement.

My Java 'wget' replacement program

Without any further ado, here's the source code for my Java wget class:

/*
 * A simple Java 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)
      {
      }
    }

  }

}

As shown, this little Java wget program downloads the contents of a URL. I put a little "usage" statement in the code to help just a little. If you're comfortable with Java, it should be pretty easy to grok.

Permalink

Dear Alvin,

First of all I would like to thank you for the interest post this article. It thanks for sharing all your tips and (extensive) knowledge! It was really help for me to avoid wget program.
Thanks,
Sujay

Permalink

Wow, this is a great little gem that saved me a lot of effort. It works for ftp://, http:// and https://. With new features in Java 1.7 and deprecation of DataInputStream, it can be written a little bit simpler now:

public static void wget(String url) throws MalformedURLException, IOException {
String s;
URL u = new URL(url);

try (InputStream is = u.openStream()) {
BufferedReader dis = new BufferedReader(new InputStreamReader(is));
while ((s = dis.readLine()) != null)
{
System.out.println(s);
}
}
}

Add new comment

The content of this field is kept private and will not be shown publicly.

Anonymous format

  • Allowed HTML tags: <em> <strong> <cite> <code> <ul type> <ol start type> <li> <pre>
  • Lines and paragraphs break automatically.
By submitting this form, you accept the Mollom privacy policy.