1、iOS裏面實現斷點續傳php
#pragma mark - 下載文件 - (IBAction)downloadFiles:(id)sender { // 1. 指定下載文件地址 NSURL *url = [NSURL URLWithString:@"http://169.254.98.245/download/iTunesConnect_DeveloperGuide_CN.zip"]; // 2. 指定文件保存路徑 NSArray *documents = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); NSString *downloadPath = [documents[0]stringByAppendingPathComponent:@"book.zip"]; // 3. 建立NSURLRequest NSURLRequest *request = [NSURLRequest requestWithURL:url]; // 4. 建立AFURLConnectionOperation AFURLConnectionOperation *operation = [[AFHTTPRequestOperation alloc]initWithRequest:request]; // 5. 設置操做的輸出流(在網絡中的數據是以流的方式傳輸的,告訴操做把文件保存在第2步設置的路徑中) [operation setOutputStream:[NSOutputStream outputStreamToFileAtPath:downloadPath append:NO]]; // 6. 設置下載進程處理塊代碼 // 6.1 bytesRead 讀取的字節——這一次下載的字節數 // 6.2 totalBytesRead 讀取的總字節——已經下載完的 // 6.3 totalBytesExpectedToRead 但願讀取的總字節——就是文件的總大小 [operation setDownloadProgressBlock:^(NSUInteger bytesRead, long long totalBytesRead, long long totalBytesExpectedToRead) { // 作下載進度百分比的工做 NSLog(@"下載百分比:%f", (float)totalBytesRead / totalBytesExpectedToRead); }]; // 7. 操做完成塊代碼 [operation setCompletionBlock:^{ // 解壓縮的順序 // 1. 定義要解壓縮的文件 —— downloadPath // 2. 要解壓縮的目標目錄 // 3. 調用類方法解壓縮 [SSZipArchive unzipFileAtPath:downloadPath toDestination:documents[0]]; // 刪除壓縮包 [[NSFileManager defaultManager]removeItemAtPath:downloadPath error:nil]; }]; // 8 啓動操做 [operation start]; }
// // ViewController.m // AFN框架 // // Created by apple on 14-6-21. // Copyright (c) 2014年 Early. All rights reserved. // #import "ViewController.h" #import "AFNetworking.h" @interface ViewController () <NSXMLParserDelegate> { NSOperationQueue *_queue; UIImageView *_imageView; } @end @implementation ViewController /** 1.在Linux系統上,運行的Web服務器叫作Apache 2.全部的http訪問都是基於html或者相關的文件,例如:php,asp,jsp,asp.net 3.form,在html頁面中又稱表單,用來提交頁面的,全部post請求的頁面,至少會有一個表單 4.get/post,get是拿數據,post是將數據體放置在表單中提交給服務器,而後服務器再響應 */ - (void)viewDidLoad { [super viewDidLoad]; _queue = [[NSOperationQueue alloc] init]; //1.檢測鏈接狀態 UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom]; btn.frame = CGRectMake(100, 100, 100, 50); [btn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal]; [btn setTitle:@"連接狀態" forState:UIControlStateNormal]; [btn addTarget:self action:@selector(reach) forControlEvents:UIControlEventTouchUpInside]; [self.view addSubview:btn]; //2.JSON解析 UIButton *btn2 = [UIButton buttonWithType:UIButtonTypeCustom]; btn2.frame = CGRectMake(100, 150, 100, 50); [btn2 setTitleColor:[UIColor blackColor] forState:UIControlStateNormal]; [btn2 setTitle:@"加載JSON" forState:UIControlStateNormal]; [btn2 addTarget:self action:@selector(loadJSON) forControlEvents:UIControlEventTouchUpInside]; [self.view addSubview:btn2]; //3.XML解析 UIButton *btn3 = [UIButton buttonWithType:UIButtonTypeCustom]; btn3.frame = CGRectMake(100, 200, 100, 50); [btn3 setTitleColor:[UIColor blackColor] forState:UIControlStateNormal]; [btn3 setTitle:@"加載XML" forState:UIControlStateNormal]; [btn3 addTarget:self action:@selector(loadXML) forControlEvents:UIControlEventTouchUpInside]; [self.view addSubview:btn3]; //4.UIImageView UIButton *btn4 = [UIButton buttonWithType:UIButtonTypeCustom]; btn4.frame = CGRectMake(100, 250, 100, 50); [btn4 setTitleColor:[UIColor blackColor] forState:UIControlStateNormal]; [btn4 setTitle:@"加載UIImageView" forState:UIControlStateNormal]; [btn4 addTarget:self action:@selector(loadImageView) forControlEvents:UIControlEventTouchUpInside]; [self.view addSubview:btn4]; //5.上傳文件 UIButton *btn5 = [UIButton buttonWithType:UIButtonTypeCustom]; btn5.frame = CGRectMake(100, 300, 100, 50); [btn5 setTitleColor:[UIColor blackColor] forState:UIControlStateNormal]; [btn5 setTitle:@"上傳圖像" forState:UIControlStateNormal]; [btn5 addTarget:self action:@selector(postImage) forControlEvents:UIControlEventTouchUpInside]; [self.view addSubview:btn5]; //6.斷點續傳 } #pragma mark - 上傳圖像 - (void)postImage { //1.定義HttpClient NSURL *url = [NSURL URLWithString:@"地址"]; AFHTTPClient *httpClient = [AFHTTPClient clientWithBaseURL:url]; //baseUrl後邊的請求都基於這個地址 //2.根據HttpClient生成一個post請求 NSMutableURLRequest *request = [httpClient multipartFormRequestWithMethod:@"POST" path:@"後續地址" parameters:nil constructingBodyWithBlock:^(id<AFMultipartFormData> formData) { //formData就是上傳的數據 NSData *data = UIImagePNGRepresentation(_imageView.image); /** 1.要上傳文件的參數 2.負責上傳文件的遠處服務中接受文件使用的參數 3.要上傳文件的文件名(別人告知) 4.要上傳文件的文件類型(別人告知) */ [formData appendPartWithFileData:data name:@"file" fileName:@"upload001.png" mimeType:@"image/png"]; }]; //3.準備作上傳的操做 AFHTTPRequestOperation *op = [[AFHTTPRequestOperation alloc] initWithRequest:request]; [op setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) { NSLog(@"上傳文件成功"); } failure:^(AFHTTPRequestOperation *operation, NSError *error) { NSLog(@"上傳文件失敗"); }]; [op start]; } #pragma mark - 加載UIImageView //若是異步在家表格圖像,不建議使用 - (void)loadImageView { //1.建立一個UIImageView _imageView = [[UIImageView alloc] initWithFrame:CGRectMake(100, 0, 50, 50)]; [self.view addSubview:_imageView]; //2.URL NSURL *url = [NSURL URLWithString:@"圖片地址"]; [_imageView setImageWithURL:url placeholderImage:[UIImage imageNamed:@""]]; } #pragma mark - 加載XML - (void)loadXML { //1.URL NSURL *url = [NSURL URLWithString:@"地址"]; //2.Request NSURLRequest *request = [NSURLRequest requestWithURL:url]; //3.Connection(默認在主隊列,能夠新建一個隊列放入,不影響主界面) AFXMLRequestOperation *op = [AFXMLRequestOperation XMLParserRequestOperationWithRequest:request success:^(NSURLRequest *request, NSHTTPURLResponse *response, NSXMLParser *XMLParser) { NSLog(@"加載XML成功"); //1.實例化解析器,並傳入數據 AFN已經作了 XMLParser //2.設置代理 [XMLParser setDelegate:self]; //3.解析器解析 [XMLParser parse]; } failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error, NSXMLParser *XMLParser) { NSLog(@"加載XML失敗"); }]; [_queue addOperation:op]; } #pragma mark - XML解析器代理方法(6個) - (void)parserDidEndDocument:(NSXMLParser *)parser { } #pragma mark - 加載JSON - (void)loadJSON { //1.URL NSURL *url = [NSURL URLWithString:@"地址"]; //2.Request NSURLRequest *request = [NSURLRequest requestWithURL:url]; //3.Connection(默認在主隊列,能夠新建一個隊列放入,不影響主界面) AFJSONRequestOperation *op = [AFJSONRequestOperation JSONRequestOperationWithRequest:request success:^(NSURLRequest *request, NSHTTPURLResponse *response, id JSON) { NSLog(@"成功加載JSON"); } failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error, id JSON) { NSLog(@"失敗加載JSON"); }]; [op start]; } #pragma mark - 檢測鏈接狀態 - (void)reach { //連接狀態的檢測 NSURL *url = [NSURL URLWithString:@"http://www.baidu.com"]; AFHTTPClient *httpClient = [AFHTTPClient clientWithBaseURL:url]; [httpClient setReachabilityStatusChangeBlock:^(AFNetworkReachabilityStatus status) { switch (status) { case AFNetworkReachabilityStatusNotReachable: NSLog(@"無連接"); break; case AFNetworkReachabilityStatusReachableViaWiFi: NSLog(@"Wifi"); break; case AFNetworkReachabilityStatusReachableViaWWAN: NSLog(@"3G連接"); break; default: NSLog(@"連接狀態未知"); break; } }]; } @end