zoukankan      html  css  js  c++  java
  • iOS 通过NSOutputStream 来下载大文件

    #import "ViewController.h"

    @interface ViewController ()<NSURLConnectionDataDelegate>

    @property (nonatomic, strong) NSOutputStream *stream; // 全局,每次下载的内容能够持续的写入

    @end

     @implementation ViewController

     - (void)viewDidLoad {

        [super viewDidLoad];

        [NSURLConnection connectionWithRequest:[NSURLRequest requestWithURL:@""] delegate:self];

    }

    #pragma mark-<NSURLConnectionDataDelegate>

     - (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response{// 接收响应

         NSString *caches = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES)  lastObject];

        // 沙盒文件的路经 

        NSString *file = [caches stringByAppendingString:response.suggestedFilename];// 和原来下载文件的后缀名保持一致性

       // 让接收的数据不断的写入沙盒文件

        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];

    }

  • 相关阅读:
    Eclipse Alt + / 快捷键失效
    oracle nvl()函数
    搭建spring boot项目
    Maximum call stack size exceeded
    vue混入函数问题
    ASP.NET Core 2.0中的Azure Blob存储
    如何在ASP.NET Core 2.0中使用Razor页面
    将参数传递给ASP.NET Core 2.0中的中间件
    使用.net core在Ubuntu构建一个TCP服务器
    如何在ASP.NET Core Web API测试中使用Postman
  • 原文地址:https://www.cnblogs.com/1018475062qq/p/7020178.html
Copyright © 2011-2022 走看看