コメントで述べたように、確認する前に URL を正規化する必要がありwww.google.com
ます。絶対 URL ではないため、正規化はアプリケーションによって異なります。以下は、URL が絶対 URL であることを確認するために使用できるコードの例です。
import java.net.URL;
public class Test {
public static void main(String [] args) throws Exception {
String [] urls = {"www.google.com",
"http://www.google.com",
"/search",
"file:/dir/file",
"file://localhost/dir/file",
"file:///dir/file"};
for (String url : urls) {
System.out.println("`" + url + "' is " +
(isAbsoluteURL(url)?"absolute":"relative"));
}
}
public static boolean isAbsoluteURL(String url)
throws java.net.MalformedURLException {
final URL baseHTTP = new URL("http://example.com");
final URL baseFILE = new URL("file:///");
URL frelative = new URL(baseFILE, url);
URL hrelative = new URL(baseHTTP, url);
System.err.println("DEBUG: file URL: " + frelative.toString());
System.err.println("DEBUG: http URL: " + hrelative.toString());
return frelative.equals(hrelative);
}
}
出力:
~$ java Test 2>/dev/null
`www.google.com' is relative
`http://www.google.com' is absolute
`/search' is relative
`file:/dir/file' is absolute
`file://localhost/dir/file' is absolute
`file:///dir/file' is absolute