Class URL
- java.lang.Object
-
- java.net.URL
-
- All Implemented Interfaces:
- Serializable
public final class URL extends Object implements Serializable
ClassURL
represents a Uniform Resource Locator, a pointer to a "resource" on the World Wide Web. A resource can be something as simple as a file or a directory, or it can be a reference to a more complicated object, such as a query to a database or to a search engine. More information on the types of URLs and their formats can be found at:http://www.socs.uts.edu.au/MosaicDocs-old/url-primer.html
In general, a URL can be broken into several parts. The previous example of a URL indicates that the protocol to use is
http
(HyperText Transfer Protocol) and that the information resides on a host machine namedwww.socs.uts.edu.au
. The information on that host machine is named/MosaicDocs-old/url-primer.html
. The exact meaning of this name on the host machine is both protocol dependent and host dependent. The information normally resides in a file, but it could be generated on the fly. This component of the URL is called the pathcomponent.A URL can optionally specify a "port", which is the port number to which the TCP connection is made on the remote host machine. If the port is not specified, the default port for the protocol is used instead. For example, the default port for
http
is80
. An alternative port could be specified as:http://www.socs.uts.edu.au:80/MosaicDocs-old/url-primer.html
The syntax of
URL
is defined by RFC 2396: Uniform Resource Identifiers (URI): Generic Syntax, amended by RFC 2732: Format for Literal IPv6 Addresses in URLs. The Literal IPv6 address format also supports scope_ids. The syntax and usage of scope_ids is described here.A URL may have appended to it a "fragment", also known as a "ref" or a "reference". The fragment is indicated by the sharp sign character "#" followed by more characters. For example,
http://java.sun.com/index.html#chapter1
This fragment is not technically part of the URL. Rather, it indicates that after the specified resource is retrieved, the application is specifically interested in that part of the document that has the tag
chapter1
attached to it. The meaning of a tag is resource specific.An application can also specify a "relative URL", which contains only enough information to reach the resource relative to another URL. Relative URLs are frequently used within HTML pages. For example, if the contents of the URL:
contained within it the relative URL:http://java.sun.com/index.html
it would be a shorthand for:FAQ.html
http://java.sun.com/FAQ.html
The relative URL need not specify all the components of a URL. If the protocol, host name, or port number is missing, the value is inherited from the fully specified URL. The file component must be specified. The optional fragment is not inherited.
The URL class does not itself encode or decode any URL components according to the escaping mechanism defined in RFC2396. It is the responsibility of the caller to encode any fields, which need to be escaped prior to calling URL, and also to decode any escaped fields, that are returned from URL. Furthermore, because URL has no knowledge of URL escaping, it does not recognise equivalence between the encoded or decoded form of the same URL. For example, the two URLs:
http://foo.com/hello world/ and http://foo.com/hello%20world
would be considered not equal to each other.Note, the
URI
class does perform escaping of its component fields in certain circumstances. The recommended way to manage the encoding and decoding of URLs is to useURI
, and to convert between these two classes usingtoURI()
andURI.toURL()
.The
URLEncoder
andURLDecoder
classes can also be used, but only for HTML form encoding, which is not the same as the encoding scheme defined in RFC2396.- Since:
- JDK1.0
- See Also:
- Serialized Form
-
-
Constructor Summary
Constructor and Description URL(String spec)
Creates aURL
object from theString
representation.URL(String protocol, String host, int port, String file)
Creates aURL
object from the specifiedprotocol
,host
,port
number, andfile
.URL(String protocol, String host, int port, String file, URLStreamHandler handler)
Creates aURL
object from the specifiedprotocol
,host
,port
number,file
, andhandler
.URL(String protocol, String host, String file)
Creates a URL from the specifiedprotocol
name,host
name, andfile
name.URL(URL context, String spec)
Creates a URL by parsing the given spec within a specified context.URL(URL context, String spec, URLStreamHandler handler)
Creates a URL by parsing the given spec with the specified handler within a specified context.
-
-
Method Summary
Modifier and Type Method and Description boolean
equals(Object obj)
Compares this URL for equality with another object.String
getAuthority()
Gets the authority part of thisURL
.Object
getContent()
Gets the contents of this URL.Object
getContent(Class[] classes)
Gets the contents of this URL.int
getDefaultPort()
Gets the default port number of the protocol associated with thisURL
.String
getFile()
Gets the file name of thisURL
.String
getHost()
Gets the host name of thisURL
, if applicable.String
getPath()
Gets the path part of thisURL
.int
getPort()
Gets the port number of thisURL
.String
getProtocol()
Gets the protocol name of thisURL
.String
getQuery()
Gets the query part of thisURL
.String
getRef()
Gets the anchor (also known as the "reference") of thisURL
.String
getUserInfo()
Gets the userInfo part of thisURL
.int
hashCode()
Creates an integer suitable for hash table indexing.URLConnection
openConnection()
Returns aURLConnection
instance that represents a connection to the remote object referred to by theURL
.URLConnection
openConnection(Proxy proxy)
Same asopenConnection()
, except that the connection will be made through the specified proxy; Protocol handlers that do not support proxing will ignore the proxy parameter and make a normal connection.InputStream
openStream()
Opens a connection to thisURL
and returns anInputStream
for reading from that connection.boolean
sameFile(URL other)
Compares two URLs, excluding the fragment component.protected void
set(String protocol, String host, int port, String file, String ref)
Sets the 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 8 fields of the URL.static void
setURLStreamHandlerFactory(URLStreamHandlerFactory fac)
Sets an application'sURLStreamHandlerFactory
.String
toExternalForm()
Constructs a string representation of thisURL
.String
toString()
Constructs a string representation of thisURL
.URI
toURI()
'Dev. 자바 > API 및 이론' 카테고리의 다른 글
자바 예외(Exception)의 계층구조 및 자주 보이는 예외 설명 (0) | 2012.09.17 |
---|---|
[JAVA API] java.net.InetAddress (0) | 2012.09.06 |
[JAVA API] java.lang.Thread (0) | 2012.09.03 |
[JAVA API] java.util.HashSet<E> (0) | 2012.09.01 |
[JAVA API] java.io.FileWriter (0) | 2012.08.08 |