Name
URL
Synopsis
This
class represents a uniform resource locator and allows the data
referred to by the URL to be downloaded. A URL can be specified as a
single string or with separate protocol, host, port, and file
specifications. Relative URLs can also be specified with a
String and the URL object to
which it is relative. getFile(
)
, getHost( ),
getProtocol( ) and related methods return the
various portions of the URL specified by a URL
object. sameFile( )
determines whether a
URL object refers to the same file as this one.
getDefaultPort(
)
returns the default port number for the
protocol of the URL object; it may differ from the
number returned by getPort(
)
. Use
openConnection( ) to obtain a
URLConnection object with which you can download
the content of the URL. In Java 5.0, you can explicitly specify a
Proxy object through which the connection should
be opened. For simple cases, however, the URL
class defines shortcut methods that create and invoke methods on a
URLConnection internally. getContent(
)
downloads the URL data and parses it
into an appropriate Java object (such as a string or image) if an
appropriate ContentHandler can be found. In Java
1.3 and later, you can pass an array of Class
objects that specify the type of objects that you are willing to
accept as the return value of this method. If you wish to parse the
URL content yourself, call openStream(
)
to obtain an
InputStream from which you can read the data.
Figure 12-27. java.net.URL
public final class ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access