【iOS开发-98】文件分段下载:@“HEAD“和[request setValue:range forHTTPHeaderField:@"Range"]

(1)文件下载,一般的下载可以直接下载。而针对视频或者下载软件等下载需要分段下载。以下就是模拟的分段下载,即利用request设置Range,这个Range就是一个“bytes=1-1024”之类规定请求或者说下载的范围。


(2)利用NSFileHandle控制文件写入。


(3)只请求头文件HEAD,可以获取信息而不下载文件。


#import "ViewController.h"

#define kSizePerTime 50000

@interface ViewController ()
@property (weak, nonatomic) IBOutlet UIImageView *imgView;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    NSURL *url=[[NSBundle mainBundle]URLForResource:@"test.jpg" withExtension:nil];
    //1、获取文件大小
    long long fizeSize=[self fileSizeWithURL:url];
    //比较本地是否有文件
    long long localfile=[self localFileSize];
    if (localfile==fizeSize) {
        NSLog(@"文件已存在");
        return;
    }
    //2、确定数据包大小
    long long FromB=0;
    long long ToB=0;
    while (fizeSize>kSizePerTime) {
        ToB=FromB+kSizePerTime-1;
        //3、下载数据包
        [self downloadWithURL:url FromB:FromB ToB:ToB];
        fizeSize-=kSizePerTime;
        FromB+=kSizePerTime;
    }
    ToB=FromB+fizeSize-1;
    [self downloadWithURL:url FromB:FromB ToB:ToB];
    NSString *path=[[NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) lastObject]stringByAppendingPathComponent:@"1.jpg"];
    self.imgView.image=[UIImage imageWithContentsOfFile:path];
}

-(long long)fileSizeWithURL:(NSURL *)url{
    NSMutableURLRequest *request=[NSMutableURLRequest requestWithURL:url cachePolicy:NSURLRequestReloadIgnoringCacheData timeoutInterval:2.0f];
    request.HTTPMethod=@"HEAD";
    NSURLResponse *response=nil;
    [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:NULL];
    return response.expectedContentLength;
}

-(long long)localFileSize{
    NSString *path=[[NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) lastObject]stringByAppendingPathComponent:@"1.jpg"];
    NSDictionary *dict=[[NSFileManager defaultManager]attributesOfItemAtPath:path error:NULL];
    return [dict[NSFileSize] longLongValue];
}

-(void)downloadWithURL:(NSURL *)url FromB:(long long)FromB ToB:(long long)ToB{
    NSMutableURLRequest *request=[NSMutableURLRequest requestWithURL:url cachePolicy:NSURLRequestReloadIgnoringCacheData timeoutInterval:2.0f];
    NSString *range=[NSString stringWithFormat:@"Bytes=%lld-%lld",FromB,ToB];
    [request setValue:range forHTTPHeaderField:@"Range"];
    NSURLResponse *response=nil;
    NSData *data=[NSURLConnection sendSynchronousRequest:request returningResponse:&response error:NULL];
    
    //写入文件
    [self appendData:data];
    NSLog(@"%@",response);
}

-(void)appendData:(NSData *)data{
    NSString *path=[[NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) lastObject]stringByAppendingPathComponent:@"1.jpg"];
    NSFileHandle *fh=[NSFileHandle fileHandleForWritingAtPath:path];
    if (!fh) {
        [data writeToFile:path atomically:YES];
    }else{
        [fh seekToEndOfFile];
        [fh writeData:data];
        [fh closeFile];
    }
}

@end


郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。