java通过url获取类_《由浅入深学Java》第十五章 利用URL类获取网络资源

《由浅入深学Java》

第十五章 利用URL类获取网络资源

15.1 URL类和网络资源

URL(Uniform Resource

Locator)的中文名字是“统一资源定位器”,它用统一的格式标识因特网上的资源(当然,它们也是以文件为单位存在的)。一个完整的URL的格式如下:

协议://主机域名:端口/路径文件名/#锚点?请求字符串

常用的协议和对应端口有:HTTP - 80,FTP-

21,TELNET - 23,SMTP- 25,POP3- 110。

主机域名如java.sun.com,它对应一个IP地址。路径文件名如news/index.htm,末端的HTM文件是一个网页文件,后面的“锚点和请求字符串”只有当文件为网页文件时才有,如html,

asp,php等类型的文件。

Java预制了java.net.URL类来解决网络资源的传输问题。它提供了许多方法来完成对远程站点的访问和操作,使得网络编程变得非常容易。URL构建器有下列几种。

URL(String

urlAddress) 用字符串给出的合法的URL地址

URL(String protocol, String host, [int

port,] String file) 用协议、主机名、端口和文件名构建,端口可省略使用相应协议的默认端口。

URL(String context, String

pathfile) 通过相对位置来构建

例如

URL url = new

URL("http://java.sun.com/index.html");

URL base = bew URL("http://java.sun.com/");

URL url = new URL(base, "download4/index.html");

URL类常用一些get方法获取URL的信息,除了端口为int类型外其余都是String类型的。它们是

getFile() 获取路径文件名

getHost() 获取主机名(如java.sun.com)

getPath() 获取路文件径名

getPort 得到端口号整数

getProtocol()获取协议名

getQuery() 获取查询字符串

getRef() 获取参考点(锚点)

另外还有

Object getContent()

获取URL的资源内容,例如一个图片对象。

boolean sameFile(URL aurl)

比较两个URL是否指向同一个资源(忽略参考点)

URLConnection openConnection()

打开通向该URL的连接,返回一URLConnection对象

InputStream openStream()

打开该URL的连接,返回一个输入流对象

例1 本例程演示URL类的构建器和常用方法。

///import java.net.*;import java.io.*;

public class UrlTest { static void

parseUrl (URL url) {

BufferedReader br = null;

try{

System.out.println("URL

:" + url.toString());

System.out.println("Protocol

:"+url.getProtocol());

System.out.println("Host

:"+url.getHost());

System.out.println("Port

:"+url.getPort());

System.out.println("File

:"+url.getFile());

System.out.println("Content

:" +url.getContent());

System.out.println("---");

br = new BufferedReader(new InputStreamReader(url.openStream()));

String temp =null;

temp = br.readLine();

while (temp

!= null) {

System.out.println(temp);

temp = br.readLine();

}

}catch(Exception e) {

e.printStackTrace();

}

}public static void

main(String args[]) {

URL[] url

= new URL[5];

int i

=0;

try{

//URL url =

null;url[0] = new

URL("http://127.0.0.1:80/java/test.html");

url[1] = new

URL("http://localhost/java/test.html");

url[2] = new

URL("http","localhost",80,"java/test.html");

url[4] = new

URL("http://ZCB");

url[3] = new

URL(url[4],"/java/test.html");

for (i=0;

i

for (int j=0 ;j<20; j++)

System.out.print(i);

System.out.println();

parseUrl(url[i]);

}

}catch(MalformedURLException me){

me.printStackTrace();

}

}

}

///

程序运行后的输出结果如下:

00000000000000000000

URL :http://127.0.0.1:80/java/test.html

Protocol :http

Host :127.0.0.1

Port :80

File :/java/test.html

Content :sun.net.www.http.KeepAliveStream@3ac748

文件内容(略)

11111111111111111111

URL :http://localhost/java/test.html

Protocol :http

Host :localhost

Port :-1

File :/java/test.html

Content :sun.net.www.http.KeepAliveStream@129206

文件内容(略)

22222222222222222222

URL :http://localhost:80java/test.html

Protocol :http

Host :localhost

Port :80

File :java/test.html

Content :sun.net.www.http.KeepAliveStream@30f13d

文件内容(略)

33333333333333333333

URL :http://ZCB/java/test.html

Protocol :http

Host :ZCB

Port :-1

File :/java/test.html

Content :sun.net.www.http.KeepAliveStream@2e000d

文件内容(略)

44444444444444444444

URL :http://ZCB

Protocol :http

Host :ZCB

Port :-1

File :

Content :sun.net.www.http.KeepAliveStream@169e11

简要说明

url[0]-url[3]这四URL地址指的是用不同的方法表达的同一个地址。127.0.0.1是把本机器作为“个人服务器”的IP地址,也叫做本地主机"localhost"。ZCB是我的计算机名,这三者都可以作为URL构建器中的主机名用。

从结果可以看出,getHost()在上述几种情况下就是构建器中提供的Host。其他也是。

从结果可以看出,getFile()包含主机根目录下的路径文件名。

当缺省端口号时,getPort()

=-1,这是为什么?因为构建了一个URL,Java并不探测这个地址实际存在,get方法得到的是构建器中提供的信息。如果地址不存在,在运行到需要连接时会掷出异常。

getContent()方法得到一个流如KeepAliveStream

@169e11(在内存中)。

要运行本程序本地机器必须是一个“个人服务器”。我用的操作系统是Windows

98,需要安装WSP包。个人服务器的“虚拟根目录”在c:\Inetpub\wwwroot。你还需要在其上建立一个目录java和在java中建立一个简单的test.html文件。在windows

2000中有这项功能,您只要在“控制面版—管理工具—Internet服务管理器”中设置一下就行,在对话框中有一个默认web站点。在它的虚拟跟目录中建立一个"java"目录就行了。

例2 在远程服务器上重复例1的程序。

///public class

UrlRemote{ static void

parseUrl (URL url) {

同例1 } public static void

main(String args[]) {

try{

URL url = new

URL("http://10.10.0.81/tagd/index.htm");

System.out.println("This is

from web:10-10-0-81");

parseUrl(url);

URL url2 = new

URL("http://www.163.com/");

System.out.println("This is

from web-163");

parseUrl(url2);

}catch(MalformedURLException me){

me.printStackTrace();

}

}

}

///

运行结果:

This is from web:10-10-0-81

URL :http://10.10.0.81/tagd/index.htm

Protocol :http

Host :10.10.0.81

Port :-1

File :/tagd/index.htm

This is from web-163

URL :http://www.163.com/

Protocol :http

Host :www.163.com

Port :-1

File :/

Path :/

简要说明

本例中的parseUrl()方法由例1稍作改变,去掉了getContent(),增加了getPath()。我们可以看到getFile和getPath的结果是一样的。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值