Java URI.resolve
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2534124/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Java URI.resolve
提问by Cpa
I'm trying to resolve two URIs, but it's not as straightforward as I'd like it to be.
我正在尝试解析两个 URI,但这并不像我希望的那样简单。
URI a = new URI("http://www.foo.com");
URI b = new URI("bar.html");
The trouble is that a.resolve(b).toString()is now "http://www.foo.combar.html". How can I get away with that?
麻烦的a.resolve(b).toString()是现在"http://www.foo.combar.html"。我怎么能逃脱呢?
回答by Mike Tunnicliffe
Sounds like you probably want to use URL rather than URI (which is more general and needs to deal with a less strict syntax.)
听起来您可能想使用 URL 而不是 URI(这更通用,需要处理不太严格的语法。)
URI a = new URI("http://www.foo.com");
URI b = new URI("bar.html");
URI c = a.resolve(b);
c.toString() -> "http://www.foo.combar.html"
c.getAuthority() -> "www.foo.com"
c.getPath() -> "bar.html"
URI's toString() doesn't behave as you might expect, but given its general nature it may be that it should be forgiven.
URI 的 toString() 的行为与您预期的不一样,但鉴于其一般性质,它可能应该被原谅。
Sadly URI's toURL() method doesn't behave quite as I would have hoped to give you what you want.
遗憾的是,URI 的 toURL() 方法的行为并不像我希望为您提供的那样。
URL u = c.toURL();
u.toString() -> "http://www.foo.combar.html"
u.getAuthority() -> "www.foo.combar.html" --- Oh dear :(
So best just to start straight out with a URL to get what you want:
所以最好直接从一个 URL 开始以获得你想要的:
URL x = new URL("http://www.foo.com");
URL y = new URL(x, "bar.html");
y.toString() -> "http://www.foo.com/bar.html"
回答by Mohammad Amin Bandekhoda
URI should contain the final separator('/') as well to resolve the way you want:
URI 还应包含最后的分隔符 ('/') 以按照您想要的方式解析:
URI a = new URI("http://www.foo.com/");
回答by Matthias Ronge
URI.resolvebehaves like if you are on a HTML page like http://example.org/path/to/menu.htmland click a link with href="page1.html": It cuts off the last segment (here menu.html) and puts page1.htmlin its place.
URI.resolve 的行为就像您在 HTML 页面上http://example.org/path/to/menu.html单击带有以下内容的链接一样href="page1.html":它会切断最后一段(此处menu.html)并放入page1.html其位置。
(http://example.org/path/to/menu.html, page1.html) → http://example.org/path/to/page1.html
( 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:
这也适用,如果您调用 resolve 的对象是一个目录,以斜杠结尾:
(http://example.org/path/to/, page1.html) → http://example.org/path/to/page1.html
( 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")
( http://example.org/path/to, page1.html) → http://example.org/path/page1.html(缺少“到”)
If you knowthat 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:
如果您知道要连接的 URI 的第一个参数是一个目录,但您不知道以哪种格式获取它(带或不带斜杠),这可能对您有所帮助:
static URI asDirectory(URI uri) {
String uriString = uri.toString();
return !uriString.endsWith("/") ? URI.create(uriString.concat("/")) : uri;
}
回答by UltraMaster
Ok, appears from URL deffinition scheme://domain:port/path?query_string#fragment_idthere should be 3 slashes before path (two after scheme and one directly before path)
2 situation can occure:
好的,从 URL 定义中出现scheme://domain:port/path?query_string#fragment_id路径前应该有 3 个斜杠(两个在方案之后,一个直接在路径之前)
2 情况可能发生:
- there are 3 slashes in your URI => everything is OK
- 您的 URI 中有 3 个斜杠 => 一切正常
- there are less then 3 slashes in your URI => you need to add slash at the end of URI
- 您的 URI 中的斜线少于 3 个 => 您需要在 URI 的末尾添加斜线
there is my snappet of code:
有我的代码快照:
String url = "http://www.foo.com";
String endSlash="";
int indexOfSlash = 0;
for(int i = 0;i<3;i++){
int nextIndex = url.indexOf('/',indexOfSlash);
if(!(nextIndex>0)){
if(i>1){
endSlash="/";
}else{
throw new MalformedURLException("Bad given url format, mising :// after schema");
}
}else{
indexOfSlash = ++nextIndex;
}
}
URL rightUrl = new URL(url+endSlash);

