NSURLConnction實現(xiàn)大文件的斷點續(xù)傳(離線下載)

大文件下載分析

在大文件的下載時,文件的下載傳輸時間較長,需要使用到暫停或者下載中意外退出時候仍能繼續(xù)上次的下載進度.

使用的類

  • NSURLConnction的代理方法寫入沙盒中
  • 使用文件句柄(指針)NSFileHandle在接受數(shù)據(jù)每次將句柄移動到數(shù)據(jù)末尾seekToEndOfFile使用句柄寫數(shù)據(jù);
  • NSMutableURLRequest請求頭中設(shè)定Range,實現(xiàn)斷點下載;
  • 使用NSFileManager來獲取文件的大小;

實現(xiàn)代碼

  1. 設(shè)置相關(guān)的屬性
@property (weak, nonatomic) IBOutlet UIProgressView *progressView;  
/**目標文件小大*/
@property (nonatomic, assign) NSInteger totalSize;
/**當前已下載的文件小大*/
@property (nonatomic, assign) NSInteger currentSize;
/** 文件句柄*/
@property (nonatomic, strong)NSFileHandle *handle;
/** 沙盒路徑 */
@property (nonatomic, strong) NSString *fullPath;
/** 連接對象 */
@property (nonatomic, strong) NSURLConnection *connect;
  1. 建立請求對象
    在請求頭里面設(shè)置要請求的文件起始位置,實現(xiàn)斷點下載,避免重復重復請求資源
NSURL *url = [NSURL URLWithString:@"下載資源的地址"];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
    //設(shè)置請求頭信息,告訴服務器值請求一部分數(shù)據(jù)range
    /*
     bytes=0-100 
     bytes=-100
     bytes=0- 請求100之后的所有數(shù)據(jù)
     */
 NSString *range = [NSString stringWithFormat:@"bytes=%zd-",self.currentSize];
    [request setValue:range forHTTPHeaderField:@"Range"];   
    //發(fā)送請求
    self.connect = [[NSURLConnection alloc]initWithRequest:request delegate:self];
  1. 實現(xiàn)代理方法
-(void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
    //1.得到文件的總大小(本次請求的文件數(shù)據(jù)的總大小 != 文件的總大小)
    // self.totalSize = response.expectedContentLength + self.currentSize;
    if (self.currentSize >0) {
        return;
    }
    self.totalSize = response.expectedContentLength;
    //2.寫數(shù)據(jù)到沙盒中
    self.fullPath = [[NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) lastObject]stringByAppendingPathComponent:@"文件路徑"];
    
    NSLog(@"%@",self.fullPath);
    
    //3.創(chuàng)建一個空的文件
    [[NSFileManager defaultManager] createFileAtPath:self.fullPath contents:nil attributes:nil];
    
    //NSDictionary *dict = [[NSFileManager defaultManager]attributesOfItemAtPath:self.fullPath error:nil];
    
    //4.創(chuàng)建文件句柄(指針)
    self.handle = [NSFileHandle fileHandleForWritingAtPath:self.fullPath];
}
-(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
    //1.移動文件句柄到數(shù)據(jù)的末尾
    [self.handle seekToEndOfFile];
    
    //2.寫數(shù)據(jù)
    [self.handle writeData:data];
    
    //3.獲得進度
    self.currentSize += data.length;
    //進度=已經(jīng)下載/文件的總大小
    NSLog(@"%f",1.0 *  self.currentSize/self.totalSize);
    self.progressView.progress = 1.0 *  self.currentSize/self.totalSize;
}
-(void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
{
}
-(void)connectionDidFinishLoading:(NSURLConnection *)connection
{
    //此處注意要關(guān)閉文件句柄
    [self.handle closeFile];
    self.handle = nil;
}

取消或繼續(xù)下載

/**取消任務還不可逆的*/
 [self.connect cancel];
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內(nèi)容