(1) file download, the general download can be downloaded directly. Download for video or download software needs to be downloaded in sections. The following is a staged download of the simulation, that is, using the request setting range, this range is a "bytes=1-1024" or the scope of the request or download.
(2) write with Nsfilehandle control file.
(3) Request header file head only, can obtain information without downloading the file.
#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. Get file size long long fizesize=[self filesizewithurl:url]; Compare local whether there are files long long localfile=[self localfilesize]; if (localfile==fizesize) {NSLog (@ "file already exists"); Return }//2, determine packet size long long fromb=0; Long Long tob=0; while (fizesize>ksizepertime) {tob=fromb+ksizepertime-1; 3. Download the packet [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]; [email protected] "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]; Write file [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
"iOS Dev-98" File download: @ "HEAD" and [Request Setvalue:range forhttpheaderfield:@ "range"]