我有一个简单的代码,从Web中获取XML文件 . 更具体地说,是一个来自挪威银行的汇率的XML文件 . 问题是,它有时只能起作用 . 通常,它永远不会第一次工作 .
经过一些测试,我很确定它在 InputStreamReader(url.openStream()) 上崩溃了 . 但正如所说,不是每一次 .
那么,是否有更好的方法来确保它正确地获取XML文件
所有帮助都是适用的 .
public class xmlReader {
private static Properties intProps;
public static void main(String[] args) {
intProps = new Properties();
intProps.setProperty("SOURCEURL", "https://www.dnb.no/portalfront/datafiles/miscellaneous/csv/kursliste_ws.xml");
intProps.setProperty("SOURCEDIR", "C:\\Users\\");
intProps.setProperty("SOURCEFILE", "EXCHANGERATES_#YYYYMMDD#_#U#.xml");
fetchURL();
}
private static void fetchURL() {
try {
URL url = new URL(intProps.getProperty("SOURCEURL"));
BufferedReader in = new BufferedReader(new InputStreamReader(url.openStream()));
FileOutputStream fout = new FileOutputStream(intProps.getProperty("SOURCEDIR") + intProps.getProperty("SOURCEFILE"));
String inputLine;
while ((inputLine = in.readLine()) != null) {
System.out.println("Line: " + inputLine);
fout.write(inputLine.getBytes());
}
fout.close();
in.close();
} catch(Exception e) {
e.printStackTrace();
// writeLog("Error fetching from URL: " + e.getMessage());
}
}
错误如下:java.net.SocketException:在sun.security.ssl.InputRecord.readFully(java.net.SocketInputStream.read(未知来源)的java.net.SocketInputStream.read(未知来源)处重置连接 . 未知来源)sun.security.ssl.InputRecord.read(未知来源)sun.security.ssl.SSLSocketImpl.readRecord(未知来源)sun.security.ssl.SSLSocketImpl.waitForClose(未知来源)sun.security . ssl.HandshakeOutStream.flush(未知来源)at sun.security.ssl.Handshaker.kickstart(未知来源)sun.security.ssl.SSLSocketImpl.kickstartHandshake(未知来源)sun.security.ssl.SSLSocketImpl.performInitialHandshake(未知来源) )sun.security.ssl.SSLSocketImpl.startHandshake(未知来源)at sun.security.ssl.SSLSocketImpl.startHandshake(未知来源)at sun.net.www.protocol.https.HttpsClient.afterConnect(Unknown Source)at sun . net.net www.protocol.https.AbstractDelegateHttpsURLConnection.connect(未知来源)at sun.net.www.protocol.http.HttpURLConn sun.net.www.protocol.http.HttpURLConnection.getInputStream(未知来源)的sun.net.www.protocol.https.HttpsURLConnectionImpl.getInputStream(未知来源)中的ection.getInputStream0(未知来源)