java.net
public
final
class
java.net.URL
An instance of class URL specifies the location of a resource on the world
wide web as specified by RFC 1738.
Summary
Public Constructors
|
|
|
|
|
|
URL(String spec) |
|
|
|
|
|
|
URL(URL context, String spec) |
|
|
|
|
|
|
URL(URL context, String spec, URLStreamHandler handler) |
|
|
|
|
|
|
URL(String protocol, String host, String file) |
|
|
|
|
|
|
URL(String protocol, String host, int port, String file) |
|
|
|
|
|
|
URL(String protocol, String host, int port, String file, URLStreamHandler handler) |
Public Methods
Protected Methods
|
|
|
|
|
void |
set(String protocol, String host, int port, String authority, String userInfo, String path, String query, String ref) |
|
|
|
|
|
void |
set(String protocol, String host, int port, String file, String ref) |
clone,
equals,
finalize,
getClass,
hashCode,
notify,
notifyAll,
toString,
wait,
wait,
wait
Details
Public Constructors
public
URL(String spec)
Constructs a new URL instance by parsing the specification.
Parameters
spec
| java.lang.String a URL specification. |
public
URL(URL context, String spec)
Constructs a new URL by parsing the specification given by
spec
and using the context provided by
context
.
The protocol of the specification is obtained by parsing the
spec
string.
If the spec
does not specify a protocol:
- If the context is
null
, then a
MalformedURLException
.
- If the context is not
null
, then the protocol is
obtained from the context.
If the
spec
does specify a protocol:
- If the context is
null
, or specifies a different
protocol than the spec, the context is ignored.
- If the context is not
null
and specifies the same
protocol as the specification, the properties of the new URL
are obtained from the context.
Parameters
context
| java.net.URL URL to use as context. |
spec
| java.lang.String a URL specification. |
Constructs a new URL by parsing the specification given by
spec
and using the context provided by
context
.
If the handler argument is non-null, a security check is made to verify
that user-defined protocol handlers can be specified.
The protocol of the specification is obtained by parsing the
spec
string.
If the spec
does not specify a protocol:
- If the context is
null
, then a
MalformedURLException
.
- If the context is not
null
, then the protocol is
obtained from the context.
If the
spec
does specify a protocol:
- If the context is
null
, or specifies a different
protocol than the spec, the context is ignored.
- If the context is not
null
and specifies the same
protocol as the specification, the properties of the new URL
are obtained from the context.
Parameters
context
| java.net.URL URL to use as context. |
spec
| java.lang.String a URL specification. |
handler
| java.net.URLStreamHandler a URLStreamHandler. |
Constructs a new URL instance using the arguments provided.
Parameters
protocol
| String the protocol for the URL. |
host
| String the name of the host. |
file
| the name of the resource. |
public
URL(String protocol, String host, int port, String file)
Constructs a new URL instance using the arguments provided.
Parameters
protocol
| String the protocol for the URL. |
host
| String the name of the host. |
port
| int the port number. |
file
| String the name of the resource. |
Constructs a new URL instance using the arguments provided.
If the handler argument is non-null, a security check is made to verify
that user-defined protocol handlers can be specified.
Parameters
protocol
| the protocol for the URL. |
host
| the name of the host. |
port
| the port number. |
file
| the name of the resource. |
handler
| the stream handler that this URL uses. |
Public Methods
public
boolean
equals(Object o)
Compares the argument to the receiver, and returns true if they represent
the same URL. Two URLs are equal if they have the same file, host, port,
protocol, and reference components.
Parameters
o
| the object to compare with this URL. |
Returns
true
if the object is the same as this URL,
false
otherwise.
public
String
getAuthority()
Returns the authority component of this URL.
Returns
- the receiver's authority.
public
final
Object
getContent(Class[] types)
Returns an Object representing the resource referenced by this URL.
Parameters
types
| The list of acceptable content types |
Returns
- The object of the resource pointed by this URL, or null if the
content does not match a specified content type.
Throws
IOException
| If an error occurred obtaining the content.
|
public
final
Object
getContent()
Returns an Object representing the resource referenced by this URL.
Returns
- The object of the resource pointed by this URL.
Throws
IOException
| If an error occurred obtaining the content.
|
public
int
getDefaultPort()
Returns the default port for this URL as defined by the URLStreamHandler.
Returns
- the default port for this URL
public
String
getFile()
Returns the file component of this URL.
public
String
getHost()
Returns the host component of this URL.
public
String
getPath()
Returns the path component of this URL.
public
int
getPort()
Returns the port component of this URL.
public
String
getProtocol()
Returns the protocol component of this URL.
public
String
getQuery()
Returns the query component of this URL.
public
String
getRef()
Returns the reference component of this URL.
Returns
- the receiver's reference component.
public
String
getUserInfo()
Returns the user info component of this URL.
Returns
- the receiver's user info.
public
int
hashCode()
Returns a hash code for this URL object.
Returns
- the hashcode for hashtable indexing
Creates a connection to this URL using the appropriate ProtocolHandler.
Returns
- The connection to this URL.
Throws
IOException
| if the connection to the URL is not possible.
|
The method is the same as
openConnection()
except that it
uses the
proxy
to establish a connection to this URL using
appropriate ProtocolHandler.
Parameters
proxy
| the proxy which is used to make the connection |
Returns
- The connection to this URL.
public
final
InputStream
openStream()
Returns a stream for reading from this URL.
Returns
- a stream on the contents of the resource.
public
boolean
sameFile(URL otherURL)
Returns true if the receiver and the argument refer to the same file. All
components except the reference are compared.
Parameters
otherURL
| URL to compare against. |
Returns
- true if the same resource, false otherwise
public
static
synchronized
void
setURLStreamHandlerFactory(URLStreamHandlerFactory streamFactory)
Sets the URL Stream (protocol) handler factory. This method can be
invoked only once during an application's lifetime.
A security check is performed to verify that the current Policy allows
the stream handler factory to be set.
Parameters
streamFactory
| URLStreamHandlerFactory The factory to use for finding stream
handlers.
|
public
String
toExternalForm()
Create and return the String representation of this URL.
Returns
- the external representation of this URL.
public
String
toString()
Returns a string containing a concise, human-readable description of the
receiver.
Returns
- a printable representation for the receiver.
public
URI
toURI()
Creates a URI related with this URL
Returns
- a URI related to this URL
Protected Methods
protected
void
set(String protocol, String host, int port, String authority, String userInfo, String path, String query, String ref)
Sets the properties of this URL using the provided arguments. This method
is used both within this class and by the
URLStreamHandler
code.
Parameters
protocol
| the new protocol. |
host
| the new host name. |
port
| the new port number. |
authority
| the new authority. |
userInfo
| the new user info. |
path
| the new path component. |
query
| the new query. |
ref
| the new reference. |
protected
void
set(String protocol, String host, int port, String file, String ref)
Sets the properties of this URL using the provided arguments. This method
is used both within this class and by the
URLStreamHandler
code.
Parameters
protocol
| the new protocol. |
host
| the new host name. |
port
| the new port number. |
file
| the new file component. |
ref
| the new reference. |