Main.java 3.5KB

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