gpt4 book ai didi

java.net.SocketException - 尝试获取 XML

转载 作者:行者123 更新时间:2023-12-01 06:16:05 25 4
gpt4 key购买 nike

我有一个简单的代码,可以从网络上获取 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());
}

}

The error is as following:

java.net.SocketException: Connection reset at java.net.SocketInputStream.read(Unknown Source) at java.net.SocketInputStream.read(Unknown Source) at sun.security.ssl.InputRecord.readFully(Unknown Source) at sun.security.ssl.InputRecord.read(Unknown Source) at sun.security.ssl.SSLSocketImpl.readRecord(Unknown Source) at sun.security.ssl.SSLSocketImpl.waitForClose(Unknown Source) at sun.security.ssl.HandshakeOutStream.flush(Unknown Source) at sun.security.ssl.Handshaker.kickstart(Unknown Source) at sun.security.ssl.SSLSocketImpl.kickstartHandshake(Unknown Source) at sun.security.ssl.SSLSocketImpl.performInitialHandshake(Unknown Source) at sun.security.ssl.SSLSocketImpl.startHandshake(Unknown Source) at sun.security.ssl.SSLSocketImpl.startHandshake(Unknown Source) at sun.net.www.protocol.https.HttpsClient.afterConnect(Unknown Source) at sun.net.www.protocol.https.AbstractDelegateHttpsURLConnection.connect(Unknown Source) at sun.net.www.protocol.http.HttpURLConnection.getInputStream0(Unknown Source) at sun.net.www.protocol.http.HttpURLConnection.getInputStream(Unknown Source) at sun.net.www.protocol.https.HttpsURLConnectionImpl.getInputStream(Unknown Source)

最佳答案

每次我想下载文件时,我都会使用此代码(或衍生版本)

try {   
URL url;
url = new URL("http://YourWebSourceXML.xml");


InputStream in = url.openConnection().getInputStream();
OutputStream out = new FileOutputStream("YourDestFile.xml");
byte[] buffer = new byte[1024];
for (int n;(n = in.read(buffer)) != -1;
out.write(buffer, 0, n));

in.close();
out.close(); }
catch (IOException e){
// Do something
}

编辑:

我第一次尝试就成功了:

 try {

URL url = new URL("http://www.dnb.no/portalfront/datafiles/miscellaneous/csv/kursliste_ws.xml");
BufferedReader in = new BufferedReader(new InputStreamReader(url.openStream()));


FileOutputStream fout = new FileOutputStream("x.xml");

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 - 尝试获取 XML,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24258118/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com