Java : URL - API使用例
URL (Java SE 21 & JDK 21) の使用例まとめです。
だいたいのメソッドを網羅済みです。
API仕様書のおともにどうぞ。
概要
関連するクラスとして URIクラスがあります。
「Java API 使用例 : URI」も参考にしていただけたら幸いです。
final var url = URI.create("https://example.com/java/index.html").toURL();
System.out.println(url); // "https://example.com/java/index.html"
System.out.println(url.getProtocol()); // "https"
System.out.println(url.getHost()); // "example.com"
System.out.println(url.getPath()); // "/java/index.html"
コンストラクタ
URL (String spec)
非推奨です。
URL (String protocol, String host, int port, String file)
非推奨です。
URL (String protocol, String host, int port, String file, URLStreamHandler handler)
非推奨です。
URL (String protocol, String host, String file)
非推奨です。
URL (URL context, String spec)
非推奨です。
URL (URL context, String spec, URLStreamHandler handler)
非推奨です。
メソッド
boolean equals (Object obj)
final var url1 = URI.create("http://example.com/docs/resource1.html").toURL();
final var url2 = URI.create("http://example.com/docs/resource1.html").toURL();
System.out.println(url1.equals(url2)); // true
// ホスト名の大文字・小文字の区別はしません。
final var url1 = URI.create("http://example.com/").toURL();
final var url2 = URI.create("HTTP://EXAMPLE.COM/").toURL();
System.out.println(url1.equals(url2)); // true
final var url1 = URI.create("http://example.com/a").toURL();
final var url2 = URI.create("http://example.com/b").toURL();
System.out.println(url1.equals(url2)); // false
// IPアドレスで一致していれば true となります。
final var url1 = URI.create("http://example.com").toURL();
final var url2 = URI.create("http://www.example.com").toURL();
System.out.println(url1.equals(url2)); // true
String getAuthority ()
final var url1 = URI.create("http://www.example.com/docs/resource1.html").toURL();
System.out.println(url1.getAuthority()); // "www.example.com"
System.out.println(url1.getHost()); // "www.example.com"
final var url2 = URI.create("http://user-a@example.com:80/").toURL();
System.out.println(url2.getAuthority()); // "user-a@example.com:80"
System.out.println(url2.getHost()); // "example.com"
final var url3 = URI.create("https://example.com/index.html?query#fragment").toURL();
System.out.println(url3.getAuthority()); // "example.com"
System.out.println(url3.getHost()); // "example.com"
final var url4 = URI.create("file:///~/calendar").toURL();
System.out.println(url4.getAuthority()); // ""
System.out.println(url4.getHost()); // ""
final var url5 = URI.create("jar:file:/home/xxx/sample.jar!/sample.txt").toURL();
System.out.println(url5.getAuthority()); // null
System.out.println(url5.getHost()); // ""
final Object getContent ()
※どの Content-Type がなんのクラスを返すのか…そのあたりの仕様を見つけられていません。
とりあえず text/html は InputStream を返すようです。
疑問点が解決したら更新するかもです。
final var url = URI.create("http://example.com").toURL();
final var content = url.getContent();
if (content instanceof InputStream is) {
try (final var reader = new BufferedReader(
new InputStreamReader(is))) {
reader.lines().forEach(System.out::println);
}
}
// 結果
// ↓
//<!doctype html>
//<html>
//<head>
// <title>Example Domain</title>
// ...
//</html>
final Object getContent (Class<?>[] classes)
final var url = URI.create("http://example.com").toURL();
final Class<?>[] classes = {InputStream.class};
final var content = url.getContent(classes);
if (content instanceof InputStream is) {
try (final var reader = new BufferedReader(
new InputStreamReader(is))) {
reader.lines().forEach(System.out::println);
}
}
// 結果
// ↓
//<!doctype html>
//<html>
//<head>
// <title>Example Domain</title>
// ...
//</html>
int getDefaultPort ()
final var uri1 = URI.create("http://example.com/").toURL();
System.out.println(uri1.getDefaultPort()); // 80
System.out.println(uri1.getPort()); // -1
final var uri2 = URI.create("http://example.com:9999/").toURL();
System.out.println(uri2.getDefaultPort()); // 80
System.out.println(uri2.getPort()); // 9999
final var uri3 = URI.create("https://example.com/").toURL();
System.out.println(uri3.getDefaultPort()); // 443
System.out.println(uri3.getPort()); // -1
final var uri4 = URI.create("ftp://example.com/").toURL();
System.out.println(uri4.getDefaultPort()); // 21
System.out.println(uri4.getPort()); // -1
final var uri5 = URI.create("file:///~/calendar").toURL();
System.out.println(uri5.getDefaultPort()); // -1
System.out.println(uri5.getPort()); // -1
final var uri6 = URI.create("jar:file:/home/xxx/sample.jar!/sample.txt").toURL();
System.out.println(uri6.getDefaultPort()); // -1
System.out.println(uri6.getPort()); // -1
String getFile ()
final var url1 = URI.create("http://www.example.com/docs/resource1.html").toURL();
System.out.println(url1.getFile()); // "/docs/resource1.html"
System.out.println(url1.getPath()); // "/docs/resource1.html"
final var url2 = URI.create("https://example.com/index.html?query#fragment").toURL();
System.out.println(url2.getFile()); // "/index.html?query"
System.out.println(url2.getPath()); // "/index.html"
final var url3 = URI.create("http://user-a@example.com:80/").toURL();
System.out.println(url3.getFile()); // "/"
System.out.println(url3.getPath()); // "/"
final var url4 = URI.create("file:///~/calendar").toURL();
System.out.println(url4.getFile()); // "/~/calendar"
System.out.println(url4.getPath()); // "/~/calendar"
final var url5 = URI.create("jar:file:/home/xxx/sample.jar!/sample.txt").toURL();
System.out.println(url5.getFile()); // "file:/home/xxx/sample.jar!/sample.txt"
System.out.println(url5.getPath()); // "file:/home/xxx/sample.jar!/sample.txt"
String getHost ()
final var url1 = URI.create("http://www.example.com/docs/resource1.html").toURL();
System.out.println(url1.getAuthority()); // "www.example.com"
System.out.println(url1.getHost()); // "www.example.com"
final var url2 = URI.create("http://user-a@example.com:80/").toURL();
System.out.println(url2.getAuthority()); // "user-a@example.com:80"
System.out.println(url2.getHost()); // "example.com"
final var url3 = URI.create("https://example.com/index.html?query#fragment").toURL();
System.out.println(url3.getAuthority()); // "example.com"
System.out.println(url3.getHost()); // "example.com"
final var url4 = URI.create("file:///~/calendar").toURL();
System.out.println(url4.getAuthority()); // ""
System.out.println(url4.getHost()); // ""
final var url5 = URI.create("jar:file:/home/xxx/sample.jar!/sample.txt").toURL();
System.out.println(url5.getAuthority()); // null
System.out.println(url5.getHost()); // ""
String getPath ()
final var url1 = URI.create("http://www.example.com/docs/resource1.html").toURL();
System.out.println(url1.getFile()); // "/docs/resource1.html"
System.out.println(url1.getPath()); // "/docs/resource1.html"
final var url2 = URI.create("https://example.com/index.html?query#fragment").toURL();
System.out.println(url2.getFile()); // "/index.html?query"
System.out.println(url2.getPath()); // "/index.html"
final var url3 = URI.create("http://user-a@example.com:80/").toURL();
System.out.println(url3.getFile()); // "/"
System.out.println(url3.getPath()); // "/"
final var url4 = URI.create("file:///~/calendar").toURL();
System.out.println(url4.getFile()); // "/~/calendar"
System.out.println(url4.getPath()); // "/~/calendar"
final var url5 = URI.create("jar:file:/home/xxx/sample.jar!/sample.txt").toURL();
System.out.println(url5.getFile()); // "file:/home/xxx/sample.jar!/sample.txt"
System.out.println(url5.getPath()); // "file:/home/xxx/sample.jar!/sample.txt"
int getPort ()
final var uri1 = URI.create("http://example.com/").toURL();
System.out.println(uri1.getDefaultPort()); // 80
System.out.println(uri1.getPort()); // -1
final var uri2 = URI.create("http://example.com:9999/").toURL();
System.out.println(uri2.getDefaultPort()); // 80
System.out.println(uri2.getPort()); // 9999
final var uri3 = URI.create("https://example.com/").toURL();
System.out.println(uri3.getDefaultPort()); // 443
System.out.println(uri3.getPort()); // -1
final var uri4 = URI.create("ftp://example.com/").toURL();
System.out.println(uri4.getDefaultPort()); // 21
System.out.println(uri4.getPort()); // -1
final var uri5 = URI.create("file:///~/calendar").toURL();
System.out.println(uri5.getDefaultPort()); // -1
System.out.println(uri5.getPort()); // -1
final var uri6 = URI.create("jar:file:/home/xxx/sample.jar!/sample.txt").toURL();
System.out.println(uri6.getDefaultPort()); // -1
System.out.println(uri6.getPort()); // -1
String getProtocol ()
final var url1 = URI.create("http://www.example.com/docs/resource1.html").toURL();
System.out.println(url1.getProtocol()); // "http"
final var url2 = URI.create("https://example.com/index.html?query#fragment").toURL();
System.out.println(url2.getProtocol()); // "https"
final var url3 = URI.create("file:///~/calendar").toURL();
System.out.println(url3.getProtocol()); // "file"
final var url4 = URI.create("jar:file:/home/xxx/sample.jar!/sample.txt").toURL();
System.out.println(url4.getProtocol()); // "jar"
String getQuery ()
final var url1 = URI.create("http://www.example.com/docs/resource1.html").toURL();
System.out.println(url1.getQuery()); // null
final var url2 = URI.create("https://example.com/index.html?query#fragment").toURL();
System.out.println(url2.getQuery()); // "query"
final var url3 = URI.create("https://example.com/index.html?aaa&bbb&ccc").toURL();
System.out.println(url3.getQuery()); // "aaa&bbb&ccc"
final var url4 = URI.create("jar:file:/home/xxx/sample.jar!/sample.txt").toURL();
System.out.println(url4.getQuery()); // null
String getRef ()
final var url1 = URI.create("http://www.example.com/docs/resource1.html").toURL();
System.out.println(url1.getRef()); // null
final var url2 = URI.create("https://example.com/index.html?query#fragment").toURL();
System.out.println(url2.getRef()); // "fragment"
final var url3 = URI.create("https://example.com/#aaaa").toURL();
System.out.println(url3.getRef()); // "aaaa"
final var url4 = URI.create("jar:file:/home/xxx/sample.jar!/sample.txt").toURL();
System.out.println(url4.getRef()); // null
String getUserInfo ()
final var url1 = URI.create("http://example.com/").toURL();
System.out.println(url1.getUserInfo()); // null
final var url2 = URI.create("http://user-a@example.com:80/").toURL();
System.out.println(url2.getUserInfo()); // "user-a"
int hashCode ()
final var url1 = URI.create("http://example.com/").toURL();
final var url2 = URI.create("http://example.com/").toURL();
System.out.println(url1.hashCode()); // 1575608341
System.out.println(url2.hashCode()); // 1575608341
// ホスト名の大文字・小文字の区別はしません。
final var url1 = URI.create("http://example.com/").toURL();
final var url2 = URI.create("HTTP://EXAMPLE.COM/").toURL();
System.out.println(url1.hashCode()); // 1575608341
System.out.println(url2.hashCode()); // 1575608341
final var url1 = URI.create("http://example.com/a").toURL();
final var url2 = URI.create("http://example.com/b").toURL();
System.out.println(url1.hashCode()); // 1575609848
System.out.println(url2.hashCode()); // 1575609849
final var url1 = URI.create("http://example.com").toURL();
final var url2 = URI.create("http://www.example.com").toURL();
System.out.println(url1.equals(url2)); // true
System.out.println(url1.hashCode()); // 1575608294
System.out.println(url2.hashCode()); // 1575608294
static URL of (URI uri, URLStreamHandler handler)
class DummyURLStreamHandler extends URLStreamHandler {
@Override
protected URLConnection openConnection(URL u) {
System.out.println("dummy : openConnection url = " + u);
return new URLConnection(u) {
@Override
public void connect() {
System.out.println("dummy : connect!");
}
};
}
}
final var url = URL.of(new URI("http://example.com"),
new DummyURLStreamHandler());
System.out.println(url); // "http://example.com"
final var connection = url.openConnection();
connection.connect();
// 結果
// ↓
//dummy : openConnection url = http://example.com
//dummy : connect!
URLConnection openConnection ()
final var url = URI.create("http://example.com").toURL();
final var connection = url.openConnection();
try (final var reader = new BufferedReader(
new InputStreamReader(connection.getInputStream()))) {
reader.lines().forEach(System.out::println);
}
// 結果
// ↓
//<!doctype html>
//<html>
//<head>
// <title>Example Domain</title>
// ...
//</html>
URLConnection openConnection (Proxy proxy)
final var url = URI.create("http://example.com").toURL();
final var proxy = new Proxy(Proxy.Type.HTTP,
new InetSocketAddress("proxy.example.com", 8080));
final var connection = url.openConnection(proxy);
try (final var reader = new BufferedReader(
new InputStreamReader(connection.getInputStream()))) {
reader.lines().forEach(System.out::println);
}
// 結果
// ↓
//<!doctype html>
//<html>
//<head>
// <title>Example Domain</title>
// ...
//</html>
final InputStream openStream ()
final var url = URI.create("http://example.com").toURL();
try (final var reader = new BufferedReader(
new InputStreamReader(url.openStream()))) {
reader.lines().forEach(System.out::println);
}
// 結果
// ↓
//<!doctype html>
//<html>
//<head>
// <title>Example Domain</title>
// ...
//</html>
boolean sameFile (URL other)
final var url1 = URI.create("http://example.com/").toURL();
final var url2 = URI.create("http://example.com/").toURL();
System.out.println(url1.sameFile(url2)); // true
// 参照(フラグメント) は除外して比較されます。
final var url1 = URI.create("http://example.com/#aaa").toURL();
final var url2 = URI.create("http://example.com/#bbb").toURL();
System.out.println(url1.sameFile(url2)); // true
final var url1 = URI.create("http://example.com/?aaa").toURL();
final var url2 = URI.create("http://example.com/?bbb").toURL();
System.out.println(url1.sameFile(url2)); // false
// ホスト名の大文字・小文字の区別はしません。
final var url1 = URI.create("http://example.com/").toURL();
final var url2 = URI.create("HTTP://EXAMPLE.COM/").toURL();
System.out.println(url1.sameFile(url2)); // true
final var url1 = URI.create("http://example.com/a").toURL();
final var url2 = URI.create("http://example.com/b").toURL();
System.out.println(url1.sameFile(url2)); // false
// IPアドレスで一致していれば true となります。
final var url1 = URI.create("http://example.com/").toURL();
final var url2 = URI.create("http://www.example.com/").toURL();
System.out.println(url1.sameFile(url2)); // true
static void setURLStreamHandlerFactory (URLStreamHandlerFactory fac)
class DummyURLStreamHandler extends URLStreamHandler {
@Override
protected URLConnection openConnection(URL u) {
System.out.println("dummy : openConnection url = " + u);
return new URLConnection(u) {
@Override
public void connect() {
System.out.println("dummy : connect!");
}
};
}
}
URL.setURLStreamHandlerFactory(protocol -> {
System.out.println("createURLStreamHandler! : " + protocol);
return new DummyURLStreamHandler();
});
final var url = URI.create("http://example.com/").toURL();
final var connection = url.openConnection();
connection.connect();
// 結果
// ↓
//createURLStreamHandler! : http
//dummy : openConnection url = http://example.com/
//dummy : connect!
String toExternalForm ()
final var url1 = URI.create("http://www.example.com/docs/resource1.html").toURL();
System.out.println(url1.toExternalForm()); // "http://www.example.com/docs/resource1.html"
final var url2 = URI.create("https://example.com/index.html?query#fragment").toURL();
System.out.println(url2.toExternalForm()); // "https://example.com/index.html?query#fragment"
final var url3 = URI.create("file:///~/calendar").toURL();
System.out.println(url3.toExternalForm()); // "file:/~/calendar"
final var url4 = URI.create("jar:file:/home/xxx/sample.jar!/sample.txt").toURL();
System.out.println(url4.toExternalForm()); // "jar:file:/home/xxx/sample.jar!/sample.txt"
String toString ()
結果は toExternalForm() と同じになります。
API使用例は toExternalForm のメソッドをご参照ください。
結果は同じ。ならどちらを使ったほうがよいのか…
外部サイトですが参考までに。
Is there a semantic difference between toExternalForm and toString on java.net.URL? - Stack Overflow
URI toURI ()
final var url1 = URI.create("http://www.example.com/docs/resource1.html").toURL();
System.out.println(url1.toURI()); // "http://www.example.com/docs/resource1.html"
final var url2 = URI.create("https://example.com/index.html?query#fragment").toURL();
System.out.println(url2.toURI()); // "https://example.com/index.html?query#fragment"
final var url3 = URI.create("file:///~/calendar").toURL();
System.out.println(url3.toURI()); // "file:/~/calendar"
final var url4 = URI.create("jar:file:/home/xxx/sample.jar!/sample.txt").toURL();
System.out.println(url4.toURI()); // "jar:file:/home/xxx/sample.jar!/sample.txt"
// エスケープの例。
final var url = URI.create("http://example.com/a%20b%20c/").toURL();
System.out.println(url.toURI()); // "http://example.com/a%20b%20c/"
System.out.println(url.toURI().getPath()); // "/a b c/"