サンプルコード。


import java.net.*;
 
public class JavaNetUrl {
 
  public static void main(String[] args) throws Exception{
    // env
    System.out.println("java.version=" + System.getProperty("java.version"));
    System.out.println("java.vendor=" + System.getProperty("java.vendor"));
    System.out.println("java.vm.name=" + System.getProperty("java.vm.name"));
    System.out.println("java.vm.version=" + System.getProperty("java.vm.version"));
    System.out.println("java.vm.vendor=" + System.getProperty("java.vm.vendor"));
    System.out.println("");
    // test
    print("http://www.nilab.info/a/b?c=d#e");
    print("http://www.nilab.info:80/a/b?c=d#e");
    print("http://www.nilab.info:8080/a/b?c=d#e");
    print("https://www.nilab.info/a/b?c=d#e");
  }
 
  private static void print(String str) throws Exception {
    URL url = new URL(str);
    String s = "";
    s += "***** " + str + "*****\n";
    s += "getAuthority: " + url.getAuthority() + "\n";
    s += "getDefaultPort: " + url.getDefaultPort() + "\n";
    s += "getFile: " + url.getFile() + "\n";
    s += "getHost: " + url.getHost() + "\n";
    s += "getPath: " + url.getPath() + "\n";
    s += "getPort: " + url.getPort() + "\n";
    s += "getProtocol: " + url.getProtocol() + "\n";
    s += "getQuery: " + url.getQuery() + "\n";
    s += "getRef: " + url.getRef() + "\n";
    s += "getUserInfo: " + url.getUserInfo() + "\n";
    s += "toExternalForm: " + url.toExternalForm() + "\n";
    s += "toString: " + url.toString() + "\n";
    s += "toURI: " + url.toURI() + "\n";
    System.out.println(s);
  }
}

実行結果。ポート番号を指定していないURLの場合に、getPortメソッドの戻り値が-1になるとは知らなかった。


java.version=1.6.0_33
java.vendor=Apple Inc.
java.vm.name=Java HotSpot(TM) 64-Bit Server VM
java.vm.version=20.8-b03-424
java.vm.vendor=Apple Inc.
 
***** http://www.nilab.info/a/b?c=d#e*****
getAuthority: www.nilab.info
getDefaultPort: 80
getFile: /a/b?c=d
getHost: www.nilab.info
getPath: /a/b
getPort: -1
getProtocol: http
getQuery: c=d
getRef: e
getUserInfo: null
toExternalForm: http://www.nilab.info/a/b?c=d#e
toString: http://www.nilab.info/a/b?c=d#e
toURI: http://www.nilab.info/a/b?c=d#e
 
***** http://www.nilab.info:80/a/b?c=d#e*****
getAuthority: www.nilab.info:80
getDefaultPort: 80
getFile: /a/b?c=d
getHost: www.nilab.info
getPath: /a/b
getPort: 80
getProtocol: http
getQuery: c=d
getRef: e
getUserInfo: null
toExternalForm: http://www.nilab.info:80/a/b?c=d#e
toString: http://www.nilab.info:80/a/b?c=d#e
toURI: http://www.nilab.info:80/a/b?c=d#e
 
***** http://www.nilab.info:8080/a/b?c=d#e*****
getAuthority: www.nilab.info:8080
getDefaultPort: 80
getFile: /a/b?c=d
getHost: www.nilab.info
getPath: /a/b
getPort: 8080
getProtocol: http
getQuery: c=d
getRef: e
getUserInfo: null
toExternalForm: http://www.nilab.info:8080/a/b?c=d#e
toString: http://www.nilab.info:8080/a/b?c=d#e
toURI: http://www.nilab.info:8080/a/b?c=d#e
 
***** https://www.nilab.info/a/b?c=d#e*****
getAuthority: www.nilab.info
getDefaultPort: 443
getFile: /a/b?c=d
getHost: www.nilab.info
getPath: /a/b
getPort: -1
getProtocol: https
getQuery: c=d
getRef: e
getUserInfo: null
toExternalForm: https://www.nilab.info/a/b?c=d#e
toString: https://www.nilab.info/a/b?c=d#e
toURI: https://www.nilab.info/a/b?c=d#e

ちなみに、開発&実行環境は Mac OS X Lion (MacBook Air) 上の Eclipse Indigo。

Ref. URL (Java Platform SE 6)

tags: java:

Posted by NI-Lab. (@nilab)