Home  >  Article  >  Java  >  Java Example - Parsing URL

Java Example - Parsing URL

黄舟
黄舟Original
2017-01-21 10:48:491564browse

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)!


Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn