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

Java Example – Parse a URL

Java Examples - Parsing a URL

Java Examples Java Examples

The following example demonstrates how to parse a URL address using url.getProtocol(), url.getFile() and other methods of the net.URL class:

Main.java File

import

java

.

net

.

URL

;

public

class

Main

{

public

static

void

main

(

String

[

]

args

)

throws

Exception

{

URL

url

=

new

URL

(

"

http://www.runoob.com/html/html-tutorial.html

"

)

;

System

.

out

.

println

(

"

The URL is

"

url

.

toString

(

)

)

;

System

.

out

.

println

(

"

The protocol is

"

url

.

getProtocol

(

)

)

;

System

.

out

.

println

(

"

The file name is

"

url

.

getFile

(

)

)

;

System

.

out

.

println

(

"

The host is

"

url

.

getHost

(

)

)

;

System

.

out

.

println

(

"

The path is

"

url

.

getPath

(

)

)

;

System

.

out

.

println

(

"

The port number is

"

url

.

getPort

(

)

)

;

System

.

out

.

println

(

"

The default port number is

"

url

.

getDefaultPort

(

)

)

;

}

}

The output of the above code is:



URL 是 http://www.runoob.com/html/html-tutorial.html

协议是 http

文件名是 /html/html-tutorial.html

主机是 www.runoob.com

路径是 /html/html-tutorial.html

端口号是 -1

默认端口号是 80

Java Examples Java Examples