Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/url/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java URI.resolve_Java_Url_Resolve - Fatal编程技术网

Java URI.resolve

Java URI.resolve,java,url,resolve,Java,Url,Resolve,我试图解决两个URI,但这并不像我希望的那样简单 URI a = new URI("http://www.foo.com"); URI b = new URI("bar.html"); 问题在于a.resolve(b).toString()现在是“http://www.foo.combar.html“。我怎么能逃脱呢?听起来您可能想使用URL而不是URI(URI更通用,需要处理不太严格的语法) URI的toString()的行为与您可能期望的不一样,但考虑到它的一般性质,它可能应该被原谅 遗憾

我试图解决两个URI,但这并不像我希望的那样简单

URI a = new URI("http://www.foo.com");
URI b = new URI("bar.html");

问题在于
a.resolve(b).toString()现在是
“http://www.foo.combar.html“
。我怎么能逃脱呢?

听起来您可能想使用URL而不是URI(URI更通用,需要处理不太严格的语法)

URI的toString()的行为与您可能期望的不一样,但考虑到它的一般性质,它可能应该被原谅

遗憾的是,URI的toURL()方法并不像我希望的那样能够满足您的需求

URL u = c.toURL();
u.toString()     -> "http://www.foo.combar.html"
u.getAuthority() -> "www.foo.combar.html"  --- Oh dear :(
因此,最好直接从URL开始,以获取您想要的内容:

URL x = new URL("http://www.foo.com");
URL y = new URL(x, "bar.html");
y.toString() -> "http://www.foo.com/bar.html"

确定,从URL定义中显示 路径前应该有3个斜杠(两个在方案后,一个在路径前)
2可能发生的情况:

URI中有3个斜杠=>一切正常 URI中的斜杠少于3个=>您需要在URI的末尾添加斜杠 这是我的代码快照:

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);
stringurl=”http://www.foo.com";
字符串endSlash=“”;
int indexOfSlash=0;
对于(int i=0;i0)){
如果(i>1){
endSlash=“/”;
}否则{
抛出新的MalformedURLException(“错误的给定url格式,mising://after schema”);
}
}否则{
indexOfSlash=++nextIndex;
}
}
URL rightUrl=新URL(URL+endSlash);

URI还应包含最终分隔符(“/”),以便按照您想要的方式解析:

URI a = new URI("http://www.foo.com/");

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

如果在上调用resolve的对象是一个以斜杠结尾的目录:

URL x = new URL("http://www.foo.com");
URL y = new URL(x, "bar.html");
y.toString() -> "http://www.foo.com/bar.html"
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/page1.html(缺少“to”)

如果您知道要连接的URI的第一个参数是目录,但不知道以何种格式获得它(带或不带尾随斜杠),这可能会帮助您:

static URI asDirectory(URI uri) {
    String uriString = uri.toString();
    return !uriString.endsWith("/") ? URI.create(uriString.concat("/")) : uri;
}

您期望的输出是什么?”“本来可以。为什么要使用两个URI?”?一定有原因,但还不清楚。