zoukankan      html  css  js  c++  java
  • 【转】断点继传

    断点续传的原理

    其实断点续传的原理很简单,就是在 Http 的请求上和一般的下载有所不同而已。 
    打个比方,浏览器请求服务器上的一个文时,所发出的请求如下: 
    假设服务器域名为 wwww.sjtu.edu.cn,文件名为 down.zip。

    1. GET /down.zip HTTP/1.1  
    2. Accept: image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, application/vnd.ms-   
    3. excel, application/msword, application/vnd.ms-powerpoint, */*   
    4. Accept-Language: zh-cn   
    5. Accept-Encoding: gzip, deflate   
    6. User-Agent: Mozilla/4.0 (compatible; MSIE 5.01; Windows NT 5.0)   
    7. Connection: Keep-Alive   

     服务器收到请求后,按要求寻找请求的文件,提取文件的信息,然后返回给浏览器,返回信息如下:

    1. 200  
    2. Content-Length=106786028  
    3. Accept-Ranges=bytes   
    4. Date=Mon, 30 Apr 2001 12:56:11 GMT   
    5. ETag=W/"02ca57e173c11:95b"  
    6. Content-Type=application/octet-stream   
    7. Server=Microsoft-IIS/5.0  
    8. Last-Modified=Mon, 30 Apr 2001 12:56:11 GMT   

     所谓断点续传,也就是要从文件已经下载的地方开始继续下载。所以在客户端浏览器传给 Web 服务器的时候要多加一条信息 -- 从哪里开始。 
    下面是用自己编的一个"浏览器"来传递请求信息给 Web 服务器,要求从 2000070 字节开始。

    1. GET /down.zip HTTP/1.0  
    2. User-Agent: NetFox   
    3. RANGE: bytes=2000070-   
    4. Accept: text/html, image/gif, image/jpeg, *; q=.2, */*; q=.2   

     仔细看一下就会发现多了一行 RANGE: bytes=2000070- 
    这一行的意思就是告诉服务器 down.zip 这个文件从 2000070 字节开始传,前面的字节不用传了。 
    服务器收到这个请求以后,返回的信息如下:

    1. 206  
    2. Content-Length=106786028  
    3. Content-Range=bytes 2000070-106786027/106786028  
    4. Date=Mon, 30 Apr 2001 12:55:20 GMT   
    5. ETag=W/"02ca57e173c11:95b"  
    6. Content-Type=application/octet-stream   
    7. Server=Microsoft-IIS/5.0  
    8. Last-Modified=Mon, 30 Apr 2001 12:55:20 GMT   

     和前面服务器返回的信息比较一下,就会发现增加了一行:

    1. Content-Range=bytes 2000070-106786027/106786028   

     返回的代码也改为 206 了,而不再是 200 了。

    知道了以上原理,就可以进行断点续传的编程了。

      1. NSURL *url1=[NSURL URLWithString:@"下载地址";  
      2. NSMutableURLRequest* request1=[NSMutableURLRequest requestWithURL:url1];  
      3. [request1 setValue:@"bytes=20000-" forHTTPHeaderField:@"Range"];   
      4. [request1 setCachePolicy:NSURLRequestReloadIgnoringLocalCacheData];  
      5. NSData *returnData1 = [NSURLConnection sendSynchronousRequest:request1 returningResponse:nil error:nil];   
      6. [self writeToFile:returnData1 fileName:@"SOMEPATH"];  
      7.   
      8.   
      9.   
      10.   
      11. -(void)writeToFile:(NSData *)data fileName:(NSString *) fileName  
      12. {  
      13.     NSString *filePath=[NSString stringWithFormat:@"%@",fileName];  
      14.     if([[NSFileManager defaultManager] fileExistsAtPath:filePath] == NO){  
      15.         NSLog(@"file not exist,create it...");  
      16.         [[NSFileManager defaultManager] createFileAtPath:filePath contents:nil attributes:nil];  
      17.     }else {  
      18.     NSLog(@"file exist!!!");  
      19.     }  
      20.   
      21.     FILE *file = fopen([fileName UTF8String], [@"ab+" UTF8String]);  
      22.   
      23.     if(file != NULL){  
      24.         fseek(file, 0, SEEK_END);  
      25.     }  
      26.     int readSize = [data length];  
      27.     fwrite((const void *)[data bytes], readSize, 1, file);  
      28.     fclose(file);  
        1. from: http://www.cnblogs.com/liyufeng2013/p/3826323.html
  • 相关阅读:
    聊聊 API Gateway 和 Netflix Zuul
    现行统编中学数学教科书有多烂
    线程池的成长之路
    Quick Guide to Microservices with Spring Boot 2.0, Eureka and Spring Cloud
    以太坊、Hyperledger Fabric和Corda,哪个更好?
    【SFA官方翻译】Spring WebFlux和Spring Cloud进行响应式微服务开发
    goroutine背后的系统知识
    goroutine与调度器
    MySQL命令,一篇文章替你全部搞定
    微服务架构技术栈选型手册(万字长文)
  • 原文地址:https://www.cnblogs.com/xuan52rock/p/5135722.html
Copyright © 2011-2022 走看看