📜  Java中的URL类与示例

📅  最后修改于: 2022-05-13 01:55:16.411000             🧑  作者: Mango

Java中的URL类与示例

URL 称为统一资源定位器,它只是字符串文本,用于标识 Internet 上的所有资源,告诉我们资源的地址、如何与其通信以及从中检索某些内容。

网址类

URL 的组成部分

URL 可以有多种形式。然而,最普遍的遵循如下提议的三组件系统:

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

网址类

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

URL 类的构造函数

  1. URL(字符串地址)抛出 MalformedURLException:它从指定的字符串创建一个 URL 对象。
  2. URL(String protocol, String host, String file):根据指定的协议、主机和文件名创建一个 URL 对象。
  3. URL(String protocol, String host, int port, String file):根据协议、主机、端口和文件名创建一个 URL 对象。
  4. URL(URL context, String spec):通过在给定上下文中解析给定规范创建一个 URL 对象。
  5. URL(字符串协议、字符串主机、int 端口、字符串文件、URLStreamHandler 处理程序):
    从指定的协议、主机、端口号、文件和处理程序创建一个 URL 对象。
  6. URL(URL 上下文、字符串规范、URLStreamHandler 处理程序):
    通过在指定上下文中使用指定处理程序解析给定规范来创建 URL。

URL 类中使用的重要方法

MethodAction Performed
getAuthority()Returns the authority part of URL or null if empty
getDefaultPort()Returns the default port used
getFile()Returns the file name.
getHost()Return the hostname of the URL in IPv6 format
getPath()Returns the path of the URL, or null if empty
getPort()Returns the port associated with the protocol specified by the URL
getProtocol()Returns the protocol used by the URL
getQuery()the Returns the query part of URL. A query is a part after the ‘?’ in the URL. Whenever logic is used to display the result, there would be a query field in the URL. It is similar to querying a database.
getRef()Returns the reference of the URL object. Usually, the reference is the part marked by a ‘#’ in the URL. You can see the working example by querying anything on Google and seeing the part after ‘#’
toString()As in any class, toString() returns the string representation of the given URL object.

例子:

Java
// Java program to demonstrate working of URL
  
// Importing required classes
import java.net.MalformedURLException;
import java.net.URL;
  
// Main class
// URL class
public 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+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