📜  Java的.net.URL类在Java中

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

Java的.net.URL类在Java中

URL 是统一资源定位器的首字母缩写。它是在www (万维网)中定位资源的指针。资源可以是任何内容,从简单的文本文件到任何其他内容,如图像、文件目录等。

典型的 URL 可能如下所示

http://www.example.com:80/index.html

该 URL 包含以下部分:

  • 协议:在这种情况下协议是HTTP,在某些情况下可以是HTTPS
  • 主机名:主机名表示资源所在机器的地址,在本例中为www.example.com
  • 端口号:这是一个可选属性。如果未指定,则返回 -1。在上面的例子中,端口号是 80。
  • 资源名称:它是我们想要查看的位于给定服务器上的资源的名称

您可以在此处了解有关 URL 的更多信息。

URL的Class结构如下图:



public final class java.net.URL extends java.lang.Object

以下是 URL 类提供的构造函数。

Constructor

Explanation

public URL(String  url )This constructor creates an object of URL class from given string representation
public URL(String  protocol, String  host, int  port, String  file)This constructor creates an object of URL from the specified protocol, host, port number, and file.
public URL(String  protocol, String  host, String  file)This constructor creates an object of URL from the specified protocol, port number, and file. The default port number is used in this case.
public URL(URL  context, String  src)This constructor creates an instance of a URL by parsing the given src with the specified handler within a given context.

URL类提供的方法:

Method Explanation
equals(Object obj)This method compares this URL for equality with another object.
getAuthority()This method gets the authority part of this URL.
getContent()This method gets the contents of this URL.
getContent(Class[] classes)This method gets the contents of this URL.
getDefaultPort()This method gets the default port number of the protocol associated with this URL.
getFile()This method gets the file name of this URL.
getHost()This method gets the hostname of this URL, if applicable.
getPath()This method gets the path part of this URL.
getPort()This method gets the port number of this URL.
getProtocol()This method gets the protocol name of this URL.
getQuery()This method gets the query part of this URL.
getRef()This method gets the anchor (also known as the “reference”) of this URL.
getUserInfo()This method gets the userInfo part of this URL.
hashCode()This method creates an integer suitable for hash table indexing.
openConnection()This method returns a URLConnection instance that represents a connection to the remote object referred to by the URL.
openConnection(Proxy proxy)Same as openConnection(), except that the connection will be made through the specified proxy; Protocol handlers that do not support proxing will ignore the proxy parameter and make a normal connection.
openStream()This method opens a connection to this URL and returns an InputStream for reading from that connection.
sameFile(URL other)This method compares two URLs, excluding the fragment component.
set(String protocol, String host, int port, String file, String ref)This method sets the fields of the URL.
set(String protocol, String host, int port, String authority, String userInfo, String path, String query, String ref)This method sets the specified 8 fields of the URL.
setURLStreamHandlerFactory(URLStreamHandlerFactory fac)This method sets an application’s URLStreamHandlerFactory.
toExternalForm()This method constructs a string representation of this URL.
toString()This method constructs a string representation of this URL.
toURI()This method returns a URI equivalent to this URL.

演示 1

在这个演示中,我们将为给定的字符串 URL 创建一个 URL 类的对象,我们将打印给定 URL 的主机名、协议、文件名和端口号。

Java
// importing package required
import java.net.URL;
import java.util.Scanner;
  
class GFG {
    public static void main(String[] args)
    {
        String url
            = "https://www.geeksforgeeks.org/variables-in-java/";
        // Calling method to get URL info
        getUrlInfo(url);
    }
    static void getUrlInfo(String url_string)
    {
        // Creating object of URL class
        try {
            URL url = new URL(url_string);
  
            System.out.println("Hostname: "
                               + url.getHost());
            System.out.println("Port Number: "
                               + url.getPort());
            System.out.println("File name: "
                               + url.getFile());
            System.out.println("Protocol:  "
                               + url.getProtocol());
        }
        catch (Exception e) {
        }
    }
}


输出
Hostname: www.geeksforgeeks.org
Port Number: -1
File name: /variables-in-java/
Protocol:  https