1
我在应用程序正在使用的库中遇到了一个错误。我已经将问题缩小到java.net.URL
类的行为以及图书馆如何使用它来查找资源。为什么java.io.URL去掉最后一个文件夹?
当我运行下面的代码,输出是file:/root/myfile.xml
代替file:/root/folder/myfile.xml
。
public static void main(String[] args) throws MalformedURLException {
String location = "file:///root/folder";
String spec = "myfile.xml";
URL context = new URL(location);
URL url = new URL(context, spec);
System.out.println(url);
}
如果我在location
附加一个斜杠,我会得到预期的输出。
我很好奇,为什么java.net.URL
这样的表现?
///可能工作更美好。 – bmargulies 2012-02-25 00:23:04
@bmargulies谢谢。我更新了这个问题,虽然行为是一样的。 – AEW 2012-02-25 00:29:33
我想你只需要在位置字符串的末尾保留'/'... – summea 2012-02-25 00:30:29