在RESIN WEB服务中报的错,代码很简单:docBuilder.parse(fileName),平时在公司运行挺好的,某一天我把它COPY到笔记本上时就报错了,错误堆栈如下:
DocumentBuilder.parse 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.ja
va:270)
at sun.net.www.protocol.ftp.FtpURLConnection.getInputStream(FtpURLConnec
tion.java:352)
at org.apache.xerces.impl.XMLEntityManager.setupCurrentEntity(Unknown So
urce)
at org.apache.xerces.impl.XMLVersionDetector.determineDocVersion(Unknown
Source)
at org.apache.xerces.parsers.XML11Configuration.parse(Unknown Source)
at org.apache.xerces.parsers.XML11Configuration.parse(Unknown Source)
at org.apache.xerces.parsers.XMLParser.parse(Unknown Source)
at org.apache.xerces.parsers.DOMParser.parse(Unknown Source)
at org.apache.xerces.jaxp.DocumentBuilderImpl.parse(Unknown Source)
at javax.xml.parsers.DocumentBuilder.parse(DocumentBuilder.java:180)
at xxx.xxx.xxx(Unknown Source)
at java.lang.Thread.run(Thread.java:662)
一开始以为是解释器要用FTP去网上下载DTD文件,但仔细看了下XML文件的内容,里面也没有DTD之类的声明。
仔细想了下,有一点较奇怪的是,XML在整个SERVLET里用了很多,但其它地方都没问题,只有这一处出错。于是找了其它地方的代码来比较,一比之下,发现问题了:原来其它地方传给DocumentBuilder的都是InputStream,只有这里是传文件名;这个文件位于D盘,其路径大约是//D:/xxx/yy/zzz.xml,看起来很像个URI,于是DocumentBuilder把它当为URI去网上下载了。
解决办法也很简单,把docBuilder.parse(fileName)改成docBuilder.parse(new FileInputStream(fileName))就OK了。