URL和URI(一)URL

URL类

public URL(String url) throws MalformedURLException
public URL(String protocol, String host, int port, String file) throws MalformedURLException
public URL(String protocol, String host, String file) throws MalformedURLException
public URL(URL base,String relative) throws MalformedURLException

使用哪个构造函数取决于你有哪些信息以及信息的形式;

从字符串构造URL

最简单的URL构造函数只接受一个字符串形式的绝对URL作为唯一的参数:

public URL(String url) throws MalformedURLException在这里插入代码片

例如:

try {
			URL url = new URL("http://www.baidu.com");
		} catch (MalformedURLException e) {
			System.out.println(e);
		}

由组成部分构造URL

还可以通过指定协议、主机名和文件来构建一个URL:

public URL(String protocol, String host, String file) throws MalformedURLException

这个构造函数将端口设置为-1,所以会使用该协议的默认端口。file参数应当以斜线开头,包括路径、文件名和可选的片段标识符。有可能会忘记最前面的斜线,这是一个很常见的错误。与所有URL构造函数一样,它可能会抛出MalformedURLException异常。

    try {
			URL url = new URL("http","www.eff.org","/blueribbon.html#intro");
		} catch (MalformedURLException e) {
			System.out.println(e);
		}

这会创建一个URL对象,指向http://www.eff.org/blueribbon.html#intro,并使用HTTP默认的端口(80)。
在很少见的情况下,默认端口不正确时,还允许使用一个int显式指定端口:

   try {
			URL url = new URL("http","www.eff.org","8000",/blueribbon.html#intro");
		} catch (MalformedURLException e) {
			System.out.println(e);
		}

构造相对URL

这个构造函数根据相对URL和基础URL构建一个绝对URL:

public URL(URL base,String relative) throws MalformedURLException

在这里插入图片描述

   try {
			URL url1 = new URL("http://www.ibiblio.org/javafaq.index.html");
			URL url2 = new URL(url1, "mailinglists.html");
		} catch (MalformedURLException e) {
			System.out.println(e);
		}

从URL获取数据

URL类有几个方法可以从URL获取数据:

public final InputStream openStream() throws java.io.IOException
public URLConnection openConnection() throws java.io.IOException
public URLConnection openConnection(Proxy proxy) throws java.io.IOException
public final Object getContent() throws java.io.IOException
public final Object getContent(Class[] classes)throws java.io.IOException

在这里插入图片描述

public final InputStream openStream() throws java.io.IOException

在这里插入图片描述

   try {
			URL url1 = new URL("http://www.baidu.com");
			InputStream stream = url1.openStream();
			int c;
			while((c = stream.read())!= -1)
			{
				System.out.write(c);
			}
		} catch (IOException e) {
			System.out.println(e);
		}

控制台会输出源代码:
在这里插入图片描述

public URLConnection openConnection() throws java.io.IOException

openConnection()方法为指定的URL打开一个socket,并返回一个URLConnection对象。URLConnection表示一个网络资源的打开的连接。如果调用失败,则openConnection()会抛出一个IOException异常;

  try {
			URL url1 = new URL("http://www.baidu.com");
			URLConnection uc = url1.openConnection();
			InputStream stream = uc.getInputStream();
			//从连接读取			
		} catch (IOException e) {
			System.out.println(e);
		}

在这里插入图片描述
这个方法还有一个重载版本,可以指定通过哪个代理服务器传递连接:

public URLConnection openConnection(Proxy proxy) throws java.io.IOException

public final Object getContent() throws java.io.IOException

在这里插入图片描述

URL url1 = new URL("http://www.baidu.com");
Object object = url1.getContent();
//将Object强制转换为适当的类型
//处理这个Object

在这里插入图片描述
下载一个对象:

URL url1 = new URL("http://www.baidu.com");
Object object = url1.getContent();
System.out.println(object.getClass().getName());

在这里插入图片描述
具体的类可能因为Java的版本不同而有所区别:
在这里插入图片描述

public final Object getContent(Class[] classes)throws java.io.IOException

getContent()方法的这个重载版本允许你选择希望将内容作为哪个类返回。这个方法尝试以第一种可用的格式返回URL的内容:

            URL url1 = new URL("http://www.baidu.com");
			Class<?>[] types = new Class[3];
			types[0] = String.class;
			types[1] = Reader.class;
			types[2] = InputStream.class;
			Object object = url1.getContent(types);

如果内容处理器知道如何返回资源的一个字符串表示,它就会返回一个String。如果它不知道如何返回资源的字符串表示,则返回Reader。倘若它也不知道如何将资源表示为一个读取器,那么它将返回InputStream。必须用instanceof检查返回的对象的类型:

在这里插入图片描述

分解URL

URL由以下5部分组成:

  • 模式,也称为协议
  • 授权机构
  • 路径
  • 片段标识符
  • 查询字符串

在这里插入图片描述
public String getProtocol()
getProtocol()方法返回一个String,其中包含URL的模式(如"http"、“https"或"file”):

URL url1 = new URL("http://www.baidu.com");
System.out.println(url1.getProtocol());    //输出http

public String getHost()
getHost()方法返回一个String,其中包含URL的主机名:

URL url1 = new URL("http://www.baidu.com");
System.out.println(url1.getHost());   //输出www.baidu.com

public int getPort()
在这里插入图片描述

URL url1 = new URL("http://www.baidu.com");
System.out.println(url1.getPort());   //输出-1

public int getDefaultPort()
在这里插入图片描述

URL url1 = new URL("http://www.baidu.com");
System.out.println(url1.getDefaultPort());   //输出80

public String getFile()
getFile()方法返回一个String。从主机名后的第一个斜线(/)一直到片段标识符#之前的字符,被认为是文件部分:

URL url1 = new URL("http://www.baidu.com/test.html?name=yd&sex=man#");
System.out.println(url1.getFile());   //输出/test.html?name=yd&sex=man

public String getPath()
与 getFile()相似,但它返回的String中不包含查询字符串:

URL url1 = new URL("http://www.baidu.com/test.html?name=yd&sex=man#");
System.out.println(url1.getPath());    //输出/test.html

public String getRef()
返回URL的片段标识符部分。如果URL没有片段标识符,则这个方法返回null:

URL url1 = new URL("http://www.baidu.com/test.html?name=yd&sex=man#34546");
System.out.println(url1.getRef());   //输出34546

public String getQuery()
返回URL的查询字符串。如果URL没有查询字符串,则这个方法返回null:

URL url1 = new URL("http://www.baidu.com/test.html?name=yd&sex=man#34546");
System.out.println(url1.getQuery());   //输出name=yd&sex=man

public String getUserInfo()
返回URL的用户信息。如果URL没有用户信息,则这个方法返回null:

URL url1 = new URL("http://admin@www.baidu.com/test.html?name=yd&sex=man#34546");
System.out.println(url1.getUserInfo());   //输出admin

public String getAuthority()
getAuthority()方法会以URL中的形式返回授权机构,可能有用户信息和端口,也可能没有:

URL url1 = new URL("http://admin@www.baidu.com/test.html?name=yd&sex=man#34546");
System.out.println(url1.getAuthority());   //输出admin@www.baidu.com

相等性和比较

在这里插入图片描述
在这里插入图片描述
URL类还有一个sameFile()方法,可以检查两个URL是否指向相同的资源:

public boolean sameFile(URL other)

在这里插入图片描述

比较

URL有三个方法可以将一个实例转换为另外一种形式,分别是toString()、toExternalForm()和toURI()。
在这里插入图片描述
最好toURI()方法将URL对象转换为对应的URI对象:

 public URI toURI() throws URISyntaxException

以上只是学习所做的笔记!!!
书籍:Java网络编程

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值