Browse Source

url fetch

Lewis Dominguez 6 years ago
parent
commit
cfdfda3b8d
3 changed files with 76 additions and 4 deletions
  1. BIN
      .DS_Store
  2. 76
    4
      src/main/java/Main.java
  3. BIN
      target/classes/Main.class

BIN
.DS_Store View File


+ 76
- 4
src/main/java/Main.java View File

@@ -1,7 +1,79 @@
1
-public class Main {
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
+ public class Main {
10
+
2 11
     public static void main(String[] args) {
3
-        //ask for a url
4
-        // fetch the url
5
-        // print the url
12
+        String url;    // The url from the command line or from user input.
13
+        String urlLC;  // The url transformed to lower case.
14
+        if (args.length == 0) {
15
+            Scanner stdin = new Scanner(System.in);
16
+            System.out.print("Enter a url: ");
17
+            url = stdin.nextLine();
18
+        }
19
+        else {
20
+            url = args[0];
21
+        }
22
+        urlLC = url.toLowerCase();
23
+        if ( ! (urlLC.startsWith("http://") || urlLC.startsWith("ftp://") ||
24
+                urlLC.startsWith("file://"))) {
25
+            System.out.println("Using: " + url);
26
+        }
27
+        System.out.println();
28
+        try {
29
+            readTextFromURL(url);
30
+        }
31
+        catch (IOException e) {
32
+            System.out.println("\n*** Sorry, an error has occurred ***\n");
33
+            System.out.println(e);
34
+            System.out.println();
35
+        }
6 36
     }
37
+
38
+
39
+    static void readTextFromURL( String urlString ) throws IOException {
40
+
41
+        /* Open a connection to the URL, and get an input stream
42
+           for reading data from the URL. */
43
+
44
+        URL url = new URL(urlString);
45
+        URLConnection connection = url.openConnection();
46
+        InputStream urlData = connection.getInputStream();
47
+
48
+        /* Check that the content is some type of text.  Note: If
49
+           getContentType() method were called before getting the input
50
+           stream, it is possible for contentType to be null only because
51
+           no connection can be made.  The getInputStream() method will
52
+           throw an error if no connection can be made. */
53
+
54
+        String contentType = connection.getContentType();
55
+        System.out.println("Stream opened with content type: " + contentType);
56
+        System.out.println();
57
+        if (contentType == null || contentType.startsWith("text") == false)
58
+            throw new IOException("URL does not seem to refer to a text file.");
59
+        System.out.println("Fetching context from " + urlString + " ...");
60
+        System.out.println();
61
+
62
+        /* Copy lines of text from the input stream to the screen, until
63
+           end-of-file is encountered  (or an error occurs). */
64
+
65
+        BufferedReader in;  // For reading from the connection's input stream.
66
+        in = new BufferedReader( new InputStreamReader(urlData) );
67
+
68
+        while (true) {
69
+            String line = in.readLine();
70
+            if (line == null)
71
+                break;
72
+            System.out.println(line);
73
+        }
74
+        in.close();
75
+
76
+    }
77
+
7 78
 }
79
+

BIN
target/classes/Main.class View File