URL类是Internet上任何可用资源的网关。类URL表示统一资源定位符,它是指向万维网上“资源”的指针。资源可以指向简单的文件或目录,也可以引用更复杂的对象,例如对数据库或搜索引擎的查询

什么是网址?

许多人必须知道Uniform Resource Locator-URL是一串文本,用于标识Internet上的所有资源,告诉我们资源的地址,如何与之通信以及从中检索资源。

简单的URL如下所示:

URL的组成部分:

URL可以有多种形式。然而,最一般的是三组分系统 -

协议: HTTP是这里的协议

主机名:资源所在机器的名称。

文件名:计算机上文件的路径名。

端口号:要连接的端口号(通常是可选的)。

URL类的一些构造函数: -

URL(字符串地址)抛出MalformedURLException:它从指定的String创建一个URL对象。

URL(字符串协议,字符串主机,字符串文件):根据指定的protcol,主机和文件名创建URL对象。

URL(字符串协议,字符串主机,int端口,字符串文件):根据协议,主机,端口和文件名创建URL对象。

URL(URL context,String spec):通过解析给定上下文中的给定规范来创建URL对象。

URL(字符串协议,字符串主机,int端口,字符串文件,URLStreamHandler处理程序): -

从指定的协议,主机,端口号,文件和处理程序创建URL对象。

URL(URL上下文,字符串规范,URLStreamHandler处理程序): -

通过使用指定上下文中的指定处理程序解析给定规范来创建URL。

Java中的URL类的示例程序

// Java program to demonstrate working of URL
import java.net.MalformedURLException;
import java.net.URL;
public class URLclass1
{
public static void main(String[] args)
throws MalformedURLException
{
// creates a URL with string representation.
URL url1 =
new URL("https://www.google.co.in/?gfe_rd=cr&ei=ptYq" +
"WK26I4fT8gfth6CACg#q=geeks+for+geeks+java");
// creates a URL with a protocol,hostname,and path
URL url2 = new URL("http", "www.geeksforgeeks.org",
"/jvm-works-jvm-architecture/");
URL url3 = new URL("https://www.google.co.in/search?"+
"q=gnu&rlz=1C1CHZL_enIN71" +
"4IN715&oq=gnu&aqs=chrome..69i57j6" +
"9i60l5.653j0j7&sourceid=chrome&ie=UTF" +
"-8#q=geeks+for+geeks+java");
// print the String representation of the URL.
System.out.println(url1.toString());
System.out.println(url2.toString());
System.out.println();
System.out.println("Different components of the URL3-");
// retrieve the protocol for the URL
System.out.println("Protocol:- " + url3.getProtocol());
// retrieve the hostname of the url
System.out.println("Hostname:- " + url3.getHost());
// retrieve the defalut port
System.out.println("Default port:- " +
url3.getDefaultPort());
// retrieve the query part of URL
System.out.println("Query:- " + url3.getQuery());
// retrive the path of URL
System.out.println("Path:- " + url3.getPath());
// retrive the file name
System.out.println("File:- " + url3.getFile());
// retrieve the reference
System.out.println("Reference:- " + url3.getRef());
}
}

输出:

https://www.google.co.in/?gfe_rd=cr&ei=ptYqWK26I4fT8gfth6CACg#q=geeks+for+geeks+java

https://www.geeksforgeeks.org/jvm-works-jvm-architecture/

Different components of the URL3-

Protocol:- https

Hostname:- www.google.co.in

Default port:- 443

Query:- q=gnu&rlz=1C1CHZL_enIN714IN715&oq=gnu&aqs=chrome..69i57j69i60l5.653j0j7&sourceid=chrome&ie=UTF-8

Path:- /search

File:- /search?q=gnu&rlz=1C1CHZL_enIN714IN715&oq=gnu&aqs=chrome..69i57j69i60l5.653j0j7&sourceid=chrome&ie=UTF-8

Reference:- q=geeks+for+geeks+java

上述计划中使用的一些方法的说明如下: -

public String toString():与任何类一样,toString()返回给定URL对象的字符串表示形式。

public String getAuthority():返回URL的权限部分,如果为空则返回null。

public String getPath():返回URL的路径,如果为空则返回null。

public String getQuery():返回URL的查询部分。查询是'?'之后的部分 在URL中。每当使用逻辑来显示结果时,URL中都会有一个查询字段。它类似于查询数据库。

public String getHost():以IPv6格式返回URL的主机名。

public String getFile():返回文件名。

public String getRef():返回URL对象的引用。通常,引用是URL中标记为“#”的部分。您可以通过查询谷歌上的任何内容并在“#”之后查看该部分来查看工作摘要。

public int getPort():返回与URL指定的协议关联的端口。

public int getDefaultPort:返回使用的默认端口。

public String getProtocol():返回URL使用的协议。