比如請求的數(shù)據(jù)大小是0~1000的話斷點續(xù)傳的原理酒如同下面的分段解析
分塊請求? ? Request? 請求頭? 通過包含參數(shù)
如果下載? 0~500? ? ? ? Range? ? ? 0-500
? ? ? ? ? ? 500~1000 ? ? ? ? ? 500-1000
? ? ? ? ? ?200~800 ? ? ? ? ? ? ? 200-800
? ? ? ? ? ? ?200~最后 ? ? ? ? ? ? 200-
例子:下載文件斷點續(xù)下自定義下載工具類.m中
?#import@interface downLoad : NSObject//傳下載進度(block傳值)? 0~1
@property (nonatomic,strong) void(^progressValue)(float);//設置一個url? 待下載的鏈接(url)? 下載的路徑(存儲下載文件)//初始化方法-(instancetype)initWithURL:(NSString *)url toFirlPath:(NSString *)path;//開始下載-(void)start;//停止下載-(void)stop;@end定義下載工具類.h中#import "downLoad.h"#import@implementation downLoad
{
//? ? 定義一個全局的發(fā)起下載的Connection對象
NSURLConnection *_connection;
//? 計算當前下載的進度
unsigned long long _currentLength;
//? 下載文件的總進度
long long _totalLength;
//? ? 定義一個文件讀寫的句柄
NSFileHandle *_fileHandle;
NSString *_url;
NSString *_path;
}
//設置一個url? 待下載的鏈接(url)? 下載的路徑(存儲下載文件)
-(instancetype)initWithURL:(NSString *)url toFirlPath:(NSString *)path
{
if (self=[super init]) {
_url=url;
_path=path;
[self creatConnection:url file:path];
}
return self;
}
//封裝之前的代碼和方法
-(void)creatConnection:(NSString *)url file:(NSString *)path
{
NSURL *myURL=[NSURL URLWithString:url];
NSMutableURLRequest *request=[[NSMutableURLRequest alloc]initWithURL:myURL];
NSFileManager *manager=[NSFileManager defaultManager];
_fileHandle=[NSFileHandle fileHandleForUpdatingAtPath:path];
//? ? ? ? 判斷存不存在已下載數(shù)據(jù)的文件
if ([manager fileExistsAtPath:path]) {
//? ? ? ? ? ? 如果存在計算出已經(jīng)下載文件的大小
_currentLength =[_fileHandle seekToEndOfFile];
//? ? ? ? ? ? 設置請求數(shù)據(jù)的范圍? key? : Range? ? ? value? @"bytes_currentLength-"
[request setValue:@"Range" forHTTPHeaderField:[NSString stringWithFormat:@"bytes=%llu-",_currentLength]];
}else{
//? ? ? ? 不存在? 創(chuàng)建文件
[manager createFileAtPath:path contents:nil attributes:nil];
//? ? ? ? ? ? 設置文件的長度為0
_currentLength=0;
}
_connection=[[NSURLConnection alloc]initWithRequest:request delegate:self startImmediately:NO];
}
//開始下載
-(void)start
{
if (!_connection) {
//? ? ? ? 數(shù)據(jù)為空得時候創(chuàng)建
[self creatConnection:_url file:_path];
}
[_connection start];
}
//停止下載
-(void)stop
{
[_connection cancel];
_connection=nil;
}
//實現(xiàn)下載的協(xié)議方法? NSURLConnectionDataDelegate
-(void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
//請求數(shù)據(jù)成功后 服務器返回一個數(shù)據(jù)信息(reponse)包含所下載數(shù)據(jù)的總長度
//? ? 數(shù)據(jù)總長度
_totalLength=response.expectedContentLength;
}
-(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
//一點點的返回數(shù)據(jù)? 同時實現(xiàn)一點點的寫入文件
[_fileHandle writeData:data];
//? ? 同步增加已下載數(shù)據(jù)的字節(jié)數(shù)_
_currentLength +=[data length];
//? ? 調(diào)用之前定義的block來時刻改變prograssView的值(調(diào)用block把當前的下載進度傳過去)
self.progressValue((float)_currentLength/_totalLength);
}
-(void)connectionDidFinishLoading:(NSURLConnection *)connection
{
//下載完成了給個UI提示
UIAlertView *alertView=[[UIAlertView alloc]initWithTitle:nil message:@"下載完成" delegate:nil cancelButtonTitle:@"確定" otherButtonTitles:nil, nil];
[alertView show];
}
@end
系統(tǒng)vc類的.h中
#import "ViewController.h"
#import "downLoad.h"
@interface ViewController ()
@end
@implementation ViewController
{
downLoad *_download;
}
- (void)viewDidLoad {
[super viewDidLoad];
//? ? 獲取沙盒路徑? 拼接后 存入沙盒
NSString *path=[NSString stringWithFormat:@"%@/download.zip",NSHomeDirectory()];
//? ? 獲取url的路徑
NSString *url =@"Http://10.0.179.123/1531/Xcode6.4.zip";
//創(chuàng)建下載對象
_download =[[downLoad alloc]initWithURL:url toFirlPath:path];
//? ? 取消循環(huán)引用的條件
__weak ViewController *object=self;
//? ? block 賦值
_download.progressValue=^(float value){
object.progress.progress=value;
};
NSLog(@"%@",path);
}
- (IBAction)start:(id)sender {
[_download start];
}
- (IBAction)stop:(id)sender {
[_download stop];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
}
@end