Package java.net
Class URL
java.lang.Object
java.net.URL
- All Implemented Interfaces:
Serializable
public final class URL extends Object implements Serializable
A Uniform Resource Locator that identifies the location of an Internet
resource as specified by RFC
1738.
Parts of a URL
A URL is composed of many parts. This class can both parse URL strings into parts and compose URL strings from parts. For example, consider the parts of this URL: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 |
Supported Protocols
This class may be used to construct URLs with the following protocols:- file: read files from the local filesystem.
- ftp: File Transfer Protocol
- http: Hypertext Transfer Protocol
- https: HTTP over TLS
- jar: read
Jar filesfrom the filesystem
MalformedURLException. 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.
- See Also:
- Serialized Form
-
Constructor Summary
Constructors Constructor Description URL(String spec)Creates a new URL instance by parsingspec.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 resolvingspecrelative tocontext.URL(URL context, String spec, URLStreamHandler handler)Creates a new URL by resolvingspecrelative tocontext. -
Method Summary
Modifier and Type Method Description booleanequals(Object o)Returns true if this URL equalso.StringgetAuthority()Returns the authority part of this URL, or null if this URL has no authority.ObjectgetContent()Returns the content of the resource which is referred by this URL.ObjectgetContent(Class[] types)Equivalent toopenConnection().getContent(types).intgetDefaultPort()Returns the default port number of the protocol used by this URL.intgetEffectivePort()StringgetFile()Returns the file of this URL.StringgetHost()Returns the host name or IP address of this URL.StringgetPath()Returns the path part of this URL.intgetPort()Returns the port number of this URL or-1if this URL has no explicit port.StringgetProtocol()Returns the protocol of this URL like "http" or "file".StringgetQuery()Returns the query part of this URL, or null if this URL has no query.StringgetRef()Returns the value of the reference part of this URL, or null if this URL has no reference part.StringgetUserInfo()Returns the user info of this URL, or null if this URL has no user info.inthashCode()Returns an integer hash code for this object.URLConnectionopenConnection()Returns a new connection to the resource referred to by this URL.URLConnectionopenConnection(Proxy proxy)Returns a new connection to the resource referred to by this URL.InputStreamopenStream()Equivalent toopenConnection().getInputStream(types).booleansameFile(URL otherURL)Returns true if this URL refers to the same resource asotherURL.protected voidset(String protocol, String host, int port, String file, String ref)Sets the properties of this URL using the provided arguments.protected voidset(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 voidsetURLStreamHandlerFactory(URLStreamHandlerFactory factory)Sets the stream handler factory for this VM.StringtoExternalForm()Returns a string containing a concise, human-readable representation of this URL.StringtoString()Returns a string containing a concise, human-readable representation of this URL.URItoURI()Returns the URI equivalent to this URL.URItoURILenient()Encodes this URL to the equivalent URI after escaping characters that are not permitted by URI.
-
Constructor Details
-
URL
Creates a new URL instance by parsingspec.- Throws:
MalformedURLException- ifspeccould not be parsed as a URL.
-
URL
Creates a new URL by resolvingspecrelative tocontext.- Parameters:
context- the URL to whichspecis relative, or null for no context in which casespecmust be an absolute URL.- Throws:
MalformedURLException- ifspeccould not be parsed as a URL or has an unsupported protocol.
-
URL
Creates a new URL by resolvingspecrelative tocontext.- Parameters:
context- the URL to whichspecis relative, or null for no context in which casespecmust be an absolute URL.handler- the stream handler for this URL, or null for the protocol's default stream handler.- Throws:
MalformedURLException- if the given stringspeccould not be parsed as a URL or an invalid protocol has been found.
-
URL
Creates a new URL of the given component parts. The URL uses the protocol's default port.- Throws:
MalformedURLException- if the combination of all arguments do not represent a valid URL or if the protocol is invalid.
-
URL
Creates a new URL of the given component parts. The URL uses the protocol's default port.- Parameters:
host- the host name or IP address of the new URL.port- the port, or-1for the protocol's default port.file- the name of the resource.- Throws:
MalformedURLException- if the combination of all arguments do not represent a valid URL or if the protocol is invalid.
-
URL
public URL(String protocol, String host, int port, String file, URLStreamHandler handler) throws MalformedURLExceptionCreates a new URL of the given component parts. The URL uses the protocol's default port.- Parameters:
host- the host name or IP address of the new URL.port- the port, or-1for 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.- Throws:
MalformedURLException- if the combination of all arguments do not represent a valid URL or if the protocol is invalid.
-
-
Method Details
-
setURLStreamHandlerFactory
Sets the stream handler factory for this VM.- Throws:
Error- if a URLStreamHandlerFactory has already been installed for the current VM.
-
set
Sets the properties of this URL using the provided arguments. Only aURLStreamHandlercan use this method to set fields of the existing URL instance. A URL is generally constant. -
equals
Returns true if this URL equalso. URLs are equal if they have the same protocol, host, port, file, and reference.Network I/O Warning
Some implementations of URL.equals() resolve host names over the network. This is problematic:
- The network may be slow. Many classes, including
core collections like
MapandSetexpect thatequalsandhashCodewill return quickly. By violating this assumption, this method posed potential performance problems. - Equal IP addresses do not imply equal content. Virtual hosting permits unrelated sites to share an IP address. This method could report two otherwise unrelated URLs to be equal because they're hosted on the same server.
- The network many not be available. Two URLs could be equal when a network is available and unequal otherwise.
- The network may change. The IP address for a given host name varies by network and over time. This is problematic for mobile devices. Two URLs could be equal on some networks and unequal on others.
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).
- Overrides:
equalsin classObject- Parameters:
o- the object to compare this instance with.- Returns:
trueif the specified object is equal to thisObject;falseotherwise.- See Also:
Object.hashCode()
- The network may be slow. Many classes, including
core collections like
-
sameFile
Returns true if this URL refers to the same resource asotherURL. All URL components except the reference field are compared. -
hashCode
public int hashCode()Description copied from class:ObjectReturns an integer hash code for this object. By contract, any two objects for whichObject.equals(java.lang.Object)returnstruemust return the same hash code value. This means that subclasses ofObjectusually 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
hashCodemethod if you intend implementing your ownhashCodemethod.- Overrides:
hashCodein classObject- Returns:
- this object's hash code.
- See Also:
Object.equals(java.lang.Object)
-
getContent
Returns the content of the resource which is referred by this URL. By default this returns anInputStream, or null if the content type of the response is unknown.- Throws:
IOException
-
getContent
Equivalent toopenConnection().getContent(types).- Throws:
IOException
-
openStream
Equivalent toopenConnection().getInputStream(types).- Throws:
IOException
-
openConnection
Returns a new connection to the resource referred to by this URL.- Throws:
IOException- if an error occurs while opening the connection.
-
openConnection
Returns a new connection to the resource referred to by this URL.- Parameters:
proxy- the proxy through which the connection will be established.- Throws:
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.
-
toURI
Returns the URI equivalent to this URL.- Throws:
URISyntaxException- if this URL cannot be converted into a URI.
-
toURILenient
Encodes this URL to the equivalent URI after escaping characters that are not permitted by URI.- Throws:
URISyntaxException
-
toString
Returns a string containing a concise, human-readable representation of this URL. The returned string is the same as the result of the methodtoExternalForm(). -
toExternalForm
Returns a string containing a concise, human-readable representation of this URL. -
getEffectivePort
public int getEffectivePort() -
getProtocol
Returns the protocol of this URL like "http" or "file". This is also known as the scheme. The returned string is lower case. -
getAuthority
Returns the authority part of this URL, or null if this URL has no authority. -
getUserInfo
Returns the user info of this URL, or null if this URL has no user info. -
getHost
Returns the host name or IP address of this URL. -
getPort
public int getPort()Returns the port number of this URL or-1if this URL has no explicit port.If this URL has no explicit port, connections opened using this URL will use its
default port. -
getDefaultPort
public int getDefaultPort()Returns the default port number of the protocol used by this URL. If no default port is defined by the protocol or theURLStreamHandler,-1will be returned.- See Also:
URLStreamHandler.getDefaultPort()
-
getFile
Returns the file of this URL. -
getPath
Returns the path part of this URL. -
getQuery
Returns the query part of this URL, or null if this URL has no query. -
getRef
Returns the value of the reference part of this URL, or null if this URL has no reference part. This is also known as the fragment. -
set
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. Only aURLStreamHandlercan use this method to set fields of the existing URL instance. A URL is generally constant.
-