The java.net.URL
class represents a URL. There are
constructors to create new URLs and methods to parse the different
parts of a URL. However the heart of the class are the methods that
allow you to get an InputStream
from a URL so you can read data
from a server.
The URL
class is closely tied to protocol and
content handlers. The objective is to separate the data being
downloaded from the the protocol used to download it. The protocol
handler is responsible for communicating with the server, that is
moving bytes from the server to the client. It handles any
necessary negotiation with the server and any headers. Its job is
to return only the actual bytes of the data or file requested. The
content handler takes those bytes and translates them into some
kind of Java object such as an InputStream
or
ImageProducer
.
When you construct a URL
object, Java looks for a
protocol handler that understands the protocol part of the URL such
as "http" or "mailto". If no such handler is found, the constructor
throws a MalformedURLException
. The exact protocols
that are supported vary from implementation to implementation
though http and file are supported pretty much everywhere. Sun's
JDK 1.1 understands ten:
The last five are custom protocols defined by Sun and used internally by the JDK and HotJava.