public final class URL extends Object
Constructor and Description |
---|
URL(String spec)
Initializes a URL from a complete string specification such as
"http://www.urbanophile.com/arenn/".
|
URL(String protocol,
String host,
int port,
String file)
Constructs a URL and loads a protocol handler for the values passed as
arguments.
|
URL(String protocol,
String host,
int port,
String file,
URLStreamHandler ph)
This method initializes a new instance of
URL with the
specified protocol, host, port, and file. |
URL(String protocol,
String host,
String file)
Constructs a URL and loads a protocol handler for the values passed in
as arugments.
|
URL(URL context,
String spec) |
URL(URL context,
String spec,
URLStreamHandler ph)
Creates an URL from given arguments
This method parses a String representation of a URL within the
context of an existing URL.
|
Modifier and Type | Method and Description |
---|---|
static String |
decodeURL(String url)
Decode a URL encoded with '%##' notation.
|
static String |
encodeURL(String url,
boolean spaceToPlus)
Encode the URL using %## notation.
|
boolean |
equals(Object obj)
Test another URL for equality with this one.
|
String |
getAuthority()
Returns the authority of the URL
|
String |
getFile()
Returns the file portion of the URL.
|
String |
getHost()
Returns the host of the URL
|
String |
getPath()
Returns the path of the URL.
|
int |
getPort()
Returns the port number of this URL or -1 if the default port number is
being used.
|
String |
getProtocol()
Returns the protocol of the URL
|
String |
getQuery()
Returns the query of the URL.
|
String |
getRef()
Returns the ref (sometimes called the "# reference" or "anchor") portion
of the URL.
|
String |
getUserInfo()
Returns the user information of the URL.
|
int |
hashCode()
Returns a hashcode computed by the URLStreamHandler of this URL
|
URLConnection |
openConnection()
Returns a URLConnection object that represents a connection to the remote
object referred to by the URL.
|
InputStream |
openStream()
Opens a connection to this URL and returns an InputStream for reading
from that connection
|
protected void |
set(String protocol,
String host,
int port,
String file,
String ref)
Sets the specified fields of the URL.
|
protected void |
set(String protocol,
String host,
int port,
String authority,
String userInfo,
String path,
String query,
String ref)
Sets the specified fields of the URL.
|
String |
toExternalForm()
Returns a String representing this URL.
|
String |
toString()
Returns a String representing this URL.
|
public String url
public URL(String protocol, String host, int port, String file) throws MalformedURLException
protocol
- The protocol for this URL ("http", "ftp", etc)host
- The hostname or IP address to connect toport
- The port number to use, or -1 to use the protocol's
default portfile
- The "file" portion of the URL.MalformedURLException
- If a protocol handler cannot be loaded or
a parse error occurs.public URL(String protocol, String host, String file) throws MalformedURLException
protocol
- The protocol for this URL ("http", "ftp", etc)host
- The hostname or IP address for this URLfile
- The "file" portion of this URL.MalformedURLException
- If a protocol handler cannot be loaded or
a parse error occurs.public URL(String protocol, String host, int port, String file, URLStreamHandler ph) throws MalformedURLException
URL
with the
specified protocol, host, port, and file. Additionally, this method
allows the caller to specify a protocol handler to use instead of
the default. If this handler is specified, the caller must have
the "specifyStreamHandler" permission (see NetPermission
)
or a SecurityException
will be thrown.protocol
- The protocol for this URL ("http", "ftp", etc)host
- The hostname or IP address to connect toport
- The port number to use, or -1 to use the protocol's default
portfile
- The "file" portion of the URL.ph
- The protocol handler to use with this URL.MalformedURLException
- If no protocol handler can be loaded
for the specified protocol.SecurityException
- If the SecurityManager
exists
and does not allow the caller to specify its own protocol handler.public URL(String spec) throws MalformedURLException
spec
- The complete String representation of a URLMalformedURLException
- If a protocol handler cannot be found
or the URL cannot be parsedpublic URL(URL context, String spec) throws MalformedURLException
MalformedURLException
public URL(URL context, String spec, URLStreamHandler ph) throws MalformedURLException
Additionally, this method allows the caller to specify a protocol handler
to use instead of the default. If this handler is specified, the caller
must have the "specifyStreamHandler" permission
(see NetPermission
) or a SecurityException
will be thrown.
context
- The context in which to parse the specificationspec
- The string to parse as an URLph
- The stream handler for the URLMalformedURLException
- If a protocol handler cannot be found
or the URL cannot be parsedSecurityException
- If the SecurityManager
exists
and does not allow the caller to specify its own protocol handler.public boolean equals(Object obj)
public String getFile()
path[?query]
.
Returns the empty string if there is no file portion.public String getPath()
public String getAuthority()
public String getHost()
public int getPort()
#getDefaultPort()
public String getProtocol()
public String getRef()
public String getUserInfo()
public String getQuery()
public int hashCode()
public URLConnection openConnection() throws IOException
IOException
- If an error occurspublic final InputStream openStream() throws IOException
IOException
- If an error occursprotected void set(String protocol, String host, int port, String file, String ref)
parseURL()
method in that class. URLs are otherwise
constant.protocol
- The protocol name for this URLhost
- The hostname or IP address for this URLport
- The port number of this URLfile
- The "file" portion of this URL.ref
- The anchor portion of this URL.protected void set(String protocol, String host, int port, String authority, String userInfo, String path, String query, String ref)
public String toExternalForm()
public String toString()
public static String encodeURL(String url, boolean spaceToPlus)
url
- The unencoded URL.spaceToPlus
- true if you wish a space to be encoded as a '+', false to encode it as %20