本文介绍: 一 概述HTTP Range概念断点续传逻辑断点续传示例二 HTTP Range概念通过设置请求头Range可以指定每次从网络下载数据包大小2.1 Range示例示例说明bytes=0-499从0到499的头500个字节bytes=500-999从500到999的第二个500字节bytes=500-从500字节以后的所有字节bytes=-500最后500个字节bytes=500-599,800-899同时指定几个范围2.2 R

概述

二 HTTP Range概念

通过设置请求头Range可以指定每次从网络下载数据包的大小

2.1 Range示例

示例 说明
bytes=0-499 从0到499的头500个字节
bytes=500-999 从500到999的第二个500字节
bytes=500- 从500字节以后的所有字节
bytes=-500 最后500个字节
bytes=500-599,800-899 同时指定几个范围

2.2 Range小结

断点续传逻辑

断点续传示例

4.1 代码

#import "ViewController.h"
#import  "DACircularProgressView.h"

@interface ViewController ()<NSURLConnectionDataDelegate>
@property(nonatomic,strong) NSFileHandle *writeHandle;
@property(nonatomic,assign) long long totalLength;
@property(nonatomic,assign) long long currentLength;

@property(nonatomic,weak) DACircularProgressView *circleView;
@property(nonatomic,strong) NSURLConnection *conn;
@property (weak, nonatomic) IBOutlet UIButton *btn;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    DACircularProgressView *circleView=[[DACircularProgressView alloc]init];
    circleView.frame=CGRectMake(120, 100, 100, 100);
    circleView.progressTintColor=[UIColor redColor];
    circleView.trackTintColor=[UIColor orangeColor];
    circleView.progress=0.01;
    [self.view addSubview:circleView];
    self.circleView=circleView;
    
}
- (IBAction)downloadAndPause:(UIButton *)sender
{
    //状态取反
    sender.selected=!sender.isSelected;
    if (sender.selected) { //继续
        NSLog(@"继续");
        //1-URL
        NSURL *url=[NSURL URLWithString:@"http://localhost:8080//MJServer/resources/videos.zip"];
        //2-请求
        NSMutableURLRequest *request=[NSMutableURLRequest requestWithURL:url];
        //设置请求头
        NSString *range=[NSString stringWithFormat:@"bytes=%lld-",self.currentLength];
        [request setValue:range forHTTPHeaderField:@"Range"];
        
        //3-下载
       self.conn= [NSURLConnection connectionWithRequest:request delegate:self];
    }else{ //暂停
        NSLog(@"暂停");
        [self.conn cancel];
        self.conn=nil;
    }
}

//-请求失败调用(请求超时网络异常)
-(void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
{
    NSLog(@"didFailWithError--");
}
//-接收服务器响应聚会调用
-(void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
    NSLog(@"didReceiveResponse--");
    if (self.currentLength)return;
    //文件路径
    NSString *caches=[NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES)lastObject];
    NSString *filePath=[caches stringByAppendingPathComponent:@"videos.zip"];
    
    //创建一个空的文件沙盒中
    NSFileManager *mgr=[NSFileManager defaultManager];
    [mgr createFileAtPath:filePath contents:nil attributes:nil];
    //创建一个用来数据句柄
    self.writeHandle=[NSFileHandle fileHandleForWritingAtPath:filePath];
    
   //获得文件的总大小
    self.totalLength=response.expectedContentLength;

}
//当接收服务器返回实体数据时调用(具体内容这个内容可被调用多次)
-(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
  //移动文件最后面
    [self.writeHandle seekToEndOfFile];
    //写入数据到沙盒中
    [self.writeHandle writeData:data];
    self.currentLength+=data.length;
    NSLog(@"下载进度:%f",(double)self.currentLength/self.totalLength);
    self.circleView.progress=(double)self.currentLength/self.totalLength;
    
}
//加载完毕后调用(服务器的数据已经完全返回后)
-(void)connectionDidFinishLoading:(NSURLConnection *)connection
{
    NSLog(@"connectionDidFinishLoading--");
    self.currentLength=0;
    self.totalLength=0;
    [self.btn setHidden:TRUE];
    
    //关闭文件
    [self.writeHandle closeFile];
    self.writeHandle=nil;
}
@end

4.2 效果图

原文地址:https://blog.csdn.net/Calvin_zhou/article/details/123493280

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任

如若转载,请注明出处:http://www.7code.cn/show_32092.html

如若内容造成侵权/违法违规/事实不符,请联系代码007邮箱suwngjj01@126.com进行投诉反馈,一经查实,立即删除

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注