大文件下载(一)

#import "ViewController.h"

@interface ViewController ()
//写数据的文件句柄(文件尾)
@property (nonatomic, strong) NSFileHandle *writeHandle;

//当前数据长度
@property (nonatomic, assign) long long nowlength;

//数据总长度
@property (nonatomic, assign) long long length;

//连接对象
@property (nonatomic, strong) NSURLConnection *conn;

//是否在下载
@property (nonatomic, assign,getter=isloading) BOOL loading;

@property (weak, nonatomic) IBOutlet UILabel *lengthtext;
@property (weak, nonatomic) IBOutlet UILabel *nowlengthtext;
@property (weak, nonatomic) IBOutlet UIProgressView *pro;
@property (weak, nonatomic) IBOutlet UIButton *btn;
- (IBAction)start:(UIButton*)sender;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
}


- (IBAction)start:(UIButton*)sender {
    if (self.isloading) {
        [sender setTitle:@"开始下载" forState:UIControlStateNormal];
         self.loading=NO;
        
        //取消当前请求
        [self.conn cancel];
        self.conn=nil;

    }else {
        [sender setTitle:@"暂停下载" forState:UIControlStateNormal];
        self.loading=YES;
        
        //下载地址
        NSString *urlstr=@"http://dldir1.qq.com/qqfile/QQforMac/QQ_V4.0.3.dmg";
        
        //地址转码
        urlstr=[urlstr stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
        NSURL *url=[NSURL URLWithString:urlstr];
        NSMutableURLRequest *request=[NSMutableURLRequest requestWithURL:url];
        
        //设置请求头信息
        NSString *value=[NSString stringWithFormat:@"bytes=%lld-",self.nowlength];
        [request setValue:value forHTTPHeaderField:@"Range"];
        self.conn=[NSURLConnection connectionWithRequest:request  delegate:self];

    }
    
    }
//连通服务器失败响应
- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error{
    NSLog(@"------连接服务器失败-------");
}

//连通服务器就会响应
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response{
    NSLog(@"------连接服务器成功-------");
    //获得服务器的响应头(可以得到数据信息)
//    NSHTTPURLResponse *http=(NSHTTPURLResponse *)response;
//    NSLog(@"%@",http);
   if (self.length)return;
    
    // 0.文件的存储路径
    NSString *caches=[NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES)lastObject];
    NSString *filepath=[caches stringByAppendingPathComponent:@"QQ_V4.0.3.dmg"];
    
    // 1.创建一个空的文件到沙盒中
    NSFileManager *mgr=[NSFileManager defaultManager];
    [mgr createFileAtPath:filepath contents:nil attributes:nil];
    
    // 2.创建写数据的文件句柄
    self.writeHandle=[NSFileHandle fileHandleForWritingAtPath:filepath];
    self.length=response.expectedContentLength;
    
    // 3.获得完整文件的长度
     self.lengthtext.text=[NSString stringWithFormat:@"%lld",self.length];
}

//接收到服务器数据就会响应(可能会被调用多次, 每次调用只会传递部分数据)
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data{
     NSLog(@"------接收服务器数据-------");
    NSLog(@"数据长度-----%ld",data.length);
    
    // 累加长度
    self.nowlength +=data.length;
    self.nowlengthtext.text=[NSString stringWithFormat:@"%lld",self.nowlength];
    //显示进度
    double progress=(double)self.nowlength/self.length;
    self.pro.progress=progress;
    
    //移动到文件尾部
    [self.writeHandle seekToEndOfFile];
    // 从当前移动的位置(文件尾部)开始写入数据
    [self.writeHandle writeData:data];
}

//接受完毕调用
- (void)connectionDidFinishLoading:(NSURLConnection *)connection{
   NSLog(@"------接受服务器数据完成-------");
    // 清空属性值
    self.length=0;
    self.nowlength=0;
    
     // 关闭连接(不再输入数据到文件中)
    [self.writeHandle closeFile];
    self.writeHandle=nil;
}

@end

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值