Java.net URI

PHPz
リリース: 2024-08-30 16:04:58
オリジナル
309 人が閲覧しました

Java.net パッケージは、コンポーネントから URI インスタンスを作成するためのメソッド、または URI インスタンスのいくつかのコンポーネントにアクセスして取得するためのそれらのコンポーネントの文字列形式を解析するためのメソッドを含むクラス URI を提供します。

広告 このカテゴリーの人気コース JAVA マスタリー - スペシャライゼーション | 78 コース シリーズ | 15 回の模擬テスト

無料ソフトウェア開発コースを始めましょう

Web 開発、プログラミング言語、ソフトウェア テスト、その他

URI とは何ですか?

統一リソース識別子には、特定のリソースの識別に役立つ一連の文字が含まれています。さらに、特定のプロトコルの助けを借りて、ネットワーク上のリソース表現の相互作用にも役立ちます。この特定のクラスの詳細については、以下のセクションで説明します。

構文

以下は、java.net URI クラスの構文です。

scheme:[//[user:password@]host[:port]][/]path[?query][#fragment]
ログイン後にコピー
  • スキーム: このコンポーネントは、URI にリンクされた特定のプロトコルをレイアウトします。一部のスキームでは「//」が必要ですが、他のスキームでは使用されません。
  • authority: このコンポーネントは、認証部分、ホスト、コロン「:」で始まるポート番号などのさまざまなコンポーネントで構成されています。最初のセクションの「認証」セクションはユーザー名とパスワードで構成されます。同時に、2 番目のセクションのホストには、任意の IP アドレスを指定できます。 3 番目のセクションのポート番号はオプションです。
  • パス: サーバー内に存在する特定のリソースへのアドレスで構成される文字列を持つコンポーネント。
  • クエリ: このコンポーネントは非階層データであり、前の部分の疑問符「?」によって特定のリソースを検索するために使用されるクエリです。
  • フラグメント: 二次リソースを識別するコンポーネント。ページなどに存在する見出しだけでなく小見出しにもできます。

Java.net URI クラスはどのように機能しますか?

すでに説明したように、URI はユニフォーム リソース ロケーターとして知られており、場所と名前を使用して画像、カスケード スタイル シート ファイル、ビデオなどの Web リソースを識別するのに役立ちます。ここで、場所は特定のリソースにアクセスまたは取得するためのメカニズムです。たとえば、HTTP://、FTP:// などが場所になります。それに加えて、「name」は特定のリソースのグローバルに固有の名前です。 Web アドレス https://samplepath/path/to/video.mkv がビデオを取得する例を見てみましょう。ここで、この特定のアドレスのsample-path/path/to/video.mkvセクションはURNまたは名前です。

コンストラクター

次に、URI の 5 つの異なるコンストラクターを示します。

  • URI(String st): URI は文字列 st を解析することによって構築されます。
  • URI( String skill , 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

以上がJava.net URIの詳細内容です。詳細については、PHP 中国語 Web サイトの他の関連記事を参照してください。

関連ラベル:
ソース:php
このウェブサイトの声明
この記事の内容はネチズンが自主的に寄稿したものであり、著作権は原著者に帰属します。このサイトは、それに相当する法的責任を負いません。盗作または侵害の疑いのあるコンテンツを見つけた場合は、admin@php.cn までご連絡ください。
人気のチュートリアル
詳細>
最新のダウンロード
詳細>
ウェブエフェクト
公式サイト
サイト素材
フロントエンドテンプレート
私たちについて 免責事項 Sitemap
PHP中国語ウェブサイト:福祉オンライン PHP トレーニング,PHP 学習者の迅速な成長を支援します!