iOS开发 大文件下载(支持断点下载)
2015-09-17 22:04
676 查看
网络连接不支持汉字,需要将 含有汉字的网址转UTF-8 编码,格式如下
NSString *pathUrl = @"http://10.0.8.8/download/iOS各种理论知识.pdf";
NSURL *url = [NSURL URLWithString:[pathUrl stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
// 默认就是GET请求
NSURLRequest *request = [NSURLRequest requestWithURL:url];
//
// HMViewController.m
// 03-大文件下载
//
// Created by apple on 14-6-27.
// Copyright (c) 2014年 heima. All rights reserved.
//
#import "HMViewController.h"
@interface HMViewController ()
@property (weak, nonatomic) IBOutlet UIProgressView *progressView;
/**
* 写数据的文件句柄
*/
@property (nonatomic, strong) NSFileHandle *writeHandle;
/**
* 当前已下载数据的长度
*/
@property (nonatomic, assign) long long currentLength;
/**
* 完整文件的总长度
*/
@property (nonatomic, assign) long long totalLength;
/**
* 连接对象
*/
@property (nonatomic, strong) NSURLConnection *conn;
/**
* 是否在下载
*/
@property (nonatomic, assign, getter = isDownloading) BOOL downloading;
- (IBAction)start:(UIButton *)button;
@end
@implementation HMViewController
- (void)viewDidLoad
{
[super viewDidLoad];
}
// 按钮文字: "开始", "暂停"
- (IBAction)start:(UIButton *)button { // self.currentLength == 200
if (self.isDownloading) { // 暂停下载
self.downloading = NO;
[button setTitle:@"开始" forState:UIControlStateNormal];
// 取消当前的请求
[self.conn cancel];
self.conn = nil;
} else { // 开始下载
self.downloading = YES;
[button setTitle:@"暂停" forState:UIControlStateNormal];
NSURL *url = [NSURL URLWithString:@"http://192.168.1.200:8080/MJServer/resources/jre.zip"];
// 默认就是GET请求
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
// 设置请求头信息
NSString *value = [NSString stringWithFormat:@"bytes=%lld-", self.currentLength];
[request setValue:value forHTTPHeaderField:@"Range"];
self.conn = [NSURLConnection connectionWithRequest:request delegate:self];
}
}
#pragma mark - NSURLConnectionDataDelegate 代理方法
/**
* 1. 当接受到服务器的响应(连通了服务器)就会调用
*/
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
#warning 一定要判断
if (self.totalLength) return;
// 0.文件的存储路径
NSString *caches = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) lastObject];
NSString *filepath = [caches stringByAppendingPathComponent:@"jre.zip"];
// 1.创建一个空的文件到沙盒中
NSFileManager *mgr = [NSFileManager defaultManager];
// 刚创建完毕的大小是0字节
[mgr createFileAtPath:filepath contents:nil attributes:nil];
// 2.创建写数据的文件句柄
self.writeHandle = [NSFileHandle fileHandleForWritingAtPath:filepath];
// 3.获得完整文件的长度
self.totalLength = response.expectedContentLength;
}
/**
* 2. 当接受到服务器的数据就会调用(可能会被调用多次, 每次调用只会传递部分数据)
*/
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
// 累加长度
self.currentLength += data.length;
// 显示进度
double progress = (double)self.currentLength / self.totalLength;
self.progressView.progress = progress;
// 移动到文件的尾部
[self.writeHandle seekToEndOfFile];
// 从当前移动的位置(文件尾部)开始写入数据
[self.writeHandle writeData:data];
}
/**
* 3. 当服务器的数据接受完毕后就会调用
*/
- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
NSLog(@"connectionDidFinishLoading----");
// 清空属性值
self.currentLength = 0;
self.totalLength = 0;
// 关闭连接(不再输入数据到文件中)
[self.writeHandle closeFile];
self.writeHandle = nil;
}
/**
* 请求错误(失败)的时候调用(请求超时\断网\没有网, 一般指客户端错误)
*/
- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
{
}
@end
NSString *pathUrl = @"http://10.0.8.8/download/iOS各种理论知识.pdf";
NSURL *url = [NSURL URLWithString:[pathUrl stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
// 默认就是GET请求
NSURLRequest *request = [NSURLRequest requestWithURL:url];
//
// HMViewController.m
// 03-大文件下载
//
// Created by apple on 14-6-27.
// Copyright (c) 2014年 heima. All rights reserved.
//
#import "HMViewController.h"
@interface HMViewController ()
@property (weak, nonatomic) IBOutlet UIProgressView *progressView;
/**
* 写数据的文件句柄
*/
@property (nonatomic, strong) NSFileHandle *writeHandle;
/**
* 当前已下载数据的长度
*/
@property (nonatomic, assign) long long currentLength;
/**
* 完整文件的总长度
*/
@property (nonatomic, assign) long long totalLength;
/**
* 连接对象
*/
@property (nonatomic, strong) NSURLConnection *conn;
/**
* 是否在下载
*/
@property (nonatomic, assign, getter = isDownloading) BOOL downloading;
- (IBAction)start:(UIButton *)button;
@end
@implementation HMViewController
- (void)viewDidLoad
{
[super viewDidLoad];
}
// 按钮文字: "开始", "暂停"
- (IBAction)start:(UIButton *)button { // self.currentLength == 200
if (self.isDownloading) { // 暂停下载
self.downloading = NO;
[button setTitle:@"开始" forState:UIControlStateNormal];
// 取消当前的请求
[self.conn cancel];
self.conn = nil;
} else { // 开始下载
self.downloading = YES;
[button setTitle:@"暂停" forState:UIControlStateNormal];
NSURL *url = [NSURL URLWithString:@"http://192.168.1.200:8080/MJServer/resources/jre.zip"];
// 默认就是GET请求
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
// 设置请求头信息
NSString *value = [NSString stringWithFormat:@"bytes=%lld-", self.currentLength];
[request setValue:value forHTTPHeaderField:@"Range"];
self.conn = [NSURLConnection connectionWithRequest:request delegate:self];
}
}
#pragma mark - NSURLConnectionDataDelegate 代理方法
/**
* 1. 当接受到服务器的响应(连通了服务器)就会调用
*/
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
#warning 一定要判断
if (self.totalLength) return;
// 0.文件的存储路径
NSString *caches = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) lastObject];
NSString *filepath = [caches stringByAppendingPathComponent:@"jre.zip"];
// 1.创建一个空的文件到沙盒中
NSFileManager *mgr = [NSFileManager defaultManager];
// 刚创建完毕的大小是0字节
[mgr createFileAtPath:filepath contents:nil attributes:nil];
// 2.创建写数据的文件句柄
self.writeHandle = [NSFileHandle fileHandleForWritingAtPath:filepath];
// 3.获得完整文件的长度
self.totalLength = response.expectedContentLength;
}
/**
* 2. 当接受到服务器的数据就会调用(可能会被调用多次, 每次调用只会传递部分数据)
*/
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
// 累加长度
self.currentLength += data.length;
// 显示进度
double progress = (double)self.currentLength / self.totalLength;
self.progressView.progress = progress;
// 移动到文件的尾部
[self.writeHandle seekToEndOfFile];
// 从当前移动的位置(文件尾部)开始写入数据
[self.writeHandle writeData:data];
}
/**
* 3. 当服务器的数据接受完毕后就会调用
*/
- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
NSLog(@"connectionDidFinishLoading----");
// 清空属性值
self.currentLength = 0;
self.totalLength = 0;
// 关闭连接(不再输入数据到文件中)
[self.writeHandle closeFile];
self.writeHandle = nil;
}
/**
* 请求错误(失败)的时候调用(请求超时\断网\没有网, 一般指客户端错误)
*/
- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
{
}
@end
相关文章推荐
- iOS疯狂讲解之手势识别器
- iOS开发中 new与alloc/init的区别 及 [NSArray array] 和 [[NSArray alloc]init] 及 self. 和 _ 的区别
- [置顶] iOS中copy和mutableCopy的详细分析
- IOS 使代码在ARC和MRC环境编译通用
- iOS 中TextKit的使用中,设置部分字体,出现的问题及解决方法
- IOS GDC实现单例写法ARC与MRC
- IOS--Quartz2D绘图绘制小黄人
- iOS应用开发-地图定位
- IOS--Quartz2D绘图
- JSON TO NSDictionary Mac & iOS
- IOS征途之三 分类与协议(二)
- ios 爱行华夏 技术支持
- iOS开发-获取设备电量
- ios9键盘回调与消息通知,理论上应该是个bug
- iOS开发-浅解runtime
- iOS开发—几种定时器
- iOS-多线程之GCD
- iOS疯狂讲解之加载视图的好处
- iOS程序main函数之前发生了什么
- iOS中的瀑布流(RootCollectionViewControlle)