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.

