如何使用相对路径获取URL
我有一个url:
URL url=new URL("http://www.abc.com/aa/bb/cc/file.html");
和相对路径:
String relativePath="../file2.html"; //maybe is "/file3.html"
我想使用变量url
和relativePath
获取http://www.abc.com/aa/bb/file2.html
怎么做?
new URL(url, relativePath);
尝试使用类URI而不是URL来运行winth。
要从您的url获取URI:
java.net.URI anURI=url.toUri();
然后,要解析相对URI:
URI resultURI=anURI.resolve(relativePath);
最后,要获取URL类型,请使用URI方法变量的de方法toUrl() ,并且您已经获得了它。
如果要构建指向相对文件的URL,可以使用:
URL url = new URL(new URL("file:"), "./myLocalFile.txt");
构建相对URL时,请记住,base是当前包的路径,而不是文件。 我的意思是:referer文件在.../myProject/src/pckg/javafile.java
并且.../myProject/src/pckg/javafile.java
文件在.../myProject/some-other/nice.file
,那么相对引用应如下所示:
URL url = new URL(new URL("file:"), "./../some-other/nice.file");
这对我也有用:
URL url = new URL("file:./../some-other/nice.file");
这适用于我的代码
URL url=Myclass.class.getResource("/com/fnf/si/DepAcctInq_V02.wsdl");