Friday, October 17, 2014

Get various parts from URL

package javaurlget;

import java.net.MalformedURLException;
import java.net.URL;
import java.util.logging.Level;
import java.util.logging.Logger;

public class JavaURLget {

    public static void main(String[] args) {
        String src = "http://java-buddy.blogspot.com/search/label/java8";
        try {
            URL srcURL = new URL(src);
            
            System.out.println(srcURL.toString());
            System.out.println("Host: " + srcURL.getHost());
            System.out.println("Path: " + srcURL.getPath());
            System.out.println("Port: " + srcURL.getPort());
            System.out.println("Protocol: " + srcURL.getProtocol());
            System.out.println("Authority: " + srcURL.getAuthority());
            System.out.println("File: " + srcURL.getFile());
            
        } catch (MalformedURLException ex) {
            Logger.getLogger(JavaURLget.class.getName()).log(Level.SEVERE, null, ex);
        }
        
    }
    
}



No comments:

Post a Comment