// // ViewController.m // 06-掌握-小文件下载 #import "ViewController.h" @interface ViewController () <NSURLConnectionDataDelegate> @property (weak, nonatomic) IBOutlet UIProgressView *progressView; /** 文件数据 */ @property (nonatomic, strong) NSMutableData *fileData; /** 文件的总长度 */ @property (nonatomic, assign) NSInteger contentLength; @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; //要请求url文件 NSURL *url = [NSURL URLWithString:@"http://120.25.226.186:32812/resources/videos/minion_15.mp4"]; [NSURLConnection connectionWithRequest:[NSURLRequest requestWithURL:url] delegate:self];//使用代理 } #pragma mark - <NSURLConnectionDataDelegate> - (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSHTTPURLResponse *)response { //接收到了响应; self.contentLength = [response.allHeaderFields[@"Content-Length"] integerValue]; //拿到响应头,得到下载文件的总大小,目的计算下载的百分比; //self.contentLength 要拿到下面用,so给了个强引用; self.fileData = [NSMutableData data];//接收到响应后,才创建这个临时存放数据的数据对象;要拿到写入沙盒那一步使用; } - (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data { //不断的收数据,数据的拼接;收一点写一点去沙盒; [self.fileData appendData:data]; CGFloat progress = 1.0 * self.fileData.length / self.contentLength;//下载百分比 NSLog(@"已下载:%.2f%%", (progress) * 100); self.progressView.progress = progress; } - (void)connectionDidFinishLoading:(NSURLConnection *)connection { //上一步的数据收集完了; NSLog(@"下载完毕"); //来到缓存沙盒路径,拼接文件路径,将收完的数据写入这个文件路径,临时的存放数据对象清空; // 将文件写入沙盒中 // 缓存文件夹 NSString *caches = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) lastObject]; // 文件路径 NSString *file = [caches stringByAppendingPathComponent:@"minion_15.mp4"]; // 写入数据 [self.fileData writeToFile:file atomically:YES]; self.fileData = nil; } - (void)connDownload { //异步请求网路图片; NSURL *url = [NSURL URLWithString:@"http://120.25.226.186:32812/resources/images/minion_15.png"]; [NSURLConnection sendAsynchronousRequest:[NSURLRequest requestWithURL:url] queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) { NSLog(@"%zd", data.length); }]; } //文件下载其实就是get请求,能发get请求的就能下载; - (void)dataDownlaod { //使用网络上的图片; NSURL *url = [NSURL URLWithString:@"http://120.25.226.186:32812/resources/images/minion_15.png"]; //适合下载小文件;没有办法断点下载,暂停 NSData *data = [NSData dataWithContentsOfURL:url]; NSLog(@"%zd", data.length); } @end
07-掌握-大文件下载
// ViewController.m // 06-掌握-小文件下载 #import "ViewController.h" #define XMGFile [[NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) lastObject] stringByAppendingPathComponent:@"minion_15.mp4"] @interface ViewController () <NSURLConnectionDataDelegate> @property (weak, nonatomic) IBOutlet UIProgressView *progressView; /** 文件的总长度 */ @property (nonatomic, assign) NSInteger contentLength; /** 当前下载的总长度 */ @property (nonatomic, assign) NSInteger currentLength; /** 文件句柄对象 */ @property (nonatomic, strong) NSFileHandle *handle; @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; NSURL *url = [NSURL URLWithString:@"http://120.25.226.186:32812/resources/videos/minion_15.mp4"]; [NSURLConnection connectionWithRequest:[NSURLRequest requestWithURL:url] delegate:self]; } #pragma mark - <NSURLConnectionDataDelegate> /** * 接收到响应的时候:创建一个空的文件 */ - (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSHTTPURLResponse *)response { // 获得文件的总长度 self.contentLength = [response.allHeaderFields[@"Content-Length"] integerValue]; // 创建一个空的文件 [[NSFileManager defaultManager] createFileAtPath:XMGFile contents:nil attributes:nil]; // 创建文件句柄 self.handle = [NSFileHandle fileHandleForWritingAtPath:XMGFile];//fileHandlewritingAtPath用来写文件的 } /** * 接收到具体数据:马上把数据写入一开始创建好的文件 */ - (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data { // 指定数据的写入位置 -- 文件内容的最后面 [self.handle seekToEndOfFile];//挪到最后面 // 写入数据 [self.handle writeData:data];//从最后面开始写入数据; // 拼接总长度 self.currentLength += data.length; // 进度 self.progressView.progress = 1.0 * self.currentLength / self.contentLength; } - (void)connectionDidFinishLoading:(NSURLConnection *)connection { // 关闭handle [self.handle closeFile]; self.handle = nil; // 清空长度 self.currentLength = 0; } @end
时间: 2024-10-11 11:32:37