zoukankan      html  css  js  c++  java
  • NSOutputStreamNSInputStream

    NSOutputStream-保存网络资源到本地

    _filePath = [[NetworkManager sharedInstance] pathForTemporaryFileWithPrefix:@"Get"];

    _fileStream = [NSOutputStream outputStreamToFileAtPath:self.filePath append:NO];

    [_fileStream open];

     
     
    2.进行网络请求,并返回网络的数据。
     
    通过输出流对象,将数据写入到指定的文件中。
     
    - (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data{

        NSInteger       dataLength;

        const uint8_t * dataBytes;

        NSInteger       bytesWritten;

        NSInteger       bytesWrittenSoFar;

        

        // 接收到的数据长度

        dataLength = [data length];

        dataBytes  = [data bytes];

        

        bytesWrittenSoFar = 0;

        do {

            bytesWritten = [self.fileStream write:&dataBytes[bytesWrittenSoFar]maxLength:dataLength - bytesWrittenSoFar];

            assert(bytesWritten != 0);

            if (bytesWritten == -1) {

                break;

            } else {

                bytesWrittenSoFar += bytesWritten;

            }

        } while (bytesWrittenSoFar != dataLength);

    }

    NSInputStream和NSMutableURLRequest-实现保存文件到服务器

    1.准备工作,将需要的类进行声明定义。
    *.h 文件

    NSURLConnection* _aSynConnection;

    NSInputStream *_inputStreamForFile;

    NSString *_localFilePath;

     

    @property (nonatomic,retain) NSURLConnection* aSynConnection;

    @property (nonatomic,retainNSInputStream *inputStreamForFile;

    @property (nonatomic,retain) NSString *localFilePath;

     
    *.m 文件

    @synthesize inputStreamForFile=_inputStreamForFile;

    @synthesize localFilePath=_localFilePath;

    @synthesize aSynConnection=_aSynConnection;

     
    2.进行请求
     

    - (void)btnClickAction:(id)sender{

        // 初始化目标地址的URL(发送到哪里)

        NSURL *serverURL;

        NSString *strURL=@"http://www.xxx.com/fileName.png";// 这里用图片为例

    strURL = [strURL stringByAddingPercentEscapesUsingEncoding: NSUTF8StringEncoding];

        serverURL=[NSURL URLWithString:strURL];

        

        // 初始化本地文件路径,并与NSInputStream链接

        self.localFilePath=@"本地的图片路径";

        self.inputStreamForFile = [NSInputStream inputStreamWithFileAtPath:self.localFilePath];

        

        // 上传大小

        NSNumber *contentLength;

        contentLength = (NSNumber *) [[[NSFileManager defaultManager]attributesOfItemAtPath:self.localFilePath error:NULL] objectForKey:NSFileSize];

        

     

        NSMutableURLRequest *request;

        request = [NSMutableURLRequest requestWithURL:serverURL];

        [request setHTTPMethod:@"PUT"];

        [request setHTTPBodyStream:self.inputStreamForFile];

        [request setValue:@"image/png" forHTTPHeaderField:@"Content-Type"];

        [request setValue:[contentLength description] forHTTPHeaderField:@"Content-Length"];

        

        // 请求

        self.aSynConnection = [NSURLConnection connectionWithRequest:requestdelegate:self];

    }

     
    3.接受回调函数的状态,判断是否上传成功。

     

    // 收到响应时,会触发

    - (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse*)aResponse{

        NSLog(@"请求成功!");

        returnInfoData=[[NSMutableData alloc]init];

        totalSize= [aResponse expectedContentLength];

        

        NSHTTPURLResponse * httpResponse;

        httpResponse = (NSHTTPURLResponse *)aResponse;

        if ((httpResponse.statusCode / 100) != 2) {

            NSLog(@"保存失败");

        } else {

            NSLog(@"保存成功");

        }

    }

  • 相关阅读:
    Jvm年轻代复制到Survivor To区时,对象存放不下会发生什么?
    Jvm内存布局和Java对象内存布局
    ArrayList的removeIf和iterator.remove性能比较
    闲着没事做,用js做了一个冒泡排序的动画
    对象与this
    idea 简记
    线程按序交替
    大数阶乘
    序列化 与 反序列化
    人月神话
  • 原文地址:https://www.cnblogs.com/hbf369/p/3581986.html
Copyright © 2011-2022 走看看