4

以下代码引发以下异常:

URL u = new URL("file://D:/abc.xml");
InputStream input = u.openStream();

抛出异常:

Exception in thread "main" java.net.UnknownHostException: D
at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:177)
at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:366)
at java.net.Socket.connect(Socket.java:529)
at java.net.Socket.connect(Socket.java:478)
at sun.net.NetworkClient.doConnect(NetworkClient.java:163)
at sun.net.NetworkClient.openServer(NetworkClient.java:118)
at sun.net.ftp.FtpClient.openServer(FtpClient.java:488)
at sun.net.ftp.FtpClient.openServer(FtpClient.java:475)
at sun.net.www.protocol.ftp.FtpURLConnection.connect(FtpURLConnection.java:270)
at sun.net.www.protocol.ftp.FtpURLConnection.getInputStream(FtpURLConnection.java:352)
at java.net.URL.openStream(URL.java:1010)
at com.nuance.nlu.JavaApiExmaple.main(Exmaple.java:191)
ERROR: JDWP Unable to get JNI 1.2 environment, jvm->GetEnv() return code = -2
JDWP exit error AGENT_ERROR_NO_JNI_ENV(183):  [../../../src/share/back/util.c:820]

但是,如果我使用:

  • URL u = new URL("file:///D:/abc.xml");(文件后 3 个斜线),或:
  • URL u = new URL("file:/D:/abc.xml");(文件后的 1 个斜杠:)

这行代码InputStream input = u.openStream(); 执行得很好。

4

1 回答 1

5

首先看看:文件 URL 以三个斜杠开头的原因是什么:“file:///”等?详细说明。

摘自该答案:

完整的语法是file://host/path.

如果主机是localhost,则可以省略,导致file:///path.

于 2012-06-07T09:53:41.893 回答