Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

Java URL Handling

Java URL Handling

URL (Uniform Resource Locator) is sometimes also colloquially called a web address. It represents a resource on the Internet, such as a web page or an FTP address.

In this chapter we will introduce how Java handles URLs. A URL can be divided into the following parts.


protocol://host:port/path?query#fragment

The protocol can be HTTP, HTTPS, FTP, or File; port is the port number; path is the file path and file name.

An example URL for the HTTP protocol is as follows:


http://www.runoob.com/index.html?language=cn#j2se

URL parsing:


URL Class Methods

The URL class is defined in the java.net package and is used to handle URL-related content. The creation and use of the URL class are introduced separately below.

java.net.URL provides a rich set of ways to construct URLs, and resources can be obtained through java.net.URL.

No. Method Description
1 public URL(String protocol, String host, int port, String file) throws MalformedURLException.
Creates a URL from the given parameters (protocol, host name, port number, file name).
2 public URL(String protocol, String host, String file) throws MalformedURLException
Creates a URL using the specified protocol, host name, and file name, with the port set to the protocol's default port.
3 public URL(String url) throws MalformedURLException
Creates a URL from the given URL string
4 public URL(URL context, String url) throws MalformedURLException
Creates a URL from a base address and a relative URL

The URL class contains many methods for accessing the various parts of a URL; the specific methods and descriptions are as follows:

No. Method Description
1 public String getPath()
Returns the path part of the URL.
2 public String getQuery()
Returns the query part of the URL.
3 public String getAuthority()
Gets the authority part of this URL.
4 public int getPort()
Returns the port part of the URL
5 public int getDefaultPort()
Returns the default port number of the protocol.
6 public String getProtocol()
Returns the protocol of the URL
7 public String getHost()
Returns the host of the URL
8 public String getFile()
Returns the file name part of the URL
9 public String getRef()
Gets the anchor of this URL (also called the "reference").
10 public URLConnection openConnection() throws IOException
Opens a URL connection and runs the client to access the resource.

Example

The above example demonstrates using the URL class of java.net to obtain the parameters of each part of a URL:

URLDemo.java

import

java

.

net

.*;

import

java

.

io

.*;

public

class

URLDemo

{

public

static

void

main

(

String

[

]

args

)

{

try

{

URL

url

=

new

URL

(

"

http://www.runoob.com/index.html?language=cn#j2se

"

)

;

System

.

out

.

println

(

"

The URL is:

"

url

.

toString

(

)

)

;

System

.

out

.

println

(

"

The protocol is:

"

url

.

getProtocol

(

)

)

;

System

.

out

.

println

(

"

Authentication information:

"

url

.

getAuthority

(

)

)

;

System

.

out

.

println

(

"

File name and request parameters:

"

url

.

getFile

(

)

)

;

System

.

out

.

println

(

"

Host name:

"

url

.

getHost

(

)

)

;

System

.

out

.

println

(

"

Path:

"

url

.

getPath

(

)

)

;

System

.

out

.

println

(

"

Port:

"

url

.

getPort

(

)

)

;

System

.

out

.

println

(

"

Default port:

"

url

.

getDefaultPort

(

)

)

;

System

.

out

.

println

(

"

Request parameters:

"

url

.

getQuery

(

)

)

;

System

.

out

.

println

(

"

Location (fragment):

"

url

.

getRef

(

)

)

;

}

catch

(

IOException

e

)

{

e

.

printStackTrace

(

)

;

}

}

}

The compilation and execution results of the above example are as follows:


URL 为:http://www.runoob.com/index.html?language=cn#j2se
协议为:http
验证信息:www.runoob.com
文件名及请求参数:/index.html?language=cn
主机名:www.runoob.com
路径:/index.html
端口:-1
默认端口:80
请求参数:language=cn
定位位置:j2se

URLConnection Class Methods

openConnection() returns a java.net.URLConnection.

For example:

The list of URLConnection methods is as follows:

No. Method Description
1 **Object getContent() **
Retrieves the content of the URL link
2 **Object getContent(Class[] classes) **
Retrieves the content of the URL link
3 **String getContentEncoding() **
Returns the value of the content-encoding header field.
4 **int getContentLength() **
Returns the value of the content-length header field
5 **String getContentType() **
Returns the value of the content-type header field
6 **int getLastModified() **
Returns the value of the last-modified header field.
7 **long getExpiration() **
Returns the value of the expires header field.
8 **long getIfModifiedSince() **
Returns the ifModifiedSince field value of the object.
9 public void setDoInput(boolean input)
A URL connection can be used for input and/or output. If you intend to use the URL connection for input, set the DoInput flag to true; if not, set it to false. The default value is true.
10 public void setDoOutput(boolean output)
A URL connection can be used for input and/or output. If you intend to use the URL connection for output, set the DoOutput flag to true; if not, set it to false. The default value is false.
11 public InputStream getInputStream() throws IOException
Returns the input stream of the URL, used to read the resource
12 public OutputStream getOutputStream() throws IOException
Returns the output stream of the URL, used to write the resource.
13 public URL getURL()
Returns the URL to which the URLConnection object is connected

Example

In the following example, the URL uses the HTTP protocol. openConnection returns an HttpURLConnection object.

URLConnDemo.java

import

java

.

net

.*;

import

java

.

io

.*;

public

class

URLConnDemo

{

public

static

void

main

(

String

[

]

args

)

{

try

{

URL

url

=

new

URL

(

"

http://www.runoob.com

"

)

;

URLConnection

urlConnection

=

url

.

openConnection

(

)

;

HttpURLConnection

connection

=

null

;

if

(

urlConnection

instanceof

HttpURLConnection

)

{

connection

=

(

HttpURLConnection

)

urlConnection

;

}

else

{

System

.

out

.

println

(

"

Please enter a URL address

"

)

;

return

;

}

BufferedReader

in

=

new

BufferedReader

(

new

InputStreamReader

(

connection

.

getInputStream

(

)

)

)

;

String

urlString

=

"

"

;

String

current

;

while

(

(

current

=

in

.

readLine

(

)

)

!=

null

)

{

urlString

+=

current

;

}

System

.

out

.

println

(

urlString

)

;

}

catch

(

IOException

e

)

{

e

.

printStackTrace

(

)

;

}

}

}

The compilation and execution results of the above example are as follows:


$ javac URLConnDemo.java 
$ java URLConnDemo
.....这里会输出Runoob Tutorial首页(http://www.runoob.com)的 HTML 内容.....