2011-01-04 17 views
9

में डाउनलोड नहीं करना डाउनलोड एंड्रॉइड में डाउनलोड फिर से शुरू करने के लिए यह कोड ठीक से काम नहीं कर रहा है, हालांकि यह जावा एप्लिकेशन में ठीक काम करता है। यहां मैं एक ज़िप फ़ाइल डाउनलोड करने की कोशिश कर रहा हूं, और यह डाउनलोड फिर से शुरू हो जाएगा, लेकिन नेट परिणाम एक अवैध ज़िप फ़ाइल है।फिर से शुरू करें एंड्रॉइड

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

असल में यहां मैं एक ज़िप फ़ाइल डाउनलोड करने की कोशिश कर रहा हूं ... और यह भी डाउनलोड फिर से शुरू हो जाएगा .. लेकिन शुद्ध परिणाम अमान्य ज़िप फ़ाइल है .. –

उत्तर

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.setConnectionTimeout भी जोड़ूंगा() ' –

+0

हैलो लिजो, 'ISSUE_DOWNLOAD_STATUS.intValue()' मुझे इस मूल्य के बारे में समझ में नहीं आ रहा है, कृपया मुझे इसके लिए संदर्भ दें। – DynamicMind

+2

फ़ाइल फ़ाइल = नई फ़ाइल (DESTINATION_PATH); अगर (file.exists()) { डाउनलोड = (int) file.length(); connection.setRequestProperty ("रेंज", "बाइट्स =" + (फ़ाइल। लम्बाई()) + "-"); } \t \t \t} अन्य { connection.setRequestProperty ("रेंज", "बाइट्स =" + डाउनलोड + "-"); \t \t \t} –

3

आपकी ज़िप फ़ाइल दूषित हो गई है क्योंकि आपको लगता है कि स्ट्रीम आपके द्वारा निर्दिष्ट सीमा बाइट से फिर से शुरू होती है। यह वास्तव में शुरुआत से फिर से स्ट्रीम करता है, और इसलिए आपके पास मूल से बड़ी फ़ाइल है। लंबी कहानी छोटी, आपका सर्वर रेंज संपत्ति का समर्थन नहीं करता है।

संबंधित मुद्दे