Java의 Java.net.URI 클래스

이 클래스는 해당 구성 요소에서 URI 인스턴스를 생성하거나 URI 인스턴스의 다양한 구성 요소에 액세스하고 검색하기 위해 해당 구성 요소의 문자열 형식을 구문 분석하는 방법을 제공합니다. URI란 무엇입니까? URI는 통일 자원 식별자(Uniform Resource Identifier)를 의미합니다. 통일 자원 식별자(Uniform Resource Identifier)는 특정 자원을 식별하는 데 사용되는 일련의 문자입니다. 이는 특정 프로토콜을 사용하여 네트워크를 통해 리소스 표현의 상호 작용을 가능하게 합니다.

URI URL과 URN: 차이점은 무엇입니까?

마음속에 떠오르는 질문은 URI가 리소스를 식별한다면 URL은 무엇을 하는가 하는 것입니다. 사람들은 종종 이 용어를 같은 의미로 사용하지만 이는 올바르지 않습니다. 에 따르면 팀 버너스 리 'A Uniform Resource Identifier (URI) is a compact sequence of characters that identifies an abstract or physical resource.' 'A URI can be further classified as a locator a name or both. The term 'Uniform Resource Locator' (URL) refers to the subset of URI that identify resources via a representation of their primary access mechanism (e.g. their network 'location') rather than identifying the resource by name or by some other attribute(s) of that resource. The term 'Uniform Resource Name' (URN) refers to the subset of URI that are required to remain globally unique and persistent even when the resource ceases to exist or becomes unavailable.' For example
https://www.geeksforgeeks.org/java/java//url-class-java-examples/ 
Represents a URL as it tells the exact location where url class article can be found over the network.
url-class-java-examples 
Represents a URN as it does not tell anything about the location but only gives a unique name to the resource. The difference between an object of URI class and an URL class lies in the fact that a URI string is parsed only with consideration of syntax and no lookups of host is performed on creation. Comparison of two URI objects is done solely on the characters contained in the string. But on the other hand a URL string is parsed with a certain scheme and comparisons of two URL objects is performed by looking of actual resource on the net. URI 구문:
scheme:[//[user:password@]host[:port]][/]path[?query][#fragment] 
    계획 : The scheme component lays out the protocols to be associated with the URI. In some schemes the '//' are required while others dont need them.
      abc:  //admin:[email protected]:1234/path/data ?key=value&key2=value2#fragid1 
    권한 : Authority component is made of several components- authentication section a host and optional port number preceded by a ':'. Authentication section includes username and password. Host can be any ip-address.
    abc://  admin:[email protected]:1234  /path/data ?key=value&key2=value2#fragid1 
    길 : The path represents a string containing the address within the server to the resource.
    abc://admin:[email protected]:1234/  path/data   ?key=value&key2=value2#fragid1 
    쿼리 : Query represent a nonhierarchical data usually the query used for searching of a particular resource. They are separated by a '?' from the preceding part.
    abc://admin:[email protected]:1234/path/data   ?key=value&key2=value2  #fragid1 
    조각: Fragments are used to identify secondary resources as headings or subheadings within a page etc.
    abc://admin:[email protected]:1234/path/data ?key=value&key2=value2  #fragid1   
생성자:
    URI(문자열 문자열) : Constructs a URI object by parsing the specified string. The grammar used while parsing is the one specified in RFC 2396 Appendix A.
      Syntax :  public URI(String str) throws URISyntaxException   Parameters :   str : String to be parsed into URI   Throws :   URISyntaxException : If the string violates RFC 2396 
    URI(문자열 구성표 문자열 ssp 문자열 조각) : Constructs a URI from the given components. A component may be left undefined by passing null. Initially the result string is empty. If scheme is not null it is appended. Similarly the ssp and fragment part is appended if provided.
      Syntax :  public URI(String scheme String ssp String fragment) throws URISyntaxException   Parameters :    scheme : scheme name ssp : scheme-specific part fragment : fragment part   Throws :   URISyntaxException : If the URI string constructed from the given components violates RFC 2396 
    마찬가지로 생성 시 알려진 구성 요소에 따라 다른 생성자가 제공됩니다. URI(문자열 구성표 문자열 userInfo 문자열 호스트 int 포트 문자열 경로 문자열 쿼리 문자열 조각)
      Syntax :  public URI(String scheme String userInfo String host int port String path String query String fragment)   Parameters :   scheme : string representing scheme userInfo : userinfo of URI host : host component of URI port : listening port number path : path of URI query : String representing the query part fragment :optional fragment  
    URI(문자열 구성표 문자열 호스트 문자열 경로 문자열 조각)
      Syntax :  public URI(String scheme String host String path String fragment)   Parameters :   scheme : string representing scheme host : host component of URI path : path of URI fragment :optional fragment  
    URI(문자열 구성표 문자열 권한 문자열 경로 문자열 쿼리 문자열 조각)
      Syntax :  public URI(String scheme String authority String path String query String fragment)   Parameters :   scheme : string representing scheme authority : authority path : path of URI query : String representing the query part  
방법:
    만들다() : creates a new URI object. This method can be called a pseudo constructor. It is provided for use in the situations when it is known for sure that given string will parse as the URI object and it would be considered as a programmers error if it does not parse.
      Syntax :   public static URI create(String str)   Parameters :   str : String to be parsed as URI 
    ParseServerAuthority() : This method is used to parse the URI's authority components if provided into user information host and port components. This method returns a URI object whose authority field has been parsed as a server based authority.
      Syntax :   public URI parseServerAuthority() 
    정규화(): Normalizes this URI's path. URI is constructed by normalizing the URI's path which is consistent with RFC 2396. Returns a normalized URI object.
      Syntax :   public URI normalize() 
    해결하다() : Resolves the given URI with this URI. Returns a new hierarchical URI in a manner consistent with RFC 2396.
      Syntax :   public URI resolve(URI uri)   Parameters :   uri : URI to be resolved  
    Another overloaded method which takes string as argument and is equivalent to calling resolve(URI.create(str)).
      Syntax :   public URI resolve(String str)   Parameters :   str : String to be parsed as URI 
    상대화() : Relativizes the given URI against this URI.
      Syntax :   public URI relativize(URI uri) 
    매개변수: uri : 상대화할 URI toURL() : Constructs a URL from this URI.
      Syntax :   public URL toURL() throws MalformedURLException   Throws :   MalformedURLException : If error occurs while constructing URL 
    getScheme() : Returns the scheme component of the URI
      Syntax :   public String getScheme() 
    getRawSchemeSpecificPart(): Returns the raw scheme specific component of the URI.
      Syntax :   public String getRawSchemeSpecificPart() 
    getSchemeSpecificPart() : Returns the decoded scheme specific component of the URI
      Syntax :   public String getSchemeSpecificPart() 
    getRawAuthority() : Returns the authority component of the URI. If the authority is server based then further user information host and port components are returned.
      Syntax :   public String getRawAuthority() 
    getAuthority() : Returns exact similar result as of the above method except in the decoded form.
      Syntax :   public String getAuthority() 
    getRawUserInfo(): Returns the user info component of the URI or null if it is undefined.
      Syntax :   public String getRawUserInfo() 
    getUserInfo(): Returns the user info component of the URI in decoded form or null if it is undefined.
      Syntax :   public String getUserInfo() 
    자바 구현: Java
       // Java program to illustrate various   // URI class methods   import     java.net.*  ;   class   uridemo1   {      public     static     void     main  (  String  []     args  )     throws     Exception         {      String     uribase     =     'https://www.geeksforgeeks.org/'  ;      String     urirelative     =     'languages/../java'  ;      String     str     =     'https://www.google.co.in/?gws_rd=ssl#'  +  ''      +     'q=networking+in+java+geeksforgeeks'  +  ''      +  '&spf=1496918039682'  ;          // Constructor to create a new URI      // by parsing the string      URI     uriBase     =     new     URI  (  uribase  );      // create() method      URI     uri     =     URI  .  create  (  str  );          // toString() method      System  .  out  .  println  (  'Base URI = '     +     uriBase  .  toString  ());      URI     uriRelative     =     new     URI  (  urirelative  );      System  .  out  .  println  (  'Relative URI = '     +     uriRelative  .  toString  ());      // resolve() method      URI     uriResolved     =     uriBase  .  resolve  (  uriRelative  );      System  .  out  .  println  (  'Resolved URI = '     +     uriResolved  .  toString  ());      // relativized() method      URI     uriRelativized     =     uriBase  .  relativize  (  uriResolved  );      System  .  out  .  println  (  'Relativized URI = '     +     uriRelativized  .  toString  ());      // normalize() method      System  .  out  .  println  (  uri  .  normalize  ().  toString  ());      // getScheme() method      System  .  out  .  println  (  'Scheme = '     +     uri  .  getScheme  ());      // getRawShemeSpecific() method      System  .  out  .  println  (  'Raw Scheme = '     +     uri  .  getRawSchemeSpecificPart  ());      // getSchemeSpecificPart() method      System  .  out  .  println  (  'Scheme-specific part = '     +     uri  .  getSchemeSpecificPart  ());      // getRawUserInfo() method      System  .  out  .  println  (  'Raw User Info = '     +     uri  .  getRawUserInfo  ());          // getUserInfo() method      System  .  out  .  println  (  'User Info = '     +     uri  .  getUserInfo  ());      // getAuthority() method      System  .  out  .  println  (  'Authority = '     +     uri  .  getAuthority  ());      // getRawAuthority() method      System  .  out  .  println  (  'Raw Authority = '     +     uri  .  getRawAuthority  ());      }   }   
    출력 :
    Base URI = https://www.geeksforgeeks.org/ Relative URI = languages/../java Resolved URI = https://www.geeksforgeeks.org/java/java/ Relativized URI = java https://www.google.co.in/?gws_rd=ssl#q=networking+in+ java+geeksforgeeks&spf=1496918039682 Scheme = https Raw Scheme = //www.google.co.in/?gws_rd=ssl Scheme-specific part = //www.google.co.in/?gws_rd=ssl Raw User Info = null User Info = null Authority = www.google.co.in Raw Authority = www.google.co.in  
    getHost() : Returns the host component of the URI. As the host component of a URI cannot contain escaped octets hence this method does not perform any decoding.
      Syntax :   public String getHost() 
    getPort() : Returns the port number of this URI.
      Syntax :   public int getPort() 
    Getrawpath(): Returns the raw path of this URI or null if not defined.
      Syntax :   public String getRawPath() 
    getPath() : Returns the decoded path component of this URI.
      Syntax :   public String getPath() 
    getRawQuery() : Returns the query component of the URI or null if undefined.
      Syntax :   public String getRawQuery() 
    getQuery() : Returns the query component of the URI in decoded form or null if undefined.
      Syntax :   public String getQuery() 
    getRawFragment() : Returns the fragment component of the URI or null if undefined.
      Syntax :   public String getRawFragment() 
    getFragment() : Returns the decoded fragment component of this URI or null if undefined.
      Syntax :   public String getFragment() 
    비교(): Compares this URI object with another URI object. Comparison by performed according to the natural ordering with String.compareTo() methods. If one component is undefined and other is defined than first is considered smaller than second. Components to be parsed are compared in their raw form rather than their encoded form.
      Syntax :   public int compareTo(URI uri)   Parameters :   uri : URI to be compared with 
    같음() : Tests the given object with this URI. Ig the object is not a URI it returns false. For two URIs to be considered equal requires that either both are opaque or both are hierarchical. When checking for equality of different components their raw form is considered rather than the encoded form.
      Syntax :   public boolean equals(Object ob)   Parameters :   ob : object to be compared for equality 
    isAbsolute() : Returns true if this URI is absolute otherwise false. A URI is absolute if and only if it has a scheme component.
      Syntax :   public boolean isAbsolute() 
    isOpaque() : Returns true if this URI is opaque otherwise false. A URI is opaque if and only if it is absolute and its scheme-specific part does not begin with a slash character ('/')
      Syntax :   public boolean isOpaque() 
    해시코드(): Returns the hashcode for the this URI object. All the components are taken into account while creating a hashcode for the URI object.
      Syntax :   public int hashCode() 
    toString() : Returns the string representation of this URI object.
      Syntax :   public String toString() 
    toASCIIString() : Returns the string representation in ASCII format.
      Syntax :   public String toASCIIString() 
    자바 구현 : Java
       //Java Program to illustrate various   //URI class methods   import     java.net.*  ;   class   uridemo1      {      public     static     void     main  (  String  []     args  )     throws     Exception         {      String     str     =     'https://www.google.co.in/?gws_rd=ssl#'  +  ''      +     'q=networking+in+java+geeksforgeeks'  +  ''      +  '&spf=1496918039682'  ;      // Constructor to create a new URI      // by parsing the given string.      URI     uri     =     new     URI  (  str  );      // getHost() method      System  .  out  .  println  (  'Host = '     +     uri  .  getHost  ());      // getPort() method      System  .  out  .  println  (  'Port = '     +     uri  .  getPath  ());      // getRawPath() method      System  .  out  .  println  (  'Raw Path = '     +     uri  .  getRawPath  ());      // getPath() method      System  .  out  .  println  (  'Path = '     +     uri  .  getPath  ());      // getQuery() method      System  .  out  .  println  (  'Query = '     +     uri  .  getQuery  ());      // getRawQuery() method      System  .  out  .  println  (  'Raw Query = '     +     uri  .  getRawQuery  ());      // getFragment() method      System  .  out  .  println  (  'Fragment = '     +     uri  .  getFragment  ());      // getRawFragment() method      System  .  out  .  println  (  'Raw Fragment = '     +     uri  .  getRawFragment  ());      URI     uri2     =     new     URI  (  str     +     'fr'  );      // compareTo() mrthod      System  .  out  .  println  (  'CompareTo ='     +     uri  .  compareTo  (  uri2  ));      // equals() method      System  .  out  .  println  (  'Equals = '     +     uri  .  equals  (  uri2  ));      // hashcode() method      System  .  out  .  println  (  'Hashcode : '     +     uri  .  hashCode  ());      // toString() method      System  .  out  .  println  (  'toString : '     +     uri  .  toString  ());      // toASCIIString() method      System  .  out  .  println  (  'toASCIIString : '     +     uri  .  toASCIIString  ());      }   }   
    출력 :
    Host = www.google.co.in Port = / Raw Path = / Path = / Query = gws_rd=ssl Raw Query = gws_rd=ssl Fragment = q=networking+in+java+geeksforgeeks&spf=1496918039682 Raw Fragment = q=networking+in+java+geeksforgeeks&spf=1496918039682 CompareTo =-2 Equals = false Hashcode : 480379574 toString : https://www.google.co.in/?gws_rd=ssl#q=networking+ in+java+geeksforgeeks&spf=1496918039682 toASCIIString : https://www.google.co.in/?gws_rd=ssl#q= networking+in+java+geeksforgeeks&spf=1496918039682  
참고자료: 공식 Java 문서 잘못된 내용을 발견했거나 위에서 논의한 주제에 대해 더 많은 정보를 공유하고 싶다면 의견을 작성하세요. 퀴즈 만들기