2011-01-04 5 views
9

ダウンロードを再開するためのこのコードは、Javaアプリケーションで正常に動作しますが、Androidでは正常に動作していません。ここでは、zipファイルをダウンロードしようとしていますが、ダウンロードを再開しますが、結果は無効なzipファイルです。Androidでダウンロードが再開しない

BufferedInputStream in = null; 
     FileOutputStream fos = null; 
     BufferedOutputStream bout=null; 

     try { 
      downloaded=0; 
      HttpURLConnection connection = (HttpURLConnection) url.openConnection(); 
      if(ISSUE_DOWNLOAD_STATUS.intValue()==ECMConstant.ECM_DOWNLOADING){ 
       File file=new File(DESTINATION_PATH); 
       if(file.exists()){ 
        downloaded = (int) file.length(); 
       } 
      } 
      connection.setRequestProperty("Range", "bytes=" + downloaded + "-"); 
      connection.connect(); 
      size=connection.getContentLength(); 
      Dialog.setMax(size); 
      in = new BufferedInputStream(connection.getInputStream()); 
      fos=(downloaded==0)? new FileOutputStream(DESTINATION_PATH): new FileOutputStream(DESTINATION_PATH,true); 
      bout = new BufferedOutputStream(fos, 1024); 
      byte[] data = new byte[1024]; 
      int x = 0; 
      while ((x = in.read(data, 0, 1024)) >= 0) { 
       bout.write(data, 0, x); 
       downloaded += x; 
       System.out.println(downloaded); 
       onProgressUpdate((int)(downloaded*100/size)); 
      } 

      succes=true; 
     } catch (Exception e) { 
      e.printStackTrace(); 
     } finally { 
      try { 
       in.close(); 
       bout.close(); 
      } catch (IOException e) { 
       e.printStackTrace(); 
      } 

     } 

ありがとうございます。

+0

あなたの質問にいくつかの詳細を追加してください:あなたが直面している問題は何ですか?何か間違いはありますか?もしそうなら、あなたはタックストレースを投稿できますか?私たちがあなたに答えるためには、より詳細な情報が必要です。 –

+0

実際にはここで私はzipファイルをダウンロードしようとしています...そしてそれもダウンロードを再開します..しかし、純結果は無効なzipファイルです.. –

答えて

10
HttpURLConnection connection = (HttpURLConnection) url.openConnection(); 
int buf = 1024; 

if (ISSUE_DOWNLOAD_STATUS.intValue() == ECMConstant.ECM_DOWNLOADING) { 
    File file = new File(DESTINATION_PATH); 
    if (file.exists()) { 
     downloaded = (int) file.length(); 
     connection.setRequestProperty("Range", 
      "bytes=" + file.length() + "-"); 
    } 
} else { 
    connection.setRequestProperty("Range", "bytes=" + downloaded + "-"); 
} 

connection.setDoInput(true); 
connection.setDoOutput(true); 

progressBar.setMax(connection.getContentLength()); 
in = new BufferedInputStream(connection.getInputStream()); 
fos = new FileOutputStream(DESTINATION_PATH, downloaded == 0 ? false : true); 
bout = new BufferedOutputStream(fos, buf); 
byte[] data = new byte[buf]; 

while ((int x = in.read(data, 0, buf)) >= 0) { 
    bout.write(data, 0, x); 
    downloaded += x; 
    progressBar.setProgress(downloaded); 
} 
+1

私は 'connection.setReadTimeout()'と 'connection.setConnectionTimeout () ' –

+0

こんにちはLijo、 'ISSUE_DOWNLOAD_STATUS.intValue()'この値について私には分かりません。 – DynamicMind

+2

ファイルfile =新しいファイル(DESTINATION_PATH); if(file.exists()){ downloads =(int)file.length(); connection.setRequestProperty( "Range"、 "bytes =" +(file.length())+ " - "); } \t \t \t} else { connection.setRequestProperty( "Range"、 "bytes =" + downloaded + " - "); \t \t \t} –

3

ストリームが指定した範囲のバイトから再開したと思われるため、zipファイルが壊れています。実際には最初から再びストリームされるため、元のファイルよりも大きなファイルが作成されます。あなたのサーバーはrangeプロパティをサポートしていません。

関連する問題