用输出流下载文件

Posted on 2016-07-19 23:56  柠檬片  阅读(260)  评论(0)    收藏  举报

 //创建一个数据输出流

    /*

     第一个参数:二进制的流数据要写入到哪里

     第二个参数:采用什么样的方式写入流数据,如果YES则表示追加,如果是NO则表示覆盖

     */

    self.stream = [NSOutputStream outputStreamToFileAtPath:fullPath append:YES];

    

    //只要调用了该方法就会往文件中写数据

    //如果文件不存在,那么会自动的创建一个

    [self.stream open];

 

 //使用输出流写数据

    /*

     第一个参数:要写入的二进制数据

     第二个参数:要写入的数据的大小

     */

    [self.stream write:data.bytes maxLength:data.length];

    

 

    //关闭输出流

    [self.stream close];

 

  1 #import "ViewController.h"
  2 
  3 @interface ViewController ()<NSURLConnectionDataDelegate>
  4 @property (weak, nonatomic) IBOutlet UIImageView *imagView;
  5 @property (weak, nonatomic) IBOutlet UIProgressView *progress;
  6 - (IBAction)startBtnClick:(id)sender;
  7 - (IBAction)suspendBtnClick:(id)sender;
  8 
  9 
 10 
 11 //当前已经下载文件的大小
 12 @property (nonatomic, assign) NSInteger currentLength;
 13 //下载文件的总大小
 14 @property (nonatomic, assign) NSInteger totalLength;
 15 @property (nonatomic, strong)  NSString *fullPath;
 16 //文件句柄
 17 @property (nonatomic, strong) NSFileHandle *handle ;
 18 
 19 @property (nonatomic, strong) NSURLConnection *connect;
 20 
 21 @property (nonatomic, strong) NSOutputStream *stream;
 22 @end
 23 
 24 @implementation ViewController
 25 //开始下载
 26 - (IBAction)startBtnClick:(id)sender {
 27     
 28     //1.确定请求路径
 29     NSURL *url = [NSURL URLWithString:@"http://120.25.226.186:32812/resources/videos/minion_01.mp4"];
 30     //2.创建请求对象
 31     NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
 32     
 33     /*
 34      表示头500个字节:Range: bytes=0-499
 35      表示第二个500字节:Range: bytes=500-999
 36      表示最后500个字节:Range: bytes=-500
 37      表示500字节以后的范围:Range: bytes=500-
 38      */
 39     NSString *range = [NSString stringWithFormat:@"bytes=%zd-",self.currentLength];
 40     [request setValue:range forHTTPHeaderField:@"Range"];
 41     NSLog(@"%@",range);
 42     
 43     //3.设置代理,发送请求
 44     NSURLConnection *connect =  [NSURLConnection connectionWithRequest:request delegate:self];
 45     self.connect = connect;
 46 }
 47 //暂停
 48 - (IBAction)suspendBtnClick:(id)sender {
 49     
 50     NSLog(@"暂停--------------------%zd-",self.currentLength);
 51     
 52     [self.connect cancel];
 53 }
 54 
 55 
 56 #pragma mark  NSURLConnectionDataDelegate  start
 57 
 58 /*
 59  1.接收到服务器响应的适合调用
 60  */
 61 -(void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
 62 {
 63     NSLog(@"+++++++++++");
 64     
 65     if (self.currentLength > 0) {
 66         return;
 67     }
 68     NSFileManager *manager = [NSFileManager defaultManager];
 69     
 70     //保存下载的文件到沙河
 71     NSString *caches = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) lastObject];
 72     
 73     //拼接文件全路径
 74     NSString *fullPath = [caches stringByAppendingPathComponent:response.suggestedFilename];
 75     self.fullPath = fullPath;
 76     //创建文件
 77     [manager createFileAtPath:fullPath contents:nil attributes:nil];
 78     
 79     NSLog(@"%@",fullPath);
 80     
 81     
 82     //拿到文件的总大小
 83     self.totalLength = response.expectedContentLength + self.currentLength;
 84     
 85     NSLog(@"%zd",self.totalLength);
 86     
 87     //创建输出流
 88     //追加
 89     self.stream = [NSOutputStream outputStreamToFileAtPath:self.fullPath append:YES];
 90     
 91     //开启
 92     [self.stream open];
 93     
 94 }
 95 
 96 /*
 97  2.接收到服务器返回的数据,会调用多次
 98  */
 99 -(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
100 {
101 
102     self.currentLength +=data.length;
103     
104     //写数据
105     /*
106      第一个参数:要写入的二进制数据
107      第二个参数:数据的大小
108      */
109     [self.stream write:data.bytes maxLength:data.length];
110     
111     NSLog(@"%f",1.0 *self.currentLength / self.totalLength);
112     self.progress.progress = 1.0 *self.currentLength / self.totalLength;
113 }
114 
115 /*
116  3.当请求完成之后调用该方法
117  */
118 -(void)connectionDidFinishLoading:(NSURLConnection *)connection
119 {
120     NSLog(@"connectionDidFinishLoading");
121     
122     [self.stream close];
123     self.stream = nil;
124 }
125 /*
126  4.当请求失败的适合调用该方法,如果失败那么error有值
127  */
128 -(void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
129 {
130     NSLog(@"didFailWithError");
131     
132 }
用输出流下载大文件--断点下载