public final class URL extends Object implements Serializable
http://username:password@host:8080/directory/file?query#ref
:
Component | Example value | Also known as |
---|---|---|
Protocol | http | scheme |
Authority | username:password@host:8080 | |
User Info | username:password | |
Host | host | |
Port | 8080 | |
File | /directory/file?query | |
Path | /directory/file | |
Query | query | |
Ref | ref | fragment |
Jar files
from the
filesystemMalformedURLException
. Applications may install handlers for other
schemes using setURLStreamHandlerFactory(java.net.URLStreamHandlerFactory)
or with the java.protocol.handler.pkgs
system property.
The URI
class can be used to manipulate URLs of any protocol.
Constructor and Description |
---|
URL(String spec)
Creates a new URL instance by parsing
spec . |
URL(String protocol,
String host,
int port,
String file)
Creates a new URL of the given component parts.
|
URL(String protocol,
String host,
int port,
String file,
URLStreamHandler handler)
Creates a new URL of the given component parts.
|
URL(String protocol,
String host,
String file)
Creates a new URL of the given component parts.
|
URL(URL context,
String spec)
Creates a new URL by resolving
spec relative to context . |
URL(URL context,
String spec,
URLStreamHandler handler)
Creates a new URL by resolving
spec relative to context . |
Modifier and Type | Method and Description |
---|---|
boolean |
equals(Object o)
Returns true if this URL equals
o . |
String |
getAuthority()
Returns the authority part of this URL, or null if this URL has no
authority.
|
Object |
getContent()
Returns the content of the resource which is referred by this URL.
|
Object |
getContent(Class[] types)
Equivalent to
openConnection().getContent(types) . |
int |
getDefaultPort()
Returns the default port number of the protocol used by this URL.
|
int |
getEffectivePort() |
String |
getFile()
Returns the file of this URL.
|
String |
getHost()
Returns the host name or IP address of this URL.
|
String |
getPath()
Returns the path part of this URL.
|
int |
getPort()
Returns the port number of this URL or
-1 if this URL has no
explicit port. |
String |
getProtocol()
Returns the protocol of this URL like "http" or "file".
|
String |
getQuery()
Returns the query part of this URL, or null if this URL has no query.
|
String |
getRef()
Returns the value of the reference part of this URL, or null if this URL
has no reference part.
|
String |
getUserInfo()
Returns the user info of this URL, or null if this URL has no user info.
|
int |
hashCode()
Returns an integer hash code for this object.
|
URLConnection |
openConnection()
Returns a new connection to the resource referred to by this URL.
|
URLConnection |
openConnection(Proxy proxy)
Returns a new connection to the resource referred to by this URL.
|
InputStream |
openStream()
Equivalent to
openConnection().getInputStream(types) . |
boolean |
sameFile(URL otherURL)
Returns true if this URL refers to the same resource as
otherURL . |
protected void |
set(String protocol,
String host,
int port,
String file,
String ref)
Sets the properties of this URL using the provided arguments.
|
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.
|
static void |
setURLStreamHandlerFactory(URLStreamHandlerFactory factory)
Sets the stream handler factory for this VM.
|
String |
toExternalForm()
Returns a string containing a concise, human-readable representation of
this URL.
|
String |
toString()
Returns a string containing a concise, human-readable representation of
this URL.
|
URI |
toURI()
Returns the URI equivalent to this URL.
|
URI |
toURILenient()
Encodes this URL to the equivalent URI after escaping characters that are
not permitted by URI.
|
public URL(String spec) throws MalformedURLException
spec
.MalformedURLException
- if spec
could not be parsed as a
URL.public URL(URL context, String spec) throws MalformedURLException
spec
relative to context
.context
- the URL to which spec
is relative, or null for
no context in which case spec
must be an absolute URL.MalformedURLException
- if spec
could not be parsed as a
URL or has an unsupported protocol.public URL(URL context, String spec, URLStreamHandler handler) throws MalformedURLException
spec
relative to context
.context
- the URL to which spec
is relative, or null for
no context in which case spec
must be an absolute URL.handler
- the stream handler for this URL, or null for the
protocol's default stream handler.MalformedURLException
- if the given string spec
could not
be parsed as a URL or an invalid protocol has been found.public URL(String protocol, String host, String file) throws MalformedURLException
MalformedURLException
- if the combination of all arguments do not
represent a valid URL or if the protocol is invalid.public URL(String protocol, String host, int port, String file) throws MalformedURLException
host
- the host name or IP address of the new URL.port
- the port, or -1
for the protocol's default port.file
- the name of the resource.MalformedURLException
- if the combination of all arguments do not
represent a valid URL or if the protocol is invalid.public URL(String protocol, String host, int port, String file, URLStreamHandler handler) throws MalformedURLException
host
- the host name or IP address of the new URL.port
- the port, or -1
for the protocol's default port.file
- the name of the resource.handler
- the stream handler for this URL, or null for the
protocol's default stream handler.MalformedURLException
- if the combination of all arguments do not
represent a valid URL or if the protocol is invalid.public static void setURLStreamHandlerFactory(URLStreamHandlerFactory factory)
Error
- if a URLStreamHandlerFactory has already been installed
for the current VM.protected void set(String protocol, String host, int port, String file, String ref)
URLStreamHandler
can use this method to set fields of the
existing URL instance. A URL is generally constant.public boolean equals(Object o)
o
. URLs are equal if they have
the same protocol, host, port, file, and reference.
Some implementations of URL.equals() resolve host names over the network. This is problematic:
Map
and Set
expect that equals
and hashCode
will return quickly.
By violating this assumption, this method posed potential performance
problems.
This problem is fixed in Android 4.0 (Ice Cream Sandwich). In that release, URLs are only equal if their host names are equal (ignoring case).
equals
in class Object
o
- the object to compare this instance with.true
if the specified object is equal to this Object
; false
otherwise.Object.hashCode()
public boolean sameFile(URL otherURL)
otherURL
.
All URL components except the reference field are compared.public int hashCode()
Object
Object.equals(java.lang.Object)
returns true
must return
the same hash code value. This means that subclasses of Object
usually override both methods or neither method.
Note that hash values must not change over time unless information used in equals comparisons also changes.
See Writing a correct
hashCode
method
if you intend implementing your own hashCode
method.
hashCode
in class Object
Object.equals(java.lang.Object)
public final Object getContent() throws IOException
InputStream
, or null if the content type
of the response is unknown.IOException
public final Object getContent(Class[] types) throws IOException
openConnection().getContent(types)
.IOException
public final InputStream openStream() throws IOException
openConnection().getInputStream(types)
.IOException
public URLConnection openConnection() throws IOException
IOException
- if an error occurs while opening the connection.public URLConnection openConnection(Proxy proxy) throws IOException
proxy
- the proxy through which the connection will be established.IOException
- if an I/O error occurs while opening the connection.IllegalArgumentException
- if the argument proxy is null or of is
an invalid type.UnsupportedOperationException
- if the protocol handler does not
support opening connections through proxies.public URI toURI() throws URISyntaxException
URISyntaxException
- if this URL cannot be converted into a URI.public URI toURILenient() throws URISyntaxException
URISyntaxException
public String toString()
toExternalForm()
.public String toExternalForm()
public int getEffectivePort()
public String getProtocol()
public String getAuthority()
public String getUserInfo()
public String getHost()
public int getPort()
-1
if this URL has no
explicit port.
If this URL has no explicit port, connections opened using this URL
will use its default port
.
public int getDefaultPort()
URLStreamHandler
,
-1
will be returned.URLStreamHandler.getDefaultPort()
public String getFile()
public String getPath()
public String getQuery()
public String getRef()
protected void set(String protocol, String host, int port, String authority, String userInfo, String path, String query, String ref)
URLStreamHandler
can use this method to set fields of the
existing URL instance. A URL is generally constant.