URL

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:
ComponentExample valueAlso known as
Protocolhttpscheme
Authorityusername:password@host:8080
User Infousername:password
Hosthost
Port8080
File/directory/file?query
Path/directory/file
Queryquery
Refreffragment

Supported Protocols

This class may be used to construct URLs with the following protocols: In general, attempts to create URLs with any other protocol will fail with a MalformedURLException. Applications may install handlers for other schemes using setURLStreamHandlerFactory(URLStreamHandlerFactory) or with the java.protocol.handler.pkgs system property.

The URI class can be used to manipulate URLs of any protocol.

Public Constructor Summary

URL(String spec)
Creates a new URL instance by parsing spec.
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.
URL(String protocol, String host, String file)
Creates a new URL of the given component parts.
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.

Public Method Summary

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.
final Object
getContent()
Returns the content of the resource which is referred by this URL.
final Object
getContent(Class[] types)
Equivalent to openConnection().getContent(types).
int
getDefaultPort()
Returns the default port number of the protocol used by this URL.
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(Proxy proxy)
Returns a new connection to the resource referred to by this URL.
URLConnection
openConnection()
Returns a new connection to the resource referred to by this URL.
final InputStream
openStream()
Equivalent to openConnection().getInputStream(types).
boolean
sameFile(URL otherURL)
Returns true if this URL refers to the same resource as otherURL.
synchronized 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.

Protected Method Summary

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.
void
set(String protocol, String host, int port, String file, String ref)
Sets the properties of this URL using the provided arguments.

Inherited Method Summary