小文件下载
如果文件比较小,下载方式会比较多
直接用
NSData
的dataWithContentsOfURL
方法NSURL *url = [NSURL URLWithString:@"sss"]; NSData *data = [NSData dataWithContentsOfURL:url];
利用
NSURLConnection
发送一个HTTP请求//方法一: 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); }]; 方法二: NSURL *url = [NSURL URLWithString:@"ss"]; [NSURLConnection connectionWithRequest:[NSURLRequest requestWithURL:url] delegate:self]; //NSURLConnectionDataDelegate - (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSHTTPURLResponse *)response { self.contentLength = [response.allHeaderFields[@"Content-Length"] integerValue]; 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:@"qq.mp4"]; // 写入数据 [self.fileData writeToFile:file atomically:YES]; self.fileData = nil; }
如果是下载图片,还可以利用
SDWebImage
大文件下载(不考虑断点下载)
第一种
#define XMGFile [[NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) lastObject] stringByAppendingPathComponent:@"qq.mp4"]
/** 文件的总长度 */
@property (nonatomic, assign) NSInteger contentLength;
/** 当前下载的总长度 */
@property (nonatomic, assign) NSInteger currentLength;
/** 文件句柄对象 */
@property (nonatomic, strong) NSFileHandle *handle;
NSURL *url = [NSURL URLWithString:@"qwer"];
[NSURLConnection connectionWithRequest:[NSURLRequest requestWithURL:url] delegate:self];
//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];
}
/**
* 接收到具体数据:马上把数据写入一开始创建好的文件
*/
- (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;
}
第一种
/** 输出流对象 */
@property (nonatomic, strong) NSOutputStream *stream;
[NSURLConnection connectionWithRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:@"sss"]] delegate:self];
//NSURLConnectionDataDelegate
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
//response.suggestedFilename : 服务器那边的文件名
// 文件路径
NSString *caches = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) lastObject];
NSString *file = [caches stringByAppendingPathComponent:response.suggestedFilename];
// 利用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];
}
- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
//关闭流
[self.stream close];
}