Java.net 套件提供了一個類別 URI,其中包含從其元件建立 URI 實例的方法或解析這些元件的字串形式以存取和檢索 URI 實例的多個元件的方法。
廣告
該類別中的熱門課程
JAVA 掌握 - 專業化 | 78 課程系列 | 15 次模擬測驗
開始您的免費軟體開發課程
網頁開發、程式語言、軟體測試及其他
什麼是 URI?
統一資源標識符具有有助於特定資源標識的字元序列。此外,它還有助於在某些協議的幫助下透過網路進行資源表示的交互。有關此特定類別的更多詳細資訊將在以下部分中討論。
文法
以下是 java.net URI 類別的語法。
scheme:[//[user:password@]host[:port]][/]path[?query][#fragment]
-
方案:此元件列出了與 URI 連結的特定協定。儘管某些方案需要“//”,但在其他方案中則不使用。
-
權限:此元件由不同的元件組成,例如驗證部分、主機和前面帶有冒號「:」的連接埠號碼。在第一部分中,身份驗證部分由使用者名稱和密碼組成。同時,第二段Host可以是任意的ip位址。第三部分連接埠號碼是可選的。
-
路徑:具有字串的元件,該字串由伺服器中存在的特定資源的位址組成。
-
查詢:該組件是非分層數據,其中查詢用於通過前一部分中的“?”問號查找特定資源。
-
片段:標識輔助資源的元件。它可以是頁面上出現的標題和副標題等。
Java.net URI 類別如何運作?
正如已經討論過的,URI 被稱為統一資源定位器,它可以透過使用位置和名稱來幫助識別 Web 資源,例如圖像、級聯樣式表檔案、視訊等。這裡,位置是存取或檢索特定資源的機制。例如,HTTP://、FTP:// 等都是位置。除此之外,「名稱」是特定資源的全域特定名稱。讓我們來看一個範例,其中以下網址:https://samplepath/path/to/video.mkv 檢索影片。在這裡,這個特定位址的sample-path/path/to/video.mkv部分是URN或名稱。
建構子
以下是 URI 的五種不同的建構子。
-
URI(String st): 透過解析字串 st 建構一個 URI。
-
URI( String schema , String str, String frag): 將依照給定的元件建構 URI。
-
URI( String schm, String userinf, String host, int portnum, String path, String query, String frag): 將依照給定的元件建構分層 URI。
-
URI( String schm, String host, String path, String frag): 將根據給定的元件建構分層 URI。
-
URI(String schm、String suthorty、String path、String query、String frag):將根據給定的元件建構分層 URI。
Java.net URI 的方法
以下是執行多種操作的 URI 類別的不同方法。
-
compareTo(URI ur): This URI will be compared with another URI.
-
create(String str): A URI will be created on parsing the string str
-
equals(Object obj): The URI will be tested for equality with the object obj
-
getAuthority(): Returns the URI’s authority component, which is decoded.
-
getFragment(): Returns the URI’s fragment component, which is decoded.
-
getHost(): Returns the URI’s host component, which is decoded.
-
getPath(): Returns the URI’s path component, which is decoded.
-
getPort(): URI port number will be returned.
-
getQuery(): Returns the URI’s query component, which is decoded.
-
getRawAuthority(): Returns the URI’s raw authority component.
-
getRawFragment(): Returns the URI’s raw fragment component.
-
getRawPath(): Returns the URI’s raw path component.
-
getRawQuery(): Returns the URI’s raw query component.
-
getRawSchemeSpecificPart(): Returns the URI’s raw scheme-specific part.
-
getRawUserInfo(): Returns the URI’s raw user information component.
-
getScheme(): Returns the URI’s scheme component.
-
getSchemeSpecificPart(): Returns the URI’s scheme-specific part which is decoded.
-
getUserInfo(): Returns the URI’s user information component which is decoded.
-
hashCode(): Returns URI hash code value.
-
isAbsolute(): Checks whether the URI provided is absolute or not.
-
isOpaque(): Checks whether the URI provided is opaque or not.
-
normalize(): URI path gets normalized on calling this method.
-
parseServerAuthority(): This method parses the authority component into user information, port, and host components.
-
relativize(URI uri): Given URI gets relativized to the URI specified.
-
resolve(String str): A new URI is constructed by parsing the string mentioned and resolving the same against the URI.
-
resolve(URI uri): The given URI is resolved against the URI.
-
toASCIIString(): Content of the URI mentioned is returned as a US-ASCII string.
-
toString(): Content of the URI mentioned is returned as a string.
-
toURL(): A URL gets constructed from the URI mentioned.
Examples to Implement Java.net URI
Now, let us see a sample program of java.net uri class.
Java program to demonstrate several methods in the class java.net.uri. //Java Program to demonstrate methods of URI class
Code:
import java.net.*;
class URIexample
{
public static void main(String[] args) throws Exception
{
String str = "https://www.google.com/search?q=educba+treemap&oq=educba+treemap+&aqs=chrome..69i57j69i60.12298j1j7&sourceid=chrome&ie=UTF-8";
// Creation of new URI by parsing the string
URI exampleuri = new URI(str);
//different methods of <u>uri</u> class
System.out.println("Host of the URI is: " + exampleuri.getHost());
System.out.println("Port of the URI is: = " + exampleuri.getPath());
System.out.println("Raw Path of the URI is: = " + exampleuri.getRawPath());
System.out.println("Path of the URI is: = " + exampleuri.getPath());
System.out.println("Query of the URI is: = " + exampleuri.getQuery());
System.out.println("Raw Query of the URI is: = " + exampleuri.getRawQuery());
System.out.println("Fragment of the URI is: = " + exampleuri.getFragment());
System.out.println("Raw Fragment of the URI is: = " + exampleuri.getRawFragment());
//another uri in order to demonstrate the method compareTo and equals
URI exampleuri2 = new URI(str + "fr");
System.out.println("CompareTo exampleuri2=" + exampleuri.compareTo(exampleuri2));
System.out.println("Equals of the URI is: = " + exampleuri.equals(exampleuri2));
System.out.println("Hashcode of the URI is: " + exampleuri.hashCode());
System.out.println("toString of the URI is: " + exampleuri.toString());
System.out.println("toASCIIString of the URI is: " + exampleuri.toASCIIString());
}
}
Output: In this program, different methods of java.net.uri class are used and displayed results based on that.
以上是Java.net URI的詳細內容。更多資訊請關注PHP中文網其他相關文章!