摆动下载不工作在Windows XP中

问题描述:

我有一个示例代码来从网络和它的一个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

e它进入无限:什么是“input.available()”当你初始化你的缓冲区?如果在那一刻恰好是“0”,你会很难过。

一个解决方案是使用固定长度的缓冲区(例如8192)。

+0

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