2014-02-21 56 views
3

如何在服务器端和客户端使用块从URL下载视频。如何在服务器端和客户端使用块从URL下载视频

HttpURLConnection connection = 
       (HttpURLConnection) url.openConnection(); 

     // Specify what portion of file to download. 
     connection.setRequestProperty("Range", 
       "bytes=" + downloaded + "-"); 

     // Connect to server. 
     connection.connect(); 

     // Make sure response code is in the 200 range. 
     if (connection.getResponseCode()/100 != 2) { 
      error(); 
     } 

     // Check for valid content length. 
     int contentLength = connection.getContentLength(); 
     if (contentLength < 1) { 
      error(); 
     } 

/* Set the size for this download if it 
    hasn't been already set. */ 
     if (size == -1) { 
      size = contentLength; 
      stateChanged(); 
     } 

     // Open file and seek to the end of it. 

    File f = new File("/sdcard/Sample"); 
     if(f.exists()) 
     { 

     }else 
     { 
      f.mkdir(); 
     } 

     file = new RandomAccessFile(f.getAbsolutePath()+"/"+getFileName(url), "rw"); 
     file.seek(downloaded); 

     stream = connection.getInputStream(); 

在这里,我想下载像块文件......如果从服务器传来的数据块和我的客户端我需要下载和合并是完整的视频文件

回答

0

假设你正在使用HTTP为了下载,你需要使用HEAD http verb和RANGE http header。

HEAD将为您提供文件大小(如果可用),然后RANGE允许您下载字节范围。

一旦你有文件大小,将它分成几乎相同大小的块,并为每个块产生下载线程。一旦完成,请按正确的顺序写入文件块。

有关示例的详细信息阅读此SO回答 https://stackoverflow.com/a/16788587/5231773