2016-06-14 1 views
-1

HttpURLConnection을 사용하여 서버에서 600MB 비디오 파일을 다운로드하려고합니다. 그래서 "범위"헤더를 사용하여 3 부분으로 파일을 다운로드하고 모든 바이트를 다운로드 한 후 3 개의 별도 비디오 파일을 준비했습니다.안드로이드 요청 헤더 범위 : 바이트 = 207358401-414716804, 파일이 손상 되었습니까?

MyCode :

부 - 1 :

try{ 

    URL url = new URL(downloadUrl); 
    connection =(HttpURLConnection) url.openConnection(); 
    connection.setRequestMethod("POST"); 
    connection.setRequestProperty("Connection", "keep-alive"); 
    connection.setRequestProperty("Content-type", ""); 
    connection.setRequestProperty("User-Agent", "LibHttp/1.3.8"); 
    connection.setRequestProperty("Range", "bytes=414716804-622075205"); 
    connection.connect(); 

    input = connection.getInputStream(); 

    byte data[] = new byte[4096]; 

    int count =0; 

     while ((count = input.read(data)) != -1) { 

      String path = "/sdcard/test.flv"; 
      saveTofile(data,path,count); 

     } 

    } catch (Exception e) { 
    System.out.println(SampleThread.TAG+" Download Exception "+e.toString()); 
    } 


public void saveTofile(byte[] data,String path,int count){ 

    OutputStream fos = null; 

    try { 

     fos = new FileOutputStream(new File(path),true); 

     // Writes bytes from the specified byte array to this file output stream 

     fos.write(data,0,count); 
    } 
    catch (FileNotFoundException e) { 
     System.out.println("File not found" + e); 
    } 
    catch (IOException ioe) { 
     System.out.println("Exception while writing file " + ioe); 
    } 
    finally { 
     // close the streams using close method 
     try { 
      if (fos != null) { 
       fos.close(); 
      } 
     } 
     catch (IOException ioe) { 
      System.out.println("Error while closing stream: " + ioe); 
     } 
    } 

} 

그러나 재생하는 동안

범위 : 바이트 = 0-207358401 // 재생 잘 //

부 - 2 : 범위 : bytes = 207358401-414716804 // 파일이 손상되어 재생되지 않음 //

부분 -3 : 범위 = bytes = 414716804-622075205 // 파일이 손상되지 않았습니다. g //

왜 part-1 비디오 파일이 절대적으로 훌륭하게 재생되는지, 나머지 두 개는 전혀 재생되지 않는지, 이러한 파일이 비디오 파일이 아니라는 것을 발견 할 수 없습니다.

제 문제를 제안 해주세요.

+0

* 문제를 찾을 수 없습니다 * ... 논리를 사용하십시오 ... 대부분의 비디오 포맷은 파일의 시작 부분에 헤더가 있습니다 ... 파일의 첫 부분에 fx가 있습니다. 두 번째 ""바보 ""... 두 번째 부분 만 읽으면 어떻게 느껴 집니까? – Selvin

+0

답장을 보내 주셔서 감사합니다.하지만 귀하의 요지를 얻지 못했습니다. 개념을 자세히 설명해 주시겠습니까? – animation123

+0

당신은 1 비디오 파일에서 바이트를 나누어서 3 개의 비디오 파일을 얻지 못할 것입니다 ... 비디오 파일은 어떤 구조를 가지고 있습니다. – Selvin

답변

0

바이트 분노는 오프셋 길이가 아니라 포함됩니다. 범위 : 바이트 = 0-207358401

부 - 2 : 1 -

부 작동합니다 다음과 같은 범위를 사용하여 범위 : 바이트 = 207358402-414716804

부 - 3 : 범위 : 바이트 = 414716805-

+0

아니요 :(작동하지 않았습니다. – animation123

+0

그런 다음 문제가 있습니다. 다운로더와 함께. 두 파일을 16 진수 편집기로보고 차이점을 찾으십시오. – szatmary