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,retain) NSInputStream *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(@"保存成功");

}

}

NSOutputStream\NSInputStream的更多相关文章

  1. NSStream

    NSStream 流是位数据通过通信路径的连续传送序列.它是单向的,从一个应用程序的角度,流可以是输入流(读操作流)或者输出流(写操作流),除了基于文件的流之外,其余的都是non-seekable的. ...

  2. ios开发网络学习五:输出流以及文件上传

    一:输出流 #import "ViewController.h" @interface ViewController ()<NSURLConnectionDataDelega ...

  3. iOS开发——网络篇——文件下载(NSMutableData、NSFileHandle、NSOutputStream)和上传、压缩和解压(三方框架ZipArchive),请求头和请求体格式,断点续传Range

    一.小文件下载 NSURL *url = [NSURL URLWithString:@"http://120.25.226.186:32812/resources/images/minion ...

  4. Cocoa Touch(二):数据存储CoreData, NSKeyArchiver, NSOutputStream, NSUserDefaults

    应用程序离不开数据的永久存储,有两种方式实现存储:数据库和文本文件. 作为存储管理器,最基本的功能就是增删改查了. CoreData 1.插入 AppDelegate *app = [[UIAppli ...

  5. iOS开发系列-NSOutputStream

    NSOutputStream 创建一个NSOutputStream实例 - (nullable instancetype)initToFileAtPath:(NSString *)path appen ...

  6. 【原】AFNetworking源码阅读(五)

    [原]AFNetworking源码阅读(五) 本文转载请注明出处 —— polobymulberry-博客园 1. 前言 上一篇中提及到了Multipart Request的构建方法- [AFHTTP ...

  7. AFNetworking 3.0 源码解读 总结(干货)(下)

    承接上一篇AFNetworking 3.0 源码解读 总结(干货)(上) 21.网络服务类型NSURLRequestNetworkServiceType 示例代码: typedef NS_ENUM(N ...

  8. AFNetworking 3.0 源码解读(三)之 AFURLRequestSerialization

    这篇就讲到了跟请求相关的类了 关于AFNetworking 3.0 源码解读 的文章篇幅都会很长,因为不仅仅要把代码进行详细的的解释,还会大概讲解和代码相关的知识点. 上半篇: URI编码的知识 关于 ...

  9. iOS---后台运行机制详解

    一.iOS的“伪后台”程序 首先,先了解一下iOS 中所谓的「后台进程」到底是怎么回事吧? Let me be as clear as I can be: the iOS multitasking b ...

随机推荐

  1. Bernese安装及使用

    一.安装: 伯尔尼软件的安装很简单,但是在64位下,可能perl解释器安装不成功,我找了一个,并且可用,下载地址: 链接:http://pan.baidu.com/s/1hr8fgEC 密码:fj8b ...

  2. css.day05

    1. 外边距合并  不是bug  而是一个特性  (以最大的那个边距为准) 两个盒子是并列关系 两个盒子 父子级关系 1. border 2.overflow:hidden; 3. padding  ...

  3. HTML5 FileAPI读取实例---(一)

    在HTML5中,提供了一个关于文件操作的API,通过这个API,对于从web页面上访问本地文件系统的相关处理变得十分简单.到目前为止只有部分浏览器对它提供支持. 1.FileList对象和File对象 ...

  4. 关于异常的疑难解答:System.Runtime.InteropServices.COMException

    COMException exception is thrown when an unrecognized HRESULT is returned from a COM method call.&qu ...

  5. Delphi 用ToolButton和MonthCalendar实现DateTimePicker的功能

    效果图如下: 实现平台:xp xe2,其中以上功能的实现,核心主要是参考了万一老师的资料,连接:http://www.cnblogs.com/del/archive/2011/05/12/204411 ...

  6. Java中long和double的原子性

    Java中long和double的原子性 java中基本类型中,long和double的长度都是8个字节,32位(4字节)处理器对其读写操作无法一次完成,那么,JVM,long和double是原子性的 ...

  7. 跟我学android-使用Eclipse开发第一个Android应用(三)

    打开Eclipse,选择 File—New –Android Application Project Application Name  就是我们的 应用名称,也是我们在手机应用程序列表里看到的名称. ...

  8. jquery 的日期时间控件(年月日时分秒)

    <!-- import package --> <script type="text/javascript" src="JS/jquery.js&quo ...

  9. curl 解析

    获得页面 使用命令:curl http://curl.haxx. se 这是最简单的使用方法.用这个命令获得了http://curl.haxx. se指向的页面,同样,如果这里的URL指向的是一个文件 ...

  10. HDU 1005(周期问题)

    HDU 1005 Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u   Descript ...