Java Example - Parsing URL
The following example demonstrates how to use the net.URL class methods such as url.getProtocol(), url.getFile() to parse a URL address:
Main.java file
import java.net.URL;
public class Main {
public static void main(String[] args)
throws Exception {
URL url = new URL("http://www.example.com/html/html-tutorial.html");
System.out.println("URL is" + url.toString());
System.out.println("Protocol is" + url.getProtocol());
System.out.println("File name is" + url.getFile());
System.out.println("Host is" + url.getHost());
System.out.println("Path is" + url.getPath());
System.out.println("Port number is" + url.getPort());
System.out.println("Default port number is"
+ url.getDefaultPort());
}
}
The output of running the above code is:
URL 是 http://www.example.com/html/html-tutorial.html 协议是 http 文件名是 /html/html-tutorial.html 主机是 www.example.com 路径是 /html/html-tutorial.html 端口号是 -1 默认端口号是 80Other Extensions
Java Examples