NSOutputStream\NSInputStream
NSOutputStream-保存网络资源到本地
_filePath = [[NetworkManager sharedInstance] pathForTemporaryFileWithPrefix:@"Get"];
_fileStream = [NSOutputStream outputStreamToFileAtPath:self.filePath append:NO];
[_fileStream open];
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-实现保存文件到服务器
NSURLConnection* _aSynConnection;
NSInputStream *_inputStreamForFile;
NSString *_localFilePath;
@property (nonatomic,retain) NSURLConnection* aSynConnection;
@property (nonatomic,retain) NSInputStream *inputStreamForFile;
@property (nonatomic,retain) NSString *localFilePath;
@synthesize inputStreamForFile=_inputStreamForFile;
@synthesize localFilePath=_localFilePath;
@synthesize aSynConnection=_aSynConnection;
- (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];
}
// 收到响应时,会触发
- (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的更多相关文章
- NSStream
NSStream 流是位数据通过通信路径的连续传送序列.它是单向的,从一个应用程序的角度,流可以是输入流(读操作流)或者输出流(写操作流),除了基于文件的流之外,其余的都是non-seekable的. ...
- ios开发网络学习五:输出流以及文件上传
一:输出流 #import "ViewController.h" @interface ViewController ()<NSURLConnectionDataDelega ...
- iOS开发——网络篇——文件下载(NSMutableData、NSFileHandle、NSOutputStream)和上传、压缩和解压(三方框架ZipArchive),请求头和请求体格式,断点续传Range
一.小文件下载 NSURL *url = [NSURL URLWithString:@"http://120.25.226.186:32812/resources/images/minion ...
- Cocoa Touch(二):数据存储CoreData, NSKeyArchiver, NSOutputStream, NSUserDefaults
应用程序离不开数据的永久存储,有两种方式实现存储:数据库和文本文件. 作为存储管理器,最基本的功能就是增删改查了. CoreData 1.插入 AppDelegate *app = [[UIAppli ...
- iOS开发系列-NSOutputStream
NSOutputStream 创建一个NSOutputStream实例 - (nullable instancetype)initToFileAtPath:(NSString *)path appen ...
- 【原】AFNetworking源码阅读(五)
[原]AFNetworking源码阅读(五) 本文转载请注明出处 —— polobymulberry-博客园 1. 前言 上一篇中提及到了Multipart Request的构建方法- [AFHTTP ...
- AFNetworking 3.0 源码解读 总结(干货)(下)
承接上一篇AFNetworking 3.0 源码解读 总结(干货)(上) 21.网络服务类型NSURLRequestNetworkServiceType 示例代码: typedef NS_ENUM(N ...
- AFNetworking 3.0 源码解读(三)之 AFURLRequestSerialization
这篇就讲到了跟请求相关的类了 关于AFNetworking 3.0 源码解读 的文章篇幅都会很长,因为不仅仅要把代码进行详细的的解释,还会大概讲解和代码相关的知识点. 上半篇: URI编码的知识 关于 ...
- iOS---后台运行机制详解
一.iOS的“伪后台”程序 首先,先了解一下iOS 中所谓的「后台进程」到底是怎么回事吧? Let me be as clear as I can be: the iOS multitasking b ...
随机推荐
- Android 项目建立步骤
使用eclipse,进行安卓开发,在建立项目的时候,有些步骤必须注意的, 本文就是对使用eclipse进行android开发的简单说明: 一.模拟器配置设定 使用eclipse开发安卓,需要用到and ...
- 失物招领发布-HTML5调摄像头
<!DOCTYPE html> <html lang="zh-cn"> <head> <meta charset="utf-8& ...
- 关于 rem 作为单位设置大小
rem是相对长度单位.相对于根元素(即html元素)font-size计算值的倍数htm{font-size: 62.5%;}根元素(html)先设置一个font-size,一般情况下为了容易计算re ...
- response.setContentType()的作用及参数
package com.java1234.util; import java.io.PrintWriter; import javax.servlet.http.HttpServletResponse ...
- c#中去掉字符串空格方法
(1)Trim方法 string tt=" aaa "; tt=tt.Trim() 去字符串首尾空格的函数 tt=tt.TrimEnd() 去掉字符串尾空格 tt= ...
- ueditor asp.net版本更改图片保存路径
目的:把本地上传的图片放置到跟目录下的Images/Upload文件夹下. 修改步骤: 1.ueditor.config.js文件中的, imagePath: URL + "net/&quo ...
- android - INSTALL_FAILED_MEDIA_UNAVAILABLE
解决方案是将'AndroidManifest.xml'设置 'installLocation'的属性为'auto'即可.
- cxf WebService整理 (基于注解)
http://blog.csdn.net/zjw10wei321/article/details/39889823
- UIScrollView 之图片缩放
UIScrollView 之图片缩放 有些时候,我们可能要对某些内容进行手势缩放,如下图所示 UIScrollView不仅能滚动显示大量内容,还能对其内容进行缩放处理 也就是说,要完成缩放功能的话,只 ...
- ASP.NET菜鸟之路之Response小例子
背景 我是一个ASP.NET菜鸟,暂时开始学习ASP.NET,在此记录下我个人敲的代码,没有多少参考价值,请看到的盆友们为我点个赞支持我一下,多谢了. Response.Write Redirect ...