大文件下载(一)
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
@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
相关文章推荐
- openssl之EVP系列之13---EVP_Open系列函数介绍
- vs2013之move源码解析
- 【HTTP】Fiddler(二) - 使用Fiddler做抓包分析
- android studio 开发 android studio 概览(三)
- Mybatis查询中文出错
- java中枚举使用及遍历(一)
- openssl之EVP系列之12---EVP_Seal系列函数介绍
- openssl之EVP系列之11---EVP_Verify系列函数介绍
- jsp和EL表达式
- oracle使用order by排序null值如何处理
- cocos2d-js hello world
- 解决convert: not found
- listview的多item中converview的缓存和复用
- openssl之EVP系列之10---EVP_Sign系列函数介绍
- Android快速使用SharedPreferences
- find
- 在cocos2d-JS中加载动画资源文件
- mac系统如何显示和隐藏文件
- java正则表达式去掉小数点后面多余的0
- 给一个数a,求一组数据中是否有两个数的和为a?