8.2 URL ObjectsThe URL class provides higher-level access to data than sockets do. A URL object encapsulates a Uniform Resource Locator (URL) specification. Once you have created a URL object, you can use it to access the data in the location specified by the URL. A URL allows you to access the data without needing to be aware of the details of the protocol being used, such as HTTP or FTP. For some types of data, a URL object provides a way to get the data already encapsulated in an appropriate kind of object. For example, a URL can provide JPEG data encapsulated in an ImageProducer object or text data encapsulated in a String object. You can create a URL object as follows:
try { URL js = new URL("http://www.javasoft.com/index.html"); }catch (MalformedURLException e) { return; } This type of URL specification is called an absolute URL specification because it completely specifies where to find the data. It is also possible to create a URL object with a relative URL specification that is combined with an absolute specification:
try { URL jdk = new URL(js,"java.sun.com/products/JDK/index.html"); }catch (MalformedURLException e) { return; } In this example, the URL created in the previous example is combined with a relative URL specification that doesn't specify a network address or a root directory. The constructor can only combine the specifications if the protocol for both specifications is the same. If no protocol is specified, HTTP is assumed. The rules for combining the specifications depend on the protocol. In fact, the syntax rules for the portion of the URL after the protocol and up to an optional # depend on the protocol. If there's a # in the URL specification, the portion of the spec after the # is considered reference information that specifies a location within a file. Once you have created a URL object, you can use the following access methods to get the information that the URL object encapsulates:
If you want to determine if two URL objects refer to the same file, you can use the sameFile(URL) method, which compares all the information in two URL objects except the reference information. The highest level of functionality available from a URL object is provided by the getContent() method. The getContent() method tries to determine the type of data in the file specified by the URL, and then it returns the contents of the file encapsulated in an appropriate object for that type of data. For example, if the file contains GIF data, getContent() returns an ImageProducer object. If the type of data is not explicitly specified, getContent() tries to guess the type from the filename extension and possibly also from the contents of the file. The data type names that Java uses conform to the naming scheme for MIME data types, as do the filename extensions that are recognized. The data types that correspond to various file extensions are shown in Table 8.2.
If the filename does not end with a recognized extension, the first few bytes of the file are examined. If the first few bytes match the signature of a known type, the file is assumed to be of that type. Table 8.3 shows the byte combinations that are recognized.
If you want to access the raw contents of a file instead of getting it encapsulated in an object, you can call the openStream() method of a URL. The openStream() method returns a reference to an InputStream object that you can use to read the file. URLConnection ObjectsAfter a URL object has parsed its specification, it actually creates a URLConnection object that is responsible for the protocol that it uses. The URLConnection is also responsible for determining the type of data in the file. The object is an instance of a subclass of URLConnection that is specific to the protocol specified by the URL object. As of Java 1.1, the java.net package includes the HttpURLConnection class for the HTTP protocol. The URLConnection object for a URL provides complete control over the downloading of data from that URL. Unfortunately, the functionality of URLConnection is quite complex and goes beyond the scope of this book. For a detailed explanation of URLConnection, see Java Network Programming by Eliotte Rusty Harold, published by O'Reilly & Associates. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|