Java URI get details

Description

Java URI get details


import java.net.URI; 
import java.net.URISyntaxException; 

public class Main { 
    public static void main(String[] args) { 
        String baseURIStr = "https://www.demo2s.com/java.html?"+ "id=25&rate=5.5%25#foo"; 
        String relativeURIStr = "../network/index.html"; 

        try { //from  w ww . j  a  va  2 s  . c  o  m
            URI baseURI = new URI(baseURIStr); 
            URI relativeURI = new URI(relativeURIStr); 

            // Resolve the relative URI with respect to the base URI 
            URI resolvedURI = baseURI.resolve(relativeURI); 

            printURIDetails(baseURI); 
            printURIDetails(relativeURI); 
            printURIDetails(resolvedURI); 
        } catch (URISyntaxException e) { 
            e.printStackTrace(); 
        } 
    } 

    public static void printURIDetails(URI uri) { 
        System.out.println("URI:" + uri); 
        System.out.println("Normalized:" + uri.normalize()); 
        String parts = "[Scheme=" + uri.getScheme() 
                + ", Authority=" + uri.getAuthority() 
                + ", Path=" + uri.getPath() 
                + ", Query:" + uri.getQuery() 
                + ", Fragment:" + uri.getFragment() + "]"; 
        System.out.println(parts); 
        System.out.println(); 
    } 
} 



PreviousNext

Related