Connection 斷點(diǎn)下載

{

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) {

//實(shí)現(xiàn)下載

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

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

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

//獲得已經(jīng)下載的文件大小

_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表示返回?cái)?shù)據(jù)的范圍

//value:bytes=0-1000表示從0開始返回1000個(gè)字節(jié)

//value:bytes=1000-表示返回第1000個(gè)字節(jié)之后的所有數(shù)據(jù)

[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:@"繼續(xù)下載" 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"];

//獲取最終路徑目標(biāo)路徑

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(@"下載失敗");

}

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容