您的位置:首页 > 其它

大文件与小文件下载

2016-04-07 10:16 267 查看
小文件

如果文件比较小,下载方式会比较多

1.直接用NSData的 + (id)dataWithConnentsOfURL:(NSURL *)url;

NSData *data = [NSData dataWithContentsOfURL:url];
NSLog(@"%zd",data.length);


2.利用NSURLConnection发送一个HTTP请求去下载

可以直接发送请求,

[NSURLConnection sendAsynchronousRequest:[NSURLRequest requestWithURL:url] queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse * _Nullable response, NSData * _Nullable data, NSError * _Nullable connectionError) {

NSLog(@"%zd",data.length);
}];


也可以使用代理方法

NSURLConnection *conn = [NSURLConnection connectionWithRequest:[NSURLRequest requestWithURL:url] delegate:self];


具体的代理方法

#pragma mark -<NSURLConnection>
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response {
//服务器那边的文件名
//    NSLog(@"%@",response.suggestedFilename);

//文件路径
NSString *caches = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) lastObject];

NSString *file = [caches stringByAppendingString:response.suggestedFilename];

//利用NSOutStream往Path中写入数据(append)为YES的话,每次写入都是追加
self.stream = [[NSOutputStream alloc] initToFileAtPath:file append:YES];
//打开流(如果文件不存在会自动创建)
[self.stream open];
NSLog(@"%@",file);
}

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
[self.stream write:[data bytes] maxLength:data.length];
}

-(void)connectionDidFinishLoading:(NSURLConnection *)connection {

[self.stream close];
}


View Code

二.(主要使用NSURLSession方法)

- (void)download {

//获得NSURLSession对象
NSURLSession *session = [NSURLSession sharedSession];

//获得下载任务
NSURLSessionDownloadTask *task = [session downloadTaskWithURL:[NSURL URLWithString:@"http://120.25.226.186:32812/resources/images/minion_01.png"] completionHandler:^(NSURL * _Nullable location, NSURLResponse * _Nullable response, NSError * _Nullable error) {
NSLog(@"文件下载完毕-----%@",location);

//文件将来存放的真实路径
NSString *file = [[NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) lastObject] stringByAppendingPathComponent:response.suggestedFilename];

//剪切location的临时文件到真实路径
NSFileManager *manager = [NSFileManager defaultManager];
[manager moveItemAtURL:location toURL:[NSURL fileURLWithPath:file] error:nil];
}];

//启动任务
[task resume];

}


NSURLSession是将文件下载到沙盒中的tmp文件目录下

我们需要将其剪切到Caches目录下,才能长久保存。

三.(主要使用AFNetworking框架)

- (void)upload {
AFHTTPSessionManager *mgr = [AFHTTPSessionManager manager];

[mgr POST:@"http://120.25.226.186:32812/upload" parameters:nil constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
//在这个block中设置需要上传的文件
NSData *data = [NSData dataWithContentsOfFile:@"/Users/DDZ/Desktop/test.png"];
[formData appendPartWithFileData:data name:@"file" fileName:@"test.png" mimeType:@"image/png"];
} success:^(NSURLSessionDataTask *task, id responseObject) {

NSLog(@"---%@",responseObject);
} failure:^(NSURLSessionDataTask *task, NSError *error) {

}];
}


大文件与小文件下载的本质区别是

小文件是全部下载之后进行拼接,内存占用会不断增大

大文件是下载一点就去文件写一点,内存占用基本不变
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: