Код: Выделить всё
URL context = new URI("http://example.com").toURL();
for (String spec : Arrays.asList("foo", "/foo", "http://foo.bar", "\\foo")) {
System.out.println(new URL(context, spec));
System.out.println(context.toURI().resolve(spec));
}
Код: Выделить всё
http://example.com/foo
http://example.comfoo
http://example.com/foo
http://example.com/foo
http://foo.bar
http://foo.bar
http://example.com/\foo
Exception in thread "main" java.lang.IllegalArgumentException: Illegal character in path at index 0: \foo
at java.base/java.net.URI.create(URI.java:906)
at java.base/java.net.URI.resolve(URI.java:1089)
at Main.main(Main.java:10)
Caused by: java.net.URISyntaxException: Illegal character in path at index 0: \foo
at java.base/java.net.URI$Parser.fail(URI.java:2974)
at java.base/java.net.URI$Parser.checkChars(URI.java:3145)
at java.base/java.net.URI$Parser.parseHierarchical(URI.java:3227)
at java.base/java.net.URI$Parser.parse(URI.java:3186)
at java.base/java.net.URI.(URI.java:623)
at java.base/java.net.URI.create(URI.java:904)
... 2 more
Подробнее здесь: https://stackoverflow.com/questions/790 ... or-that-al