2011-01-04 1 views
9

자바 애플리케이션에서 정상적으로 작동하지만 다운로드를 다시 시작하기위한 코드가 Android에서 제대로 작동하지 않습니다. 여기에 zip 파일을 다운로드하려고하는데 다운로드가 다시 시작되지만 그물 결과는 잘못된 zip 파일입니다.이력서 다운로드가 안드로이드에서 작동하지 않습니다.

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 파일을 다운로드하려고 시도하고있다. 그리고 그것도 다운로드를 재개 할 것이다. 그러나 net 결과는 잘못된 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

Hello 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 속성을 지원하지 않습니다.