网络:NSOutputStream的使用
来源:互联网 发布:西门子触摸屏编程手册 编辑:程序博客网 时间:2024/05/18 00:47
#import "ViewController.h"@interface ViewController ()<NSURLConnectionDataDelegate>@property (nonatomic, assign) long long fileSize; // 文件总大小@property (nonatomic, assign) long long currentSize; // 当前接收的文件大小@property (nonatomic, strong) NSOutputStream *output; // 文件输出流@end@implementation ViewController- (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view, typically from a nib.// [self serverFileSize];}// 我们在使用别人的软件的时候,点击下载会怎么样?// 提示这个文件是多大,是否下载- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event { [self serverFileSize];}// HEAD用来请求查看文件大小- (void)serverFileSize { // NSURL NSString *URLStr =@"http://localhost/01UI基础复习.mp4"; // 百分号转码 URLStr = [URLStr stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]; NSURL *url = [NSURL URLWithString:URLStr]; // NSURLRequest 获取文件大小,不是使用GET,而是使用HEAD NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url]; [request setHTTPMethod:@"HEAD"]; NSURLResponse *response; // 获取文件大小,是使用同步 [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:NULL]; // 文件总大小// NSLog(@"%@",response); self.fileSize = response.expectedContentLength;// NSLog(@"%lld",fileSize); // 提示用户文件总大下,是否需要下载 // 下载文件 [self download:url];}- (void)download:(NSURL *)url { // NSURLRequest 下载文件,从服务器获取的意思 GET NSURLRequest *request = [NSURLRequest requestWithURL:url]; // 开始下载文件, 知道下载的进度 [NSURLConnection connectionWithRequest:request delegate:self];}#pragma mark - NSURLConnection 代理/** NSFileHandle 选择写入文件的方式初始化,在写入文件之前先把光标移动文件的最后,写完之后关闭 NSOutputStream 初始化的时候选择拼接文件,再打开流,写入数据(多次),关闭流 */// 接收到响应- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response { NSLog(@"接收到响应%@ -- %lld",response,response.expectedContentLength);// NSHTTPURLResponse *httpResp// self.fileSize = response.expectedContentLength; // 文件总大小 NSString *path = [[NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) lastObject] stringByAppendingPathComponent:@"123.mp4"]; self.output = [[NSOutputStream alloc]initToFileAtPath:path append:YES]; // 在写入编辑文件之前,打开文件 [self.output open];}- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {// NSLog(@"接收到数据 %zd",data.length); // 如果需要知道进度,首要要知道文件的总大小,还要接收了多少 self.currentSize += data.length; NSLog(@"%f",(CGFloat)self.currentSize / self.fileSize); // uunt8_t -> NSData// [NSData dataWithBytes:<#(nullable const void *)#> length:<#(NSUInteger)#>] [self.output write:data.bytes maxLength:data.length];}- (void)connectionDidFinishLoading:(NSURLConnection *)connection { NSLog(@"下载完成了"); // 关闭文件流 [self.output close];}- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error { NSLog(@"出错了");}@end
0 0
- 网络:NSOutputStream的使用
- Cocoa Stream 系列3--NSOutputStream的使用
- 依靠NSOutputStream,NSURLSession,NSURLSessionDataTask创建一个支持断点下载的任务
- NStream、NSOutputStream、NSInputStream
- NSInputStream 和 NSOutputStream
- 通过NSOutputstream读写文件
- NSOutputStream NSInputStream 简单用法
- 关于数据流NSOutputStream
- ios-NSFileHandle和NSOutputStream
- NSOutputStream-保存网络资源到本地
- 网络硬盘盒的使用
- 网络存储空间的使用
- 网络+UIWebView的使用
- 网络协议的使用
- 网络:GET 的使用
- 网络:HEAD 的使用
- 网络:NSFIleHandle的使用
- 网络命令的使用
- C语言中的找特殊数字问题
- 魅族usb调试
- HDU 5371 Manacher
- 网络:NSFIleHandle的使用
- CG 知识摘要
- 网络:NSOutputStream的使用
- Spring切入点表达式常用写法
- JIRA环境搭建
- Spring多数据源分布式事务管理/springmvc+spring+atomikos[jta]+druid+mybatis
- linux系统启动报错:[contains a file system with errors, check forced]的解决方法参考
- [转]使用web.config中的httpHandlers
- Maven报错:Error assembling WAR: webxml attribute is required (or pre-existing WEB-INF/web.xml
- 使用volley读取压缩文件的内容(zip、gz)
- 语法错误 : 缺少“;”(在“类型”的前面)