文件下载(NSURLConnectionNSURLSession)

#pragma mark - 大文件下载

//方法一: NSURLConnection (合理:单个线程 下载一点就写入一点)使用NSFileHandle
//句柄对象
@property (nonatomic, strong) NSFileHandle * writeHandle;

- (void)downloadFile4
{
    NSURL *url = [NSURL URLWithString:@"http://localhost:8080/Server/resources/videos.zip"];

    NSURLRequest *request = [NSURLRequest requestWithURL:url];

    // 下载(创建完conn对象后,会自动发起一个异步请求,通过delegate回调下载信息)
    [NSURLConnection connectionWithRequest:request delegate:self];
}

#pragma mark - NSURLConnectionDataDelegate
- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
{//请求失败

}

// 1.接收到服务器的响应就会调用
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
    // 文件路径
    NSString *caches = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) lastObject];
    NSString *filepath = [caches stringByAppendingPathComponent:@"videos.zip"];

    // 创建一个空的文件 到 沙盒中
    NSFileManager *mgr = [NSFileManager defau< Manager];
    [mgr createFileAtPath:filepath contents:nil attributes:nil];

    // 创建一个用来写数据的文件句柄
    self.writeHandle = [NSFileHandle fileHandleForWritingAtPath:filepath];
}

// 2.当接收到服务器返回的实体数据时调用(具体内容,这个方法可能会被调用多次)
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
    // 移动到文件的最后面
    [self.writeHandle seekToEndOfFile];

    // 将数据写入沙盒
    [self.writeHandle writeData:data];
}

// 3.加载完毕后调用(服务器的数据已经接收完毕)
- (void)connectionDidFinishLoading:(NSURLConnection *)connection;
{
    // 关闭文件 设置为空
    [self.writeHandle closeFile];
    self.writeHandle = nil;
}

//文件流
@property (nonatomic, strong) NSOutputStream * fileStream;

//方法二: NSURLConnection 使用NSOutputStream
- (void)downloadFile6
{
    NSURL *url = [NSURL URLWithString:@"http://localhost:8080/Server/resources/videos.zip"];

    NSURLRequest *request = [NSURLRequest requestWithURL:url];

    // 下载(创建完conn对象后,会自动发起一个异步请求,通过delegate回调下载信息)
    [NSURLConnection connectionWithRequest:request delegate:self];
}

#pragma mark - NSURLConnectionDataDelegate
- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
{//请求失败

}

// 1.接收到服务器的响应就会调用
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
    // 文件路径
    NSString *caches = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) lastObject];
    NSString *filepath = [caches stringByAppendingPathComponent:@"videos.zip"];

    // 创建一个空的文件 到 沙盒中
    NSFileManager *mgr = [NSFileManager defau< Manager];
    [mgr createFileAtPath:filepath contents:nil attributes:nil];

    // 创建一个用来写数据的文件句柄
    self.fileStream = [NSOutputStream outputStreamToFileAtPath:filepath append:YES];
}

// 2.当接收到服务器返回的实体数据时调用(具体内容,这个方法可能会被调用多次)
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
    //将数据追加到文件流中
    [self.fileStream write:data.bytes maxLen >h:data.len >h];
}

// 3.加载完毕后调用(服务器的数据已经接收完毕)
- (void)connectionDidFinishLoading:(NSURLConnection *)connection;
{
    // 关闭文件流
    [self.fileStream close];
}

更多内容--博客导航每周一篇哟!!!

有任何关于iOS开发的问题!欢迎下方留言!!!或者邮件lieryangios@126.com 虽然我不一定能够解答出来,但是我会请教iOS开发高手!!!解答您的问题!!!

文件下载(NSURLConnection/NSURLSession)

原文地址:http://www.cnblogs.com/CoderEYLee/p/Object-C-0029.html


最新回复(0)
/jishuQYFwYrEQkGAZuOxJy3Pq_2Fn0tf6BcqjoKo0kJ9_2BFgAvI_3D4719336
8 简首页