/******************************************************************** * World Wide Web Page Download Using Java * *===================================================================* * Name: Wen-Chen Hu * * Dated: October 1, 1999 * ********************************************************************* ********************************************************************* * The Purpose of this Project * ********************************************************************* Demonstrate how to use Java language to download Web pages. ********************************************************************* * How to Use This Program robot * ********************************************************************* > javac net.java > java net url number 1. net : this class name 2. url : a seed URL 3. number: number of Web pages downloaded For example, > java net http://www.eng.auburn.edu/~wenchen/ 50 ********************************************************************* ********************************************************************/ import java.net.*; import java.io.*; import java.lang.*; /******************************************************************* * Main Program * *******************************************************************/ public class net { public static void main (String args[]) { // args[0]: seed URL // args[1]: number of Web sites downloaded String url = args[0]; downLoadHTML t = new downLoadHTML(); for (int i = 0; i < Integer.valueOf(args[1]).intValue(); i++) { t.loading (url, "work"); // work is a temporary workarea // start text processing on the work, // enter the extracted information into database, and // assign the url with a new URL . } } } class downLoadHTML { void loading (String url, String work) { //Open the URL for reading try { URL u = new URL(url); try { URLConnection uc = u.openConnection( ); // now turn the URLConnection into a DataInputStream DataInputStream theHTML = new DataInputStream (uc.getInputStream()); OutputStream to_file = new FileOutputStream (work); try { byte[] buffer = new byte[4096]; int bytes_read; while ((bytes_read = theHTML.read(buffer)) != -1) to_file.write (buffer, 0, bytes_read); to_file.close(); } // end try catch (Exception e) { System.err.println(e); } } // end try catch (Exception e) { System.err.println(e); } } // end try catch (MalformedURLException e) { System.err.println(e); } } }