Java URL类及实例

来源:这里教程网 时间:2026-02-17 21:16:15 作者:

Java URL类及实例

URL被称为统一资源定位器(Uniform Resource Locator),它是一个简单的文本字符串,用于识别互联网上的所有资源,告诉我们资源的地址,如何与之通信,以及从它那里获取什么。

URL的组成部分

一个URL可以有多种形式。然而,最普遍的是遵循下面提出的三部分系统。

    协议: HTTP是这里的协议主机名: 资源所在的机器的名称。文件名: 机器上的文件的路径名。端口号: 用于连接的端口号(通常是可选的)

URL类

URL类是通往互联网上任何资源的网关。类URL代表一个统一资源定位器,它是指向万维网上 “资源 “的一个指针。一个资源可以指向一个简单的文件或目录,也可以指向一个更复杂的对象,如对数据库或搜索引擎的查询。

URL类的构造函数

    URL(String address) 抛出MalformedURLException: 它从指定的字符串创建一个URL对象。URL(String protocol, String host, String file): 从指定的协议、主机和文件名创建一个URL对象。URL(String protocol, String host, int port, String file): 从协议、主机、端口和文件名创建一个URL对象。URL(URL context, String spec): 在给定的上下文中通过解析给定的规格创建一个URL对象。URL(String protocol, String host, int port, String file, URLStreamHandler handler):
    从指定的协议、主机、端口号、文件和处理器创建一个URL对象。
    URL(URL context, String spec, URLStreamHandler handler):
    通过在指定的上下文中用指定的处理器解析给定的规格来创建一个URL。

URL类中使用的重要方法

方法名称执行的操作
getAuthority()返回URL的授权部分,如果是空的则为空
getDefaultPort()返回所使用的默认端口
getFile()返回文件名。
getHost()以IPv6格式返回URL的主机名
getPath()返回URL的路径,如果为空,则为空
getPort()返回与URL所指定的协议相关的端口
getProtocol()返回URL所使用的协议
getQuery()返回URL的查询部分。查询是URL中’?’之后的部分。每当逻辑被用来显示结果时,URL中会有一个查询字段。它类似于查询数据库。
getRef()返回URL对象的引用。通常情况下,引用是URL中以’#’标记的部分。你可以通过在Google上查询任何东西并看到’#’后面的部分来看到工作实例。
toString()在任何类中,toString()返回给定URL对象的字符串表示。

例子

// Java program to demonstrate working of URL  // Importing required classesimport java.net.MalformedURLException;import java.net.URL;  // Main class// URL classpublic class GFG {      // Main driver method    public static void main(String[] args)        throws MalformedURLException    {          // Creating 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");          // Creating 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");          // Printing 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-");          // Retrieving the protocol for the URL        System.out.println("Protocol:- "                           + url3.getProtocol());          // Retrieving the hostname of the url        System.out.println("Hostname:- " + url3.getHost());          // Retrieving the default port        System.out.println("Default port:- "                           + url3.getDefaultPort());          // Retrieving the query part of URL        System.out.println("Query:- " + url3.getQuery());          // Retrieving the path of URL        System.out.println("Path:- " + url3.getPath());          // Retrieving the file name        System.out.println("File:- " + url3.getFile());          // Retrieving the reference        System.out.println("Reference:- " + url3.getRef());    }}

输出

https://www.google.co.in/?gfe_rd=cr&ei=ptYqWK26I4fT8gfth6CACg#q=geeks+for+geeks+javahttps://www.geeksforgeeks.org/jvm-works-jvm-architecture/Different components of the URL3-Protocol:- httpsHostname:- www.google.co.inDefault port:- 443Query:- q=gnu&rlz=1C1CHZL_enIN714IN715&oq=gnu&aqs=chrome..69i57j69i60l5.653j0j7&sourceid=chrome&ie=UTF-8Path:- /searchFile:- /search?q=gnu&rlz=1C1CHZL_enIN714IN715&oq=gnu&aqs=chrome..69i57j69i60l5.653j0j7&sourceid=chrome&ie=UTF-8Reference:- q=geeks+for+geeks+java

相关推荐