网络篇----NSURLConnection断点下载
2015-08-13 17:03
609 查看
#import "HMViewController.h" #import "DACircularProgressView.h" @interface HMViewController () <NSURLConnectionDataDelegate> - (IBAction)download:(UIButton *)sender; /** * 用来写数据的文件句柄对象 */ @property (nonatomic, strong) NSFileHandle *writeHandle; /** * 文件的总大小 */ @property (nonatomic, assign) long long totalLength; /** * 当前已经写入的文件大小 */ @property (nonatomic, assign) long long currentLength; /** * 连接对象 */ @property (nonatomic, strong) NSURLConnection *conn; @property (nonatomic, weak) DACircularProgressView *circleView; @end @implementation HMViewController - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view, typically from a nib. DACircularProgressView *circleView = [[DACircularProgressView alloc] init]; circleView.frame = CGRectMake(100, 50, 100, 100); circleView.progressTintColor = [UIColor redColor]; circleView.trackTintColor = [UIColor blueColor]; circleView.progress = 0.0000001; [self.view addSubview:circleView]; self.circleView = circleView; } #pragma mark - NSURLConnectionDataDelegate代理方法 /** * 请求失败时调用(请求超时、网络异常) * * @param error 错误原因 */ - (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error { NSLog(@"didFailWithError"); } /** * 1.接收到服务器的响应就会调用 * * @param response 响应 */ - (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response { if (self.currentLength) return; // 文件路径 NSString *caches = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) lastObject]; NSString *filepath = [caches stringByAppendingPathComponent:@"videos.zip"]; // 创建一个空的文件 到 沙盒中 NSFileManager *mgr = [NSFileManager defaultManager]; [mgr createFileAtPath:filepath contents:nil attributes:nil]; // 创建一个用来写数据的文件句柄 self.writeHandle = [NSFileHandle fileHandleForWritingAtPath:filepath]; // 获得文件的总大小 self.totalLength = response.expectedContentLength; } /** * 2.当接收到服务器返回的实体数据时调用(具体内容,这个方法可能会被调用多次) * * @param data 这次返回的数据 */ - (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data { // 移动到文件的最后面 [self.writeHandle seekToEndOfFile]; // 将数据写入沙盒 [self.writeHandle writeData:data]; // 累计文件的长度 self.currentLength += data.length; //显示下载的百分比 self.circleView.progress = (double)self.currentLength/ self.totalLength; } /** * 3.加载完毕后调用(服务器的数据已经完全返回后) */ - (void)connectionDidFinishLoading:(NSURLConnection *)connection { self.currentLength = 0; self.totalLength = 0; // 关闭文件 [self.writeHandle closeFile]; self.writeHandle = nil; } - (IBAction)download:(UIButton *)sender { // 状态取反 sender.selected = !sender.isSelected; // 断点续传 // 断点下载 if (sender.selected) { // 继续(开始)下载 // 1.URL NSURL *url = [NSURL URLWithString:@"http://localhost:8080/MJServer/resources/videos.zip"]; // 2.请求 NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url]; // 设置请求头 NSString *range = [NSString stringWithFormat:@"bytes=%lld-", self.currentLength]; [request setValue:range forHTTPHeaderField:@"Range"]; // 3.下载(创建完conn对象后,会自动发起一个异步请求) self.conn = [NSURLConnection connectionWithRequest:request delegate:self]; } else { // 暂停 [self.conn cancel]; self.conn = nil; } } @end
相关文章推荐
- 使用HttpClientdopost编写 客户端、服务器与数据库Demo
- TCP与UDP区别
- 基于TCP/IP协议的网络编程
- 【Ex2013】【跨站点DAG】启用DAC模式,防止数据库级别上出现网络分区
- 第十八章、Linux网络配置
- tomcat下配置https环境
- IOS Q&A 我应该如何在网络传输中保持数据的安全性?
- 使用开源框架android-async-http-1.4.8.jar与服务器传输数据
- Nginx的tcp反向代理初探
- 网络篇----下载大文件
- 黑马程序员--java技术blog---第九篇:网络编程(2)
- java HttpServletRequest 获取和设置session
- Apache Http Server与Tomcat6 的负载均衡(二)
- xUtils框架介绍之HttpUtils的使用
- HTTP长连接与短链接
- Android_HTTP状态码介绍
- 深度学习概述:从感知机到深度网络
- 玩转git 网络资源汇总
- meta标签中的http-equiv属性使用介绍
- Android使用HttpURLConnection上传文件