URL的相关知识

本文介绍了URL的基本概念及其在Java中的应用,包括URL类的构造方法、如何通过URL读取网络资源以及使用URLConnection进行更复杂的交互。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

URl是统一资源定位器的简称,它表示网络上某一资源的地址。通过URL,我们可以访问相应的Internet。浏览器通过解析给定的URL可以在网络上查找相应的文件或其他资源。

1.URL的组成

那首先来看看URL都包括了些什么:

URL的组成包括两个部分:协议名和资源名。

解释:

协议名:是指明获取资源所使用的传输s协议,如http、ftp、file等等

资源名:一个完整的资源名包括主机名、端口号、文件名或文件内部的一个引用。

[color=red][b]注:并不是每一个URL都要包括这些内容的,对于多数协议,主机名和文件名是必须的其他的可有可无。[/b][/color]
2.java中的URL类

了解了URL下面看看java中所提供的URL类:

类中包括以下构造方法:
public URL(String spec);//根据String表示形式创建URL对象。

public URL(String protocol,String host,int port,String file);//根据指定 protocol、host、port 号和 file 创建 URL 对象。

public URL(String protocol,String host,int port,String file,URLStreamHandler handler )//根据指定的 protocol、host、port 号、file 和 handler 创建 URL 对象。

public URL(String protocol,String host,String file)//根据指定的 protocol 名称、host 名称和 file 名称创建 URL。

public URL(URL context,String spec)// 通过在指定的上下文中对给定的 spec 进行解析创建 URL。

public URL(URL context , String spec,URLStreamHandler handler )// 通过在指定的上下文中用指定的处理程序对给定的 spec 进行解析来创建 URL。

[color=red][b]注意:类URL的构造方法都声明抛出非运行时异常,因此生成URL对象时,我们必须要对这一异常进行处理,通常是用try—catch语句进行捕获。[/b][/color]
package com.oyqh;

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

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

[color=red][b]一个URL对象生成后,其属性是不能被改变的,但是我们可以通过类URL多提供的方法来获取这些属性:[/b][/color]
package com.oyqh;

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

public class Test {
public static void main(String[] args) {
try {
URL url = new URL("http://betawap.joycp.net/user/login.php");
System.out.println("url.getProtocol() = " + url.getProtocol());
System.out.println("url.getHost() = " + url.getHost());
System.out.println("url.getPort() = " + url.getPort());
System.out.println("url.getFile() = " + url.getFile());
System.out.println("url.getQuery() = " + url.getQuery());
System.out.println("url.getPath() = " + url.getPath());
} catch (MalformedURLException e) {
e.printStackTrace();
}
}
}
输出:
url.getProtocol() = http
url.getHost() = betawap.joycp.net
url.getPort() = -1
url.getFile() = /user/login.php
url.getQuery() = null
url.getPath() = /user/login.php

3. 通过URL读取www信息

package com.oyqh;

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

public class Test {
public static void main(String[] args) {
BufferedReader bufferedReader = null;
try {

URL url = new URL("http://www.baidu.com");
bufferedReader = new BufferedReader(new InputStreamReader(url.openStream()));
String inleng="";
while((inleng=bufferedReader.readLine())!=null){
System.out.println(inleng);
}
bufferedReader.close();
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}

4.URLConnection 连接WWW

通过URL的方法openStream(),我们只能从网络上读取数据,如果我们同时还想输出数据,这时URL就不能满足我们我需求,所以引入了URLConnection。通过它可以得到我们想要的(因为该类中有getInputStream和getOutputStream)。


package com.oyqh.test;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;

public class TestConnection {
public static void main(String[] args) {
try {
URL url = new URL(
"www.baidu.com");
URLConnection connection = url.openConnection();
BufferedReader bufferedReader = new BufferedReader(
new InputStreamReader(connection.getInputStream()));
String slin = "";
while ((slin = bufferedReader.readLine()) != null) {
System.out.println(slin);
}
bufferedReader.close();
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}

其实URL中的openStream就相当于openConnection().getInputStream();


当我们得到一个URL对象后,就可以通过它读取指定的www资源。我们可以使用URL中的openStream()方法,

openStream()方法与指定的URL建立连接并返回InputStream.这样我们就可以将网络资源转换成流来处理了
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值