2012-04-11 45 views
1

我試圖用簡單的Java類使用以下代碼下載文件的文件:簡單的Java類下載

public class Download { 

    public static void main(String[] args) throws Exception { 

     Download d = new Download(); 
     d.URLSetUp("http://www.sheldonbrown.com/web_sample1.html"); 
    } 

    public String URLSetUp(String urlProp) throws Exception { 

     StringBuffer tempData = new StringBuffer(); 
     String contentXML = ""; 
     String line = ""; 
     URL url1; 
     try { 
      url1 = new URL(urlProp); 

      URLConnection conn = url1.openConnection(); 

      conn.setDoOutput(true); 
      OutputStreamWriter wr = new OutputStreamWriter(conn 
       .getOutputStream()); 

      BufferedReader rd = new BufferedReader(new InputStreamReader(conn 
       .getInputStream())); 
       while ((line = rd.readLine()) != null) { 
       tempData.append(line); 
      } 
      contentXML = tempData.toString(); 

      wr.close(); 
      rd.close(); 

     } catch (MalformedURLException e) { 

      e.printStackTrace(); 
     } catch (IOException e) { 

      e.printStackTrace(); 
     } 
     if (contentXML != null) { 
      return contentXML; 
     } else { 

      System.out.println("Error"); 
     } 
     return null; 

    } 
} 

它給我以下錯誤:

# 
# An unexpected error has been detected by Java Runtime Environment: 
# 
# Internal Error (434C41535326494C453041525345520E4350500B65), pid=5156, tid=4296 
# 
# Java VM: Java HotSpot(TM) Client VM (1.6.0_03-b05 mixed mode) 
# An error report file with more information is saved as hs_err_pid5156.log 
# 
# If you would like to submit a bug report, please visit: 
# http://java.sun.com/webapps/bugreport/crash.jsp 
# 

請讓我知道如果你們有任何靈魂。

感謝
斯納

+0

刪除'eclipse'標籤,按照[this](http://meta.stackexchange.com/q/128954/179533) – ArjunShankar 2012-04-11 17:18:37

回答

2

正如你可能已經確定,那是你的虛擬機崩潰。在Java中通常不會觸發這一點,因此我會先升級您的JVM。 1.6.0_ 已經很老了。

This page詳細介紹了Java 7的下載以及更早的Java 6 JDK。正如你所看到的,他們可以使用次要版本31,所以你比較過時。

如果你想進一步調試,注意上面的錯誤報告文件,並檢查出this SO answer

1

源文件是正確的,我試了一下,它的工作原理。

只有一個錯誤,但它與您的問題無關。您正在使用readLine()方法。當你得到該行並將其附加到tempData時,將失去行終止字符,所以請記住修復此問題,或者更改文件。

+0

是的..這是因爲我在Android活動中這樣做我得到了那個錯誤,.. – Smitha 2012-04-11 11:20:28