GetFileFromSite.java


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

class GetFileFromSite {
  public static void main(String[] args) {
    if(args.length != 2) {
      System.out.println("Usage: java GetFileFromSite url file");
      return;
    }

    InputStream in = null;
    URLConnection connection = null;
    FileOutputStream fout = null;

    try {
      URL url = new URL(args[0]);
      connection = url.openConnection();
      in = connection.getInputStream();
      fout = new FileOutputStream(args[1]);

      // Download and save the file.
      int b;
      while (((b = in.read()) != -1)) {
        fout.write(b);
      }
    } catch (IOException exc) {
      System.out.println("Connection Error: " + exc);
    } finally {
      try {
        if(in != null) in.close();
        if(fout != null) fout.close();
      } catch (IOException exc) {
        System.out.println("Error closing stream: " + exc);
      }
    }
  }
}


Results

Try the following command:

java GetFileFromSite https://johnloomis.org/motd/owl1.jpg owl1.jpg

Reference

Herbert Schildt and Dale Skrien, Java Programming, A Comprehensive Introduction,
McGraw-Hill, 2013. ISBN 978-0-07-802207-4. p 998-1000.

Try this command (all on one line) to get a zip file created by the author of the reference:

java GetFileFromSite http://highered.mcgraw-hill.com/sites/dl
/free/0072974168/584690/SourceCode.zip SourceCode.zip


Maintained by John Loomis, updated Wed Nov 14 20:43:15 2012