网络:HEAD 的使用

#import "ViewController.h"

@interface ViewController ()<NSURLConnectionDataDelegate>
@property (nonatomic, assign) long long fileSize; // 文件总大小
@property (nonatomic, assign) long long currentSize; // 当前接收的文件大小
@property (nonatomic, strong) NSMutableData *dataM;// 拼接下载的二进制数据

@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 代理

// 接收到响应
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response {
    NSLog(@"接收到响应%@ -- %lld",response,response.expectedContentLength);
//    NSHTTPURLResponse *httpResp
//    self.fileSize = response.expectedContentLength; // 文件总大小

}

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
//    NSLog(@"接收到数据 %zd",data.length);
    // 如果需要知道进度,首要要知道文件的总大小,还要接收了多少
    self.currentSize += data.length;
    // 把每次下载的数据保存起来
    [self.dataM appendData:data];
    NSLog(@"%f",(CGFloat)self.currentSize / self.fileSize);
}

- (void)connectionDidFinishLoading:(NSURLConnection *)connection {
    NSLog(@"下载完成了");
    // 把文件写入到沙盒
    NSString *path = [[NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) lastObject] stringByAppendingPathComponent:@"123.mp4"];
    NSLog(@"%@",path);
    // 写入数据
    [self.dataM writeToFile:path atomically:YES];
    // 清空数据, 只是在文件下载完之后,清空了一部份内存。但是还是存在内存锋值,也会造成程序闪退
    self.dataM = nil;

}

- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error {
    NSLog(@"出错了");
}

- (NSMutableData *)dataM {

    if (_dataM == nil) {
        _dataM = [NSMutableData data];
    }
    return _dataM;
}


@end
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值