2011-01-04 18 views
9

Questo codice per riprendere il download non funziona correttamente in Android, sebbene funzioni correttamente in un'applicazione Java. Qui sto cercando di scaricare un file zip, e riprenderà il download, ma il risultato netto è un file zip non valido.Riprendi Download non funzionante in 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(); 
      } 

     } 

Grazie.

+0

Si prega di aggiungere alcuni dettagli nella tua domanda: qual è esattamente il problema che stai affrontando? C'è qualche errore? In tal caso, puoi pubblicare thetacktrace? Abbiamo bisogno di maggiori dettagli se vogliamo rispondervi. –

+0

Attualmente sto cercando di scaricare un file zip ... e riprenderà anche il download..ma il risultato netto è un file zip non valido .. –

risposta

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

Vorrei anche aggiungere un 'connection.setReadTimeout()' e un 'connection.setConnectionTimeout() ' –

+0

Ciao Lijo, 'ISSUE_DOWNLOAD_STATUS.intValue()' non capisco questo valore per favore dammi il riferimento per questo. – DynamicMind

+2

File file = nuovo file (DESTINATION_PATH); if (file.exists()) { scaricato = (int) file.length(); connection.setRequestProperty ("Range", "byte =" + (file.length()) + "-"); } \t \t \t} else { connection.setRequestProperty ("Range", "byte =" + scaricato + "-"); \t \t \t} –

3

Il file zip è danneggiato perché si pensa che lo stream riprenda dal byte dell'intervallo specificato. In realtà scorre di nuovo dall'inizio, e quindi hai un file più grande dell'originale. Per farla breve, il tuo server non supporta la proprietà range.

Problemi correlati