import java.io.*; import java.net.*; public class Downloader { // small HTTP (web) client similar to wget program private static void download(String url) throws Exception { URL myURL = new URL(url); // getFile() ==> filename URLConnection myConn = myURL.openConnection(); // 1KB = 1024 Bytes (1 Byte = 8 bits) double totalSize = myConn.getContentLength(); // network reader InputStream netIn = myURL.openStream(); String fname = myURL.getFile(); System.out.println("downloading " + fname + " " + totalSize + " bytes"); FileOutputStream fileOut = new FileOutputStream("tmpFile.gif"); byte[] buffer = new byte[512]; double currentSize = 0; int nByte = netIn.read(buffer); // fill buffer from current cursor position while (nByte != -1) { // write buffer to file fileOut.write(buffer); currentSize += nByte; nByte = netIn.read(buffer); // fill buffer from current cursor position } System.out.println("completed downloading: " + currentSize + " bytes"); fileOut.close(); netIn.close(); } public static void main(String[] args) throws Exception { String url = "http://www.cs.gettysburg.edu/~skim/cs112/hws/hw3/Cat.gif"; download(url); //new Downloader().download(url); // if download is not static } }