断点续传
确认思路
- 检查服务器文件信息
- 检查本地文件
- 如果比服务器文件小,续传
- 如果比服务器文件大,重新下载
- 如果和服务器文件一样,下载完成
- 断点续传
代码实现
检查服务器文件信息
- (void)remoteInfoWithURL:(NSURL *)url {
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
request.HTTPMethod = @"HEAD";
NSURLResponse *response = nil;
[NSURLConnection sendSynchronousRequest:request returningResponse:&response error:NULL];
self.expectedContentLength = response.expectedContentLength;
self.targetPath = [NSTemporaryDirectory() stringByAppendingPathComponent:response.suggestedFilename];
}
检查本地文件
- (long long)localFileSize {
NSFileManager *manager = [NSFileManager defaultManager];
long long fileSize = 0;
if ([manager fileExistsAtPath:self.targetPath]) {
fileSize = [[manager attributesOfItemAtPath:self.targetPath error:NULL] fileSize];
}
if (fileSize > self.expectedContentLength) {
[manager removeItemAtPath:self.targetPath error:NULL];
fileSize = 0;
}
return fileSize;
}
断点续传
- (void)downloadWithURL:(NSURL *)url offset:(long long)offset {
self.fileSize = offset;
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url cachePolicy:1 timeoutInterval:kTimeout];
NSString *rangeStr = [NSString stringWithFormat:@"bytes=%lld-", offset];
[request setValue:rangeStr forHTTPHeaderField:@"Range"];
NSURLConnection *conn = [NSURLConnection connectionWithRequest:request delegate:self];
[conn start];
}
修改代理方法
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response {
self.fileStream = [[NSOutputStream alloc] initToFileAtPath:self.targetPath append:YES];
[self.fileStream open];
}
下载主方法
- (void)downloadWithURL:(NSURL *)url {
[self remoteInfoWithURL:url];
long long fileSize = [self localFileSize];
if (fileSize == self.expectedContentLength) {
NSLog(@"下载完成");
return;
}
[self downloadWithURL:url offset:fileSize];
}