Using a URLConnection using Java
The URLConnection class establishes a connection to a URL. The openStream( ) method of URL we used in Example 5-1 is merely a convenience method that creates a URLConnection object and calls its getInputStream( ) method. By using a URLConnection object directly instead of relying on openStream( ), you have much more control over the process of downloading the contents of a URL.
Example 5-2 is a simple program that shows how to use a URLConnection to obtain the content type, size, last-modified date, and other information about the resource referred to by a URL. If the URL uses the HTTP protocol, it also demonstrates how to use theHttpURLConnection subclass to obtain additional information about the connection.
Note the use of the java.util.Date class to convert a timestamp (a long that contains the number of milliseconds since midnight, January 1, 1970 GMT) to a human-readable date and time string.