iOS之通过NSOutputStream大文件下载
Posted 学东哥哥
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了iOS之通过NSOutputStream大文件下载相关的知识,希望对你有一定的参考价值。
#import "ViewController.h" @interface ViewController () <NSURLConnectionDataDelegate> /** 输出流对象 */ @property (nonatomic, strong) NSOutputStream *stream; @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; [NSURLConnection connectionWithRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:@"http://120.25.226.186:32812/resources/videos/minion_01.mp4"]] delegate:self]; } #pragma mark - <NSURLConnectionDataDelegate> - (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response { // response.suggestedFilename : 服务器那边的文件名 // 文件路径 NSString *caches = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) lastObject]; NSString *file = [caches stringByAppendingPathComponent:response.suggestedFilename]; NSLog(@"%@", file); // 利用NSOutputStream往Path中写入数据(append为YES的话,每次写入都是追加到文件尾部) self.stream = [[NSOutputStream alloc] initToFileAtPath:file append:YES]; // 打开流(如果文件不存在,会自动创建) [self.stream open]; } - (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data { [self.stream write:[data bytes] maxLength:data.length]; NSLog(@"didReceiveData-------"); } - (void)connectionDidFinishLoading:(NSURLConnection *)connection { [self.stream close]; NSLog(@"-------"); } @end
以上是关于iOS之通过NSOutputStream大文件下载的主要内容,如果未能解决你的问题,请参考以下文章
源码0603-01-了解-大文件下载(NSOutputStream)
NSOutputStream 自 iOS 8.3 更新后停止工作