结果
上面的代码示例将产生以下结果。
URL is http://www.server.com
protocol is TCP/IP
file name is java_program.txt
host is 122.45.2.36
path is
port is 2
default port is 1
以下是获取 URL 部分的另一个示例?
import java.net.URL;
public class NewClass {
public static void main(String args[]) throws Exception {
URL u = new URL("https://www.cainiaoya.com/javaexamples/net_singleuser.htm");
System.out.println("The URL is " + u);
System.out.println("The file part is " + u.getFile());
System.out.println("host is " + u.getHost());
System.out.println("path is " + u.getPath());
System.out.println("port is " + u.getPort());
System.out.println("default port is " + u.getDefaultPort());
}
}
上面的代码示例将产生以下结果。
The URL is https://www.cainiaoya.com/javaexamples/net_singleuser.htm
The file part is /javaexamples/net_singleuser.htm
host is www.cainiaoya.com
path is /javaexamples/net_singleuser.htm
port is -1
default port is 443