当前位置:K88软件开发文章中心编程语言JavaJava01 → 文章内容

java.net.URL类

减小字体 增大字体 作者:佚名  来源:网上搜集  发布时间:2019-1-4 7:54:03

-->

URl(Uniform Resource Locator)类代表统一资源定位器,统一资源定位器是指互联网“资源”的名称。资源可以是简单的文件或目录,也可以是对更为复杂的对 象的引用,例如对数据可或搜索引擎的查询。通常URL可以由协议名、主机、端口和资源组成。URL的格式为”protocol://host:port/resourceName”。例如,URL地
址“http://www.k88.net”。

URL的构造方法

public URL (String str)throws MalformedURLException
该构造方法使用字符串初始化一个URL对象。
例如:

try {
URL url=new URL(“http://baidu.com”);
} catch (MalformedURLException e) {
e.printStackTrace();
}

URL常用方法

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
package cn.k88.net_Demo;

import java.net.MalformedURLException;
import java.net.URL;

public class URL_method {

public static void main(String[] args) {
URL url = null;
try {
url = new URL("http://baidu.com");
} catch (MalformedURLException e) {

e.printStackTrace();
}
System.out.println(url.getHost()); // 获取URL主机号
System.out.println(url.getFile()); // 获取URL文件名
System.out.println(url.getPort()); // 获取URL端口号
System.out.println(url.getProtocol()); // 获取URL协议
System.out.println(url.getQuery()); // 获取URL的查询部分
System.out.println(url.getUserInfo()); // 获取URL的用户信息

}

}

读取 URL 中的资源

URL对象调用InputStream openStream() 方法可以返回一个输入流,该输入流指向URL对象包含的资源。通过该输入流可以将服务器上的资源信息读入到客户端。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
package cn.k88.net_Demo;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.URL;

public class URL_Resource {
    public static void main(String[] args) throws IOException {

        URL url;
        url = new URL("http://www.k88.net");
        // 通过URL的openStream方法获得URL对象所标识的资源的字节输入流。
        InputStream is = url.openStream();
        // 将字节输入流转换为字符输入流,并指定字符编码
        InputStreamReader isr = new InputStreamReader(is, "utf-8");
        // 为字符输入流提供缓冲流,提高I/O性能
        BufferedReader bfr = new BufferedReader(isr);

        String len;
        while ((len = bfr.readLine()) != null) {
            System.out.println(len);

        }

        bfr.close();
        isr.close();
        is.close();

    }
}

下例中,将URL获取的资源通过FileOutputStream写入到指定文件中。由于网络速度或其他因素,URL资源可能引起堵塞,因此例子中使用了一个线程读取URL资源,以免堵塞主线程。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
package cn.k88.net_Demo;

import java.net.URL;
import java.util.Scanner;

public class URL_Demo {

public static void main(String[] args) {
Scanner scanner;
URL url;
Thread readURL;
Look look = new Look();
System.out.println("输入url资源,例如:http://www.k88.net");
scanner = new Scanner(System.in);
String source = scanner.nextLine();
scanner.close();
try {
url = new URL(source);
look.setURL(url);
readURL = new Thread(look);
readURL.start();
}

catch (Exception exp) {
System.out.println(exp);

}

}

}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
package cn.k88.net_Demo;

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;

public class Look implements Runnable {
URL url;

public void setURL(URL url) {
this.url = url;
}

public void run() {
try {
InputStream is = url.openStream();
FileOutputStream writefile = new FileOutputStream("D://URL.html");

byte[] b = new byte[10000];
while (is.read(b) != -1) {
writefile.write(b);
}
writefile.close();

} catch (IOException e) {
}
System.out.println("url资源已保存至D://URL.html");
}
}

java.net.URL类