java.net.SocketException - 尝试获取XML

时间:2014-06-17 07:31:37

标签: java xml socketexception

我有一个简单的代码,可以从网上获取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:连接重置为   java.net.SocketInputStream.read(未知来源)at   java.net.SocketInputStream.read(未知来源)at   sun.security.ssl.InputRecord.readFully(未知来源)at   sun.security.ssl.InputRecord.read(未知来源)at   sun.security.ssl.SSLSocketImpl.readRecord(未知来源)at   sun.security.ssl.SSLSocketImpl.waitForClose(未知来源)at   sun.security.ssl.HandshakeOutStream.flush(未知来源)at   sun.security.ssl.Handshaker.kickstart(未知来源)at   sun.security.ssl.SSLSocketImpl.kickstartHandshake(未知来源)at   sun.security.ssl.SSLSocketImpl.performInitialHandshake(未知来源)     在sun.security.ssl.SSLSocketImpl.startHandshake(未知来源)at   sun.security.ssl.SSLSocketImpl.startHandshake(未知来源)at   sun.net.www.protocol.https.HttpsClient.afterConnect(未知来源)     在   sun.net.www.protocol.https.AbstractDelegateHttpsURLConnection.connect(未知   来源)at   sun.net.www.protocol.http.HttpURLConnection.getInputStream0(未知   来源)at   sun.net.www.protocol.http.HttpURLConnection.getInputStream(未知   来源)at   sun.net.www.protocol.https.HttpsURLConnectionImpl.getInputStream(未知   源)

1 个答案:

答案 0 :(得分:-1)

每次我想要下载文件时,我都会使用此代码(或者删除)

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());                                                                           
}    

快速又脏,也许这会有所帮助。