给定两条路径,如何计算从一个到另一个的相对路径?在Java中查找从一个URL到另一个URL的相对路径
我想过用奇怪的方式使用split
,但它看起来很有趣,尤其是在如下情况下:"http://foo.com/bar/baz#header"
或"http://foo.com/bar/baz"?param=value
。
一个例子是:
String url1 = "http://foo.com/bar/baz";
String url2 = "http://foo.com/bar/qux/quux/corge";
System.out.println(relative(url1, url2)); // -> "../qux/quux/corge"
从'url1'到'url2'的相对路径不会是'./qux/quux/corge'就是'../ qux/quux/corge'。 –
@JonnyHenly,你是对的。编辑我的帖子 – Brian
相关:http://stackoverflow.com/a/705963/1831987 – VGR