The following examples demonstrate how to use the url.getProtocol(), url.getFile() and other methods of the net.URL class to parse URL addresses:
/* author by w3cschool.cc Main.java */import java.net.URL;public class Main { public static void main(String[] args) throws Exception { URL url = new URL("http://www.w3cschool.cc/html/html-tutorial.html"); System.out.println("URL 是 " + url.toString()); System.out.println("协议是 " + url.getProtocol()); System.out.println("文件名是 " + url.getFile()); System.out.println("主机是 " + url.getHost()); System.out.println("路径是 " + url.getPath()); System.out.println("端口号是 " + url.getPort()); System.out.println("默认端口号是 " + url.getDefaultPort()); }}
The above code runs the output results For:
URL 是 协议是 http文件名是 /html/html-tutorial.html 主机是 路径是 /html/html-tutorial.html 端口号是 -1默认端口号是 80
The above is the Java example - parsing the content of the URL. For more related content, please pay attention to the PHP Chinese website (www.php.cn)!