您的位置:首页 > 其它

大文件下载(一)

2015-06-26 14:08 260 查看
#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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: