@interface Hmviewcontroller () <nsurlconnectiondatadelegate>
/**
* file handle object for writing data
*/
@property (nonatomic, strong) nsfilehandle *writehandle;
/**
* Total size of the file
*/
@property (nonatomic, assign) long long totallength;
/**
* file size that is currently written
*/
@property (nonatomic, assign) long long currentlength;
@property (nonatomic, weak) Dacircularprogressview *circleview;
@end
@implementation Hmviewcontroller
-(void) viewdidload
{
[Super viewdidload];
additional setup after loading the view, typically from a nib.
dacircularprogressview *circleview = [[dacircularprogressview alloc] init];
Circleview. Frame = CGRectMake (+, +, + );
Circleview. Progresstintcolor = [uicolor redcolor];
Circleview. Tracktintcolor = [uicolor bluecolor];
Circleview. Progress = 0.01;
[self. View addsubview: Circleview];
self. Circleview = Circleview;
}
-(void) Touchesbegan: (nsset *) touches withevent: (uievent *) event
{
//1.URL
nsurl *url = [nsurl urlwithstring:@ "http://localhost:8080/MJServer/resources/ Music.zip "];
//2. Request
nsurlrequest *request = [nsurlrequest requestwithurl: url];
//3. Download ( An asynchronous request is automatically initiated when the Conn object is created )
[nsurlconnection connectionwithrequest: Request delegate:self];
}
#pragma mark-nsurlconnectiondatadelegate Proxy method
/**
* call when request fails (Request timeout, network exception)
*
* @param Error cause
*/
-(void) connection: (nsurlconnection *) connection didfailwitherror: (nserror *) error
{
NSLog(@ "Didfailwitherror");
}
/**
* 1. the response received from the server will be called
*
* @param response Response
*/
-(void) connection: (nsurlconnection *) connection didreceiveresponse: (nsurlresponse *) Response
{
// file path
nsstring *caches = [nssearchpathfordirectoriesindomains(nscachesdirectory, Nsuserdomainmask, YES) lastobject];
nsstring *filepath = [caches stringbyappendingpathcomponent:@ "Videos.zip"];
// Create an empty file into the sandbox
nsfilemanager *mgr = [nsfilemanager defaultmanager];
[Mgr createfileatpath: filepath contents:nil attributes:nil];
// Create a file handle to write data
self. Writehandle = [nsfilehandle filehandleforwritingatpath: filepath];
// Get the total size of the file
self. Totallength = response. Expectedcontentlength;
}
/**
* 2. called when the Entity data returned by the server is received (specific content, this method may be called multiple times)
*
* @param data returned at this time
*/
-(void) connection: (nsurlconnection *) connection didreceivedata: (nsdata *) data
{
// move to the last side of the file
[self. Writehandle seektoendoffile];
// write data to sandbox
[self. Writehandle writedata:d ATA];
// The length of the cumulative file
self. Currentlength + = data. Length;
nslog (@ " download progress: %f " (doubleself. Currentlength/self. Totallength
self. Circleview. Progress = (double)self. Currentlength/ self. Totallength;
}
/**
* 3. called after loading (the server's data is fully returned)
*/
-(void) connectiondidfinishloading: (nsurlconnection *) connection
{
self. Currentlength = 0;
self. Totallength = 0;
// close file
[self. Writehandle closefile];
self. Writehandle = nil;
}
@end
A:nsfilehandle can only open ready-made files, so if it is a new file, you need to Nsfilemanager create a new file first;
B: After opening a file, you need to close a file;
C: Depending on the requirements may be set different offsets (that is, the position of the cursor), you can move to the beginning or end, you can first get the current position and then increase and decrease the number of bytes to move;
D: File path is NSString object, you can construct a complete file path with stringbyappendingpathcomponent+ file name method;
E: The content written in the file is NSData, if it is other format can be converted to the data type datausingencoding:nsutf8stringencoding;
F: Similarly, the directory is written similar to Linux, ~ home directory,/root directory,. Current directory,. Parent directories, using/representing hierarchies, and so on.
(1) Nsfilemanager (see: http://blog.csdn.net/enuola/article/details/7797055)
Large File Download