NSURLConnection实现大文件断点下载:采用输出流来继续写文件
#import"ViewController.h"
@interfaceViewController()
@property(weak,nonatomic)IBOutletUIProgressView*progressView;
@property(nonatomic,assign)NSIntegertotalSize;
@property(nonatomic,assign)NSIntegercurrentSize;
/**沙盒路径*/
@property(nonatomic,strong)NSString*fullPath;
/**连接对象*/
@property(nonatomic,strong)NSURLConnection*connect;
/**输出流*/
@property(nonatomic,strong)NSOutputStream*stream;
@end
@implementationViewController
- (IBAction)startBtnClick:(id)sender {
[selfdownload];
}
- (IBAction)cancelBtnClick:(id)sender {
[self.connectcancel];
}
- (IBAction)goOnBtnClick:(id)sender {
[selfdownload];
}
//内存飙升
-(void)download
{
//1.url
// NSURL *url = [NSURL URLWithString:@"http://imgsrc.baidu.com/forum/w%3D580/sign=54a8cc6f728b4710ce2ffdc4f3cec3b2/d143ad4bd11373f06c0b5bd1a40f4bfbfbed0443.jpg"];
NSURL*url = [NSURLURLWithString:@"http://www.33lc.com/article/UploadPic/2012-10/2012102514201759594.jpg"];
//2.创建请求对象
NSMutableURLRequest*request = [NSMutableURLRequestrequestWithURL:url];
//设置请求头信息,告诉服务器值请求一部分数据range
/*
bytes=0-100
bytes=-100
bytes=0-请求100之后的所有数据
*/
NSString*range = [NSStringstringWithFormat:@"bytes=%zd-",self.currentSize];
[requestsetValue:rangeforHTTPHeaderField:@"Range"];
NSLog(@"+++++++%@",range);
//3.发送请求
NSURLConnection*connect = [[NSURLConnectionalloc]initWithRequest:requestdelegate:self];
self.connect= connect;
}
#pragma mark ----------------------
#pragma mark NSURLConnectionDataDelegate
-(void)connection:(NSURLConnection*)connection didReceiveResponse:(NSURLResponse*)response
{
NSLog(@"didReceiveResponse");
//1.得到文件的总大小(本次请求的文件数据的总大小!=文件的总大小)
// self.totalSize = response.expectedContentLength + self.currentSize;
if(self.currentSize>0) {
return;
}
self.totalSize= response.expectedContentLength;
//2.写数据到沙盒中
self.fullPath= [[NSSearchPathForDirectoriesInDomains(NSCachesDirectory,NSUserDomainMask,YES)lastObject]stringByAppendingPathComponent:@"123.jpg"];
NSLog(@"%@",self.fullPath);
//3.创建输出流
//NSOutputStream
//NSInputStream
/*
第一个参数:文件的路径
第二个参数:YES追加
特点:如果该输出流指向的地址没有文件,那么会自动创建一个空的文件
*/
NSOutputStream*stream = [[NSOutputStreamalloc]initToFileAtPath:self.fullPathappend:YES];
//打开输出流
[streamopen];
self.stream= stream;
}
-(void)connection:(NSURLConnection*)connection didReceiveData:(NSData*)data
{
//写数据
[self.streamwrite:data.bytesmaxLength:data.length];
//3.获得进度
self.currentSize+= data.length;
//进度=已经下载/文件的总大小
NSLog(@"%f",1.0*self.currentSize/self.totalSize);
self.progressView.progress=1.0*self.currentSize/self.totalSize;
//NSLog(@"%@",self.fullPath);
}
-(void)connection:(NSURLConnection*)connection didFailWithError:(NSError*)error
{
}
-(void)connectionDidFinishLoading:(NSURLConnection*)connection
{
//关闭流
[self.streamclose];
self.stream=nil;
NSLog(@"connectionDidFinishLoading");
NSLog(@"%@",self.fullPath);
}
@end