2013-02-28 44 views
0

我有一個示例代碼來從網絡和它的一個swing應用程序下載一些數據(pdf,gif和mp3)。它在Windows 7中完美工作,但不能在Windows XP中工作。擺動下載不工作在Windows XP中

我的代碼是

public static Float downloadFile(String targetUrl,File filePath) 
     { 
      try 
      { 

       Integer count=0; 
       URL url = new URL(targetUrl); 
       HttpURLConnection connection=(HttpURLConnection) url.openConnection(); 
       connection.setRequestMethod("GET"); 
       connection.setDoOutput(true); 
       connection.setConnectTimeout(10000); 
       connection.connect(); 

       int lenghtOfFile = connection.getContentLength(); 
       Thread.sleep(100); 
       InputStream input = new BufferedInputStream(url.openStream()); 
       Thread.sleep(100); 
       OutputStream output = new FileOutputStream(filePath); 
       byte data[] = new byte[input.available()]; 
       long total = 0; 
       while ((count = input.read(data)) != -1) 
       { 
        total += count; 
        output.write(data, 0, count); 
       } 
       output.flush(); 
       output.close(); 
       input.close(); 
      } 
      catch (Exception e) 
      { 
       e.printStackTrace(); 
      } 
      return 2.5f; 
      } 
    } 

while循環通過代碼設置爲0

+0

這兩個系統是否安裝了相同的JRE?什麼是Thread.sleep? – Adrian 2013-03-01 07:35:29

回答

0

e它進入無限:什麼是「input.available()」當你初始化你的緩衝區?如果在那一刻恰好是「0」,你會很難過。

一個解決方案是使用固定長度的緩衝區(例如8192)。

+0

嗨,非常感謝它,它完美的作品! – 2013-03-07 06:29:04

相關問題