file:// 方法上的 java.net.UnknownHostException
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2680837/
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.net.UnknownHostException on file:// method
提问by Vidar Kongsli
failed to open file file://D/:/dev/test_all.html JavaException: java.net.UnknownHostException: D
Any ideas for why this happens?
为什么会发生这种情况的任何想法?
回答by Salandur
the third / is in the wrong place, the file url is contructed with file:///<path>
第三个 / 在错误的地方,文件 url 是用 file:///<path>
回答by Etaoin
Your URL is malformed. Instead of file://D/:/you want file://D:/-- no slash between the drive letter and the colon.
您的网址格式错误。而不是file://D/:/你想要file://D:/- 驱动器号和冒号之间没有斜线。
回答by Alessandro S.
Here is my solution that's actually working with XMLParserv2, I hope this helps:
这是我实际使用 XMLParserv2 的解决方案,我希望这会有所帮助:
protected URL createURL(String filename){
URL url = null;
try {
url = new URL("file://" + System.getProperty("user.dir") + File.separator + filename);
} catch (MalformedURLException e) {
System.out.println(e.getMessage());
e.printStackTrace();
}
return url;
}

