|
@@ -1,7 +1,94 @@
|
|
1
|
+import java.net.URL;
|
|
2
|
+import java.net.URLConnection;
|
|
3
|
+import java.util.Scanner;
|
|
4
|
+import java.io.BufferedReader;
|
|
5
|
+import java.io.IOException;
|
|
6
|
+import java.io.InputStream;
|
|
7
|
+import java.io.InputStreamReader;
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+/**
|
|
11
|
+ * This short program demonstrates the URL and URLConnection classes by
|
|
12
|
+ * attempting to open a connection to a URL and read text from it. The url
|
|
13
|
+ * can be specified on the command line. If no command line argument is
|
|
14
|
+ * provided, the user is prompted for input. It can be a complete url, including
|
|
15
|
+ * the "protocol" at the beginning ("http://", "ftp://", or "file://"). If
|
|
16
|
+ * it does not start with one of these protocols, "http://" is added to the
|
|
17
|
+ * beginning of the input line. If an error occurs while trying to fetch the data,
|
|
18
|
+ * a message is output. Otherwise, the text from the URL is copied to the screen.
|
|
19
|
+ */
|
|
20
|
+
|
1
|
21
|
public class Main {
|
|
22
|
+
|
2
|
23
|
public static void main(String[] args) {
|
3
|
|
- //ask for a url
|
4
|
|
- // fetch the url
|
5
|
|
- // print the url
|
|
24
|
+ String url; // The url from the command line or from user input.
|
|
25
|
+ String urlLC; // The url transformed to lower case.
|
|
26
|
+ if (args.length == 0) {
|
|
27
|
+ Scanner stdin = new Scanner(System.in);
|
|
28
|
+ System.out.print("Enter a url: ");
|
|
29
|
+ url = stdin.nextLine();
|
|
30
|
+ }
|
|
31
|
+ else {
|
|
32
|
+ url = args[0];
|
|
33
|
+ }
|
|
34
|
+ urlLC = url.toLowerCase();
|
|
35
|
+ if ( ! (urlLC.startsWith("http://") || urlLC.startsWith("ftp://") ||
|
|
36
|
+ urlLC.startsWith("file://"))) {
|
|
37
|
+ System.out.println("Using: " + url);
|
|
38
|
+ }
|
|
39
|
+ System.out.println();
|
|
40
|
+ try {
|
|
41
|
+ readTextFromURL(url);
|
|
42
|
+ }
|
|
43
|
+ catch (IOException e) {
|
|
44
|
+ System.out.println("\n*** Sorry, an error has occurred ***\n");
|
|
45
|
+ System.out.println(e);
|
|
46
|
+ System.out.println();
|
|
47
|
+ }
|
6
|
48
|
}
|
7
|
|
-}
|
|
49
|
+
|
|
50
|
+ /**
|
|
51
|
+ * This subroutine attempts to copy text from the specified URL onto the screen.
|
|
52
|
+ * Any error must be handled by the caller of this subroutine.
|
|
53
|
+ * @param urlString contains the URL in text form
|
|
54
|
+ */
|
|
55
|
+ static void readTextFromURL( String urlString ) throws IOException {
|
|
56
|
+
|
|
57
|
+ /* Open a connection to the URL, and get an input stream
|
|
58
|
+ for reading data from the URL. */
|
|
59
|
+
|
|
60
|
+ URL url = new URL(urlString);
|
|
61
|
+ URLConnection connection = url.openConnection();
|
|
62
|
+ InputStream urlData = connection.getInputStream();
|
|
63
|
+
|
|
64
|
+ /* Check that the content is some type of text. Note: If
|
|
65
|
+ getContentType() method were called before getting the input
|
|
66
|
+ stream, it is possible for contentType to be null only because
|
|
67
|
+ no connection can be made. The getInputStream() method will
|
|
68
|
+ throw an error if no connection can be made. */
|
|
69
|
+
|
|
70
|
+ String contentType = connection.getContentType();
|
|
71
|
+ System.out.println("Stream opened with content type: " + contentType);
|
|
72
|
+ System.out.println();
|
|
73
|
+ if (contentType == null || contentType.startsWith("text") == false)
|
|
74
|
+ throw new IOException("URL does not seem to refer to a text file.");
|
|
75
|
+ System.out.println("Fetching context from " + urlString + " ...");
|
|
76
|
+ System.out.println();
|
|
77
|
+
|
|
78
|
+ /* Copy lines of text from the input stream to the screen, until
|
|
79
|
+ end-of-file is encountered (or an error occurs). */
|
|
80
|
+
|
|
81
|
+ BufferedReader in; // For reading from the connection's input stream.
|
|
82
|
+ in = new BufferedReader( new InputStreamReader(urlData) );
|
|
83
|
+
|
|
84
|
+ while (true) {
|
|
85
|
+ String line = in.readLine();
|
|
86
|
+ if (line == null)
|
|
87
|
+ break;
|
|
88
|
+ System.out.println(line);
|
|
89
|
+ }
|
|
90
|
+ in.close();
|
|
91
|
+
|
|
92
|
+ } // end readTextFromURL()
|
|
93
|
+
|
|
94
|
+} // end class FetchURL
|