Connection 斷點下載

{

NSURLConnection *_connection;

//文件句柄,緩存文件,必須存在

NSFileHandle *_handle;

long long _receiveSize;

long long _totalSize;

}

- (IBAction)StartAndPauseAction:(id)sender {

NSString *destionPath = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents/Cache/destion/aa.mov"];

NSLog(@"destionPath ---> %@",destionPath);

//判斷文件是否存在

if(![[NSFileManager defaultManager] fileExistsAtPath:destionPath]) {

if(self.btn.tag == 1) {

//實現下載

NSString *tempPath = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents/Cache/temp/aa.mov"];

//獲取文件的大小.屬性等

NSDictionary *Dic = [[NSFileManager defaultManager] attributesOfItemAtPath:tempPath error:nil];

//獲得已經下載的文件大小

_receiveSize= [[Dic objectForKey:NSFileSize] longLongValue];

NSString *urlStr =@"http://localhost:8080/UpLoad/XiaTianXieZouQu.rmvb";

urlStr = [urlStr stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];

NSMutableURLRequest * request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:urlStr]];

//key:Range表示返回數據的范圍

//value:bytes=0-1000表示從0開始返回1000個字節

//value:bytes=1000-表示返回第1000個字節之后的所有數據

[request addValue:[NSString stringWithFormat:@"bytes=%lld-",_receiveSize] forHTTPHeaderField:@"Range"];

_connection = [NSURLConnection connectionWithRequest:request delegate:self];

self.btn.tag= 2;

[self.btn setTitle:@"正在下載" forState:UIControlStateNormal];

}else{

//暫停

[_connection cancel];

self.btn.tag=1;

[self.btn setTitle:@"繼續下載" forState:UIControlStateNormal];

}

}else{

NSLog(@"文件已存在");

}

}

#pragma mark -- NSURLConnectionDataDelegate,NSURLConnectionDelegate

- (void)connection:(NSURLConnection*)connection didReceiveResponse:(NSURLResponse*)response {

NSString *filePath =@"Cache/temp";

filePath = [filePath getExistsFilePathWithFileName:@"aa.mov"];

_handle = [NSFileHandle fileHandleForWritingAtPath:filePath];

NSHTTPURLResponse *HTTPResponse = (NSHTTPURLResponse*)response;

_totalSize = HTTPResponse.expectedContentLength + _receiveSize;

}

- (void)connection:(NSURLConnection*)connection didReceiveData:(NSData*)data {

_receiveSize =_receiveSize+ data.length;

self.ProgressView.progress= (float)_receiveSize/_totalSize;

NSLog(@"總的:%lld ?progress:%f",_totalSize,self.ProgressView.progress);

//從文件末尾寫入

[_handle seekToFileOffset:[_handle seekToEndOfFile]];

[_handle writeData:data];

}

- (void)connectionDidFinishLoading:(NSURLConnection*)connection {

[_handle closeFile];

//搬移文件

//獲取原路徑

NSString *tempPath =@"Cache/temp";

tempPath = [tempPath getExistsFilePathWithFileName:@"aa.mov"];

//獲取最終路徑目標路徑

NSString *destionPatn =@"Cache/destion";

destionPatn = [destionPatn getExistsFilePath];

destionPatn = [destionPatn stringByAppendingString:@"/aa.mov"];

NSLog(@"destionPatn ---> %@",destionPatn);

if([[NSFileManager defaultManager] moveItemAtPath:tempPath toPath:destionPatn error:nil]) {

NSLog(@"文件搬移成功");

}else{

NSLog(@"12");

}

}

- (void)connection:(NSURLConnection *)connection didFailWithError:(NSErro r*)error {

NSLog(@"下載失敗");

}

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容