网络通信中关于请求数据、断点续传和写入本地文件-创新互联
- (void)viewDidLoad {
创新互联公司-专业网站定制、快速模板网站建设、高性价比水城网站开发、企业建站全套包干低至880元,成熟完善的模板库,直接使用。一站式水城网站制作公司更省心,省钱,快速模板网站建设找我们,业务覆盖水城地区。费用合理售后完善,十载实体公司更值得信赖。[super viewDidLoad];
NSLog(@"%@",NSHomeDirectory());
//取得已下载数据大小
NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
receiveTotal = [[userDefaults objectForKey:@"ReceiveTotal"] doubleValue];
total = [[userDefaults objectForKey:@"Total"] doubleValue];
//显示上一次下载的进度
if (total > 0) {
CGFloat progress = receiveTotal/total;
self.progressView.progress = progress;
self.progressLabel.text = [NSString stringWithFormat:@"%.2f%%", progress * 100];
}
}
- (IBAction)downLoadClick:(UIButton *)sender {
if (!isDownLoad) {
//1.构建URL
NSURL *url = [NSURL URLWithString:@"http://s.qdcdn.com/lovebizhi/LoveWallpaper4Mac.dmg"];
//2.构建Request
NSMutableURLRequest *mRequest = [NSMutableURLRequest requestWithURL:url];
//断点续传
if (receiveTotal > 0) {
//设置续传位置
NSString *position = [NSString stringWithFormat:@"bytes=%d-",(int)receiveTotal];
[mRequest setValue:position forHTTPHeaderField:@"Range"];
}
//3.发送异步请求
connection = [NSURLConnection connectionWithRequest:mRequest delegate:self];
isDownLoad = YES;
//获取字符串
NSString *urlStr = url.absoluteString;
//获取urlStr的最后一部分
NSString *fileName = [urlStr lastPathComponent];
//写入文件
filePath = [NSHomeDirectory() stringByAppendingFormat:@"/Documents/%@",fileName];
//创建文件
[[NSFileManager defaultManager] createFileAtPath:filePath contents:nil attributes:nil];
}
}
//暂停下载
- (IBAction)pauseClick:(UIButton *)sender {
//取消下载连接
[connection cancel];
connection = nil;
//将缓冲数据写入文件
[self appendFileData:self.receiveData];
//在本地保存已下载文件的大小和文件总大小
NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
[userDefaults setObject:@(receiveTotal) forKey:@"ReceiveTotal"];
[userDefaults setObject:@(total) forKey:@"Total"];
//将数据同步写入文件
[userDefaults synchronize];
isDownLoad = NO;
}
#pragma mark-NSURLConnectionDataDelegate
//服务器响应
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSHTTPURLResponse *)response{
self.receiveData = [[NSMutableData alloc]init];
//判断总大小是否为0,如果为0,说明从起始位置开始下,获取文件总大小
if (total == 0) {
//获取所有的响应头
NSDictionary *fields = response.allHeaderFields;
NSLog(@"fields is %@",fields);
//获取数据的总大小
NSNumber *length = [fields objectForKey:@"Content-Length"];
total = [length doubleValue];
NSLog(@"total is %.2f",total);
}
}
//接收数据
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data{
[self.receiveData appendData:data];
receiveTotal += data.length;
//计算进度
double progress = receiveTotal / total;
//刷新UI
self.progressView.progress = progress;
self.progressLabel.text = [NSString stringWithFormat:@"%.2f%%",progress * 100];
//判断缓冲的数据是否大于500KB
if (self.receiveData.length >= 500 * 1024) {
//写入数据
[self appendFileData:self.receiveData];
//清空
self.receiveData.data = nil;
}
}
//数据传输完成
- (void)connectionDidFinishLoading:(NSURLConnection *)connection{
//将最后一个缓冲文件写入
if (self.receiveData.length < 500 * 1024) {
//写入数据
[self appendFileData:self.receiveData];
//清空
self.receiveData.data = nil;
}
_progressLabel.text = @"下载完成";
self.progressView.progress = 0;
isDownLoad = NO;
}
- (void)appendFileData:(NSData *)data{
if (data.length == 0) {
return;
}
//使用NSFileHandle写文件,此文件必须已经创建,NSFileHandle不会创建文件
NSFileHandle *fileHandle = [NSFileHandle fileHandleForWritingAtPath:filePath];
//将数据插入到写入点
[fileHandle seekToEndOfFile];
[fileHandle writeData:data];
//关闭文件,确保写入完成
[fileHandle closeFile];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
另外有需要云服务器可以了解下创新互联scvps.cn,海内外云服务器15元起步,三天无理由+7*72小时售后在线,公司持有idc许可证,提供“云服务器、裸金属服务器、高防服务器、香港服务器、美国服务器、虚拟主机、免备案服务器”等云主机租用服务以及企业上云的综合解决方案,具有“安全稳定、简单易用、服务可用性高、性价比高”等特点与优势,专为企业上云打造定制,能够满足用户丰富、多元化的应用场景需求。
分享标题:网络通信中关于请求数据、断点续传和写入本地文件-创新互联
网站URL:http://cdiso.cn/article/cocjih.html