2012-12-10 97 views
4

我有一个应用程序从下拉框(这是公共共享路径)下载zip文件内容。我使用HttpURLConnection编写了下载代码,但是它没有按预期工作,而是下载一小部分(下载zip文件显示31 kb,但其原始大小为3mb)。我正在密码我的代码。请帮我解决这个问题。在android中的zip文件下载

  URL url = new URL("drop box public share url"); 
     //create the new connection 
     HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection(); 
     urlConnection.setAllowUserInteraction(false); 
     urlConnection.setInstanceFollowRedirects(true); 
     urlConnection.setConnectTimeout(5 * 1000); 
     urlConnection.setRequestMethod("GET"); 
     urlConnection.setDoOutput(true); 
     urlConnection.setDoInput(true); 

     urlConnection.connect(); 
     File SDCardRoot = Environment.getExternalStorageDirectory(); 
     File file = new File(SDCardRoot,"/download/sample.zip"); 
     FileOutputStream fileOutput = new FileOutputStream(file); 
     InputStream inputStream = urlConnection.getInputStream(); 
     int totalSize = urlConnection.getContentLength(); 
     int downloadedSize = 0; 
     //create a buffer... 
     byte[] buffer = new byte[1024]; 
     int bufferLength = 0; 
     while ((bufferLength = inputStream.read(buffer)) > 0) { 
       fileOutput.write(buffer, 0, bufferLength); 
            downloadedSize += bufferLength; 
       onProgressUpdate(downloadedSize, totalSize); 

     } 
     //close the output stream when done 
     fileOutput.close(); 
     inputStream.close(); 
+0

您可能希望检索“Content-length”响应标头的值并读取,直到获得多个字节。 –

+0

如果etienne建议不起作用,您必须查看'if-range'是否有效检索数据是多个部分。 – njzk2

回答

0

尝试使用普通的URLConnection而不是HttpURLConnection。并看到输出。