ios大文件下载

  1 #import "HMViewController.h"
  2 
  3 @interface HMViewController ()
  4 @property (weak, nonatomic) IBOutlet UIProgressView *progressView;
  5 /**
  6  *  写数据的文件句柄
  7  */
  8 @property (nonatomic, strong) NSFileHandle *writeHandle;
  9 /**
 10  *  当前已下载数据的长度
 11  */
 12 @property (nonatomic, assign) long long currentLength;
 13 /**
 14  *  完整文件的总长度
 15  */
 16 @property (nonatomic, assign) long long totalLength;
 17 
 18 - (IBAction)start;
 19 @end
 20 
 21 @implementation HMViewController
 22 
 23 - (void)viewDidLoad
 24 {
 25     [super viewDidLoad];
 26     
 27 }
 28 
 29 - (IBAction)start {
 30     // 检测一个文件\文件夹是否存在
 31 //    [[NSFileManager defaultManager] fileExistsAtPath:<#(NSString *)#>];
 32     
 33     // 获得一个文件\文件夹的属性(包含了文件大小\最后修改时间)
 34 //    [[NSFileManager defaultManager] attributesOfItemAtPath: error:<#(NSError *__autoreleasing *)#>];
 35     
 36     NSURL *url = [NSURL URLWithString:@"http://192.168.1.200:8080/MJServer/resources/video.zip"];
 37     // 默认就是GET请求
 38     NSURLRequest *request = [NSURLRequest requestWithURL:url];
 39     [NSURLConnection connectionWithRequest:request delegate:self];
 40 }
 41 
 42 #pragma mark - NSURLConnectionDataDelegate 代理方法
 43 /**
 44  *  1. 当接受到服务器的响应(连通了服务器)就会调用
 45  */
 46 - (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
 47 {
 48     // 获得服务器的响应头
 49 //    NSHTTPURLResponse *httpReponse = (NSHTTPURLResponse *)response;
 50 //    NSLog(@"%@", httpReponse.allHeaderFields);
 51     
 52     // 0.文件的存储路径
 53     NSString *caches = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) lastObject];
 54     NSString *filepath = [caches stringByAppendingPathComponent:@"video.zip"];
 55     
 56     // 1.创建一个空的文件到沙盒中
 57     NSFileManager *mgr = [NSFileManager defaultManager];
 58     // 刚创建完毕的大小是0字节
 59     [mgr createFileAtPath:filepath contents:nil attributes:nil];
 60     
 61     // 2.创建写数据的文件句柄
 62     self.writeHandle = [NSFileHandle fileHandleForWritingAtPath:filepath];
 63     
 64     // 3.获得完整文件的长度
 65     self.totalLength = response.expectedContentLength;
 66 }
 67 
 68 /**
 69  *  2. 当接受到服务器的数据就会调用(可能会被调用多次, 每次调用只会传递部分数据)
 70  */
 71 - (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
 72 {
 73     // 累加长度
 74     self.currentLength += data.length;
 75     
 76     // 显示进度
 77     double progress = (double)self.currentLength / self.totalLength;
 78     self.progressView.progress = progress;
 79     
 80     // 移动到文件的尾部
 81     [self.writeHandle seekToEndOfFile];
 82     // 从当前移动的位置(文件尾部)开始写入数据
 83     [self.writeHandle writeData:data];
 84 }
 85 
 86 /**
 87  *  3. 当服务器的数据接受完毕后就会调用
 88  */
 89 - (void)connectionDidFinishLoading:(NSURLConnection *)connection
 90 {
 91     NSLog(@"connectionDidFinishLoading----");
 92     
 93     // 清空属性值
 94     self.currentLength = 0;
 95     self.totalLength = 0;
 96     
 97     // 关闭连接(不再输入数据到文件中)
 98     [self.writeHandle closeFile];
 99     self.writeHandle = nil;
100 }
101 
102 /**
103  *  请求错误(失败)的时候调用(请求超时\断网\没有网, 一般指客户端错误)
104  */
105 - (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
106 {
107     
108 }
109 @end

 

posted @ 2016-01-25 18:44  xiaocaoera  阅读(390)  评论(0编辑  收藏  举报