iOS网络编程(七)

作者: BEYOND黄 | 来源:发表于2017-06-01 15:46 被阅读42次

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

相关文章

  • iOS网络编程(七)

    NSURLConnection实现大文件断点下载:采用输出流来继续写文件 #import"ViewControll...

  • iOS搭建Socket服务器的相关方法

    iOS网络编程层次 iOS网络编程层次结构也分为三层: Cocoa层:NSURL,Bonjour,Game Kit...

  • Socket

    Socket iOS网络编程层次结构 iOS网络编程层次结构分为三层,从上往下依次为: Cocoa层:NSURL,...

  • iOS面试题宝典

    objective-c语法篇 内存管理篇 UI篇 iOS网络编程篇 iOS多线程篇 数据持久化篇 杂七杂八篇 计算...

  • iOS关于HTTP协议和网络编程

    1.网络编程 1>什么是网络编程? 网络编程,是我们iOS程序开发者针对网络模块进行得代码编程,是作为一个资深开发...

  • IOS网络编程

    IOS网络编程 NSURLConnection NSURLSession是NSURLConnection 的替代者...

  • 网络相关以及TCP/IP协议

    一、iOS9 网络编程的重大改变: 1、网络请求方式的改变 1)NSURLConnection: iOS9之前使用...

  • iOS网络编程

    iOS网络相关类介绍 网络请求地址对象——NSURL url 介绍url,统一资源定位符,也被称为网址,因特网上标...

  • iOS网络编程

    网络编程 1. 概论 建立连接:通过IP或者域名来连接两台设备,通过端口号找到对应的通信程序 通信协议:要传输的数...

  • iOS网络编程

    一、URL URL的全称是Uniform Resource Locator(统一资源定位符),通过1个URL,能找...

网友评论

    本文标题:iOS网络编程(七)

    本文链接:https://www.haomeiwen.com/subject/zfdefxtx.html