URI.resolve behaves like if you are on a HTML page like http://example.org/path/to/menu.html
and click a link with href="page1.html"
: It cuts off the last segment (here menu.html
) and puts page1.html
in its place.
(http://example.org/path/to/menu.html
, page1.html
) → http://example.org/path/to/page1.html
This works also, if the object you call resolve on is a directory, denoted by ending in a slash:
(http://example.org/path/to/
, page1.html
) → http://example.org/path/to/page1.html
If it does not end in a slash, the outcome is not what you might expect:
(http://example.org/path/to
, page1.html
) → http://example.org/path/page1.html
(missing "to")
If you know that the first argument of the URIs to concatenate is a directory, but you don’t know in which format you get it (with or without trailing slash), this might help you:
static URI asDirectory(URI uri) {
String uriString = uri.toString();
return !uriString.endsWith("/") ? URI.create(uriString.concat("/")) : uri;
}