/home/joey/NetBeansProjects/comp173/src/webpagedisplay/WebPageGrabber.java
 1 package webpagedisplay;
 2 
 3 import java.io.BufferedReader;
 4 import java.io.IOException;
 5 import java.io.InputStreamReader;
 6 import java.net.MalformedURLException;
 7 import java.net.URL;
 8 /**
 9  *Accepts URLs as CLI arguements, laods the page from the server, then prints it out.
10  * Meant to be ran from CLI.
11  * @author Chris Jarrett
12  * 
13  */
14 public class WebPageGrabber {
15     /**
16      *The main for this script
17      * @param accepts URLs as arguements
18      */
19     public static void main(String[] args) {
20         //make sure atleast one url ws entered
21         if(args.length == 0) {
22             System.err.println("Command line arguements needed.");
23             System.exit(-1);
24         }
25         //go through the urls entered
26         for (String s: args) {
27             //create a handle for the url
28             URL urlhandle = null;
29             try {
30                 urlhandle = new URL(s);
31             } catch (MalformedURLException ex) {
32                 System.err.println("invalid url.");
33                 System.err.flush();
34                 System.exit(-1);
35             }
36             //create the read buffer
37             BufferedReader in = null;
38             try {
39                 in = new BufferedReader(new InputStreamReader(urlhandle.openStream()));
40             } catch (IOException ex) {
41                 System.err.println("Could not contact url.");
42                 System.err.flush();
43                 System.exit(-1);
44             }
45             //read the buffer and print it
46             String inputLine;
47             try {
48                 while ((inputLine = in.readLine()) != null) {
49                     System.out.println(inputLine);
50                 }
51             } catch (IOException ex) {
52                 System.err.println("No content found.");
53                 System.err.flush();
54                 System.exit(-1);
55             }
56             //close the buffer and we're done
57             try {
58                 in.close();
59             } catch (IOException ex) {
60                 System.err.println("Could not close the url handler.");
61                 System.err.flush();
62                 System.exit(-1);
63             }
64 
65         }
66     }
67 }
68 
69