URL类的主要方法和构造函数

来源:互联网 发布:python 获取当前行数 编辑:程序博客网 时间:2024/05/29 04:46
URL是一个描述如何在INTERNET上定位资源的字符串。一个完整得编程者能很方便地利用URL在
INTERNET上进行网络通信
一个完整得URL由协议、主机名、端口号、文件名、引用组成。例
http://tech.china.com:80/zh_cn/news/index.html
它使用http协议,并使用端口80与主机teach.china.com连接从而访问文件/zh_cn/news/index.html
Java中得URL类代表了各种URL,以下是构造函数
URL(String spec)
URL(String protocol,String host,int port,String file)
URL(String protocol,String host,int port,String file,URLStreamHandler handler)
URL(String protocol,String host,String file)
URL(URL context,String spec)
URL(URL context,String spec,URLStreamHandler handler)
参数说明
context 在spec 为相对URL时解释spec
handler 指定上下文的处理器
host 主机名
file 文件路径名
port 要使用的端口号
protocol 要使用的协议
spec URL字符串
使用URL类中得方法可以获URL组件(协议,主机号,端口号,文件名,引用),此外,它还提供了获取URL
所代表得资源方法
URL类得主要方法
Object getContent() //获取该URL所标示的内容
String getFile() //获取该URL的文件名
String getHost() //获取该URL的主机号
Int getPort() //获取该URL的端口号
String getProtocol() //获取该URL的协议
String getQuery() //获取该URL的查询部分
String getUserInfo() //获取该URL的用户信息
URLConnection openConnection() //打开由该URL标示的位置的连接
InputStream openStream() //打开该URL的输入流
booleam sameFile(URL other) //判断两个URL是否指向同一个文件
void set(String protocol,String host,int port,String file,String ref) //设置该URL域
String toExternalForm() //返回该URL的字符串
原创粉丝点击