【问题标题】:Simple java class to download the file简单的java类下载文件
【发布时间】:2012-04-12 01:18:08
【问题描述】:

我正在尝试使用以下代码使用简单的 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  
#

如果你们有任何灵魂,请告诉我。

谢谢
斯内哈

【问题讨论】:

  • 删除了“eclipse”标签,根据this

标签: java download runtime-error urlconnection


【解决方案1】:

您可能已经确定,那是您的虚拟机崩溃了。您通常在 Java 中所做的任何事情都不会触发这种情况,因此我将从升级您的 JVM 开始。 1.6.0_03 已经很老了。

This page 详细介绍了 Java 7 下载以及早期的 Java 6 JDK。如您所见,它们的版本为 31 次要版本,因此您相对已过时。

如果您想进一步调试,请记下上述错误报告文件并查看this SO answer

【讨论】:

    【解决方案2】:

    源文件是正确的,我试了一下,可以了。

    只有一个错误,但与您的问题无关。您正在使用readLine() 方法。当您获得该行并将其附加到 tempData 时,您会丢失行终止字符,因此请记住修复此问题,或者您更改文件。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-02-05
      • 2015-08-18
      • 1970-01-01
      • 1970-01-01
      • 2010-12-12
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多