[iOS 多線程 & 網路,ios多線程

來源:互聯網
上載者:User

[iOS 多線程 & 網路,ios多線程
A.檔案上傳思路:傳送檔案資料給伺服器使用post請求必須手動佈建要求頭: 內容大小Content-Length & 內容類型 Content-Type請求體:檔案資料檔案上傳的格式要求十分嚴格,必須嚴格遵守由於是一次性負載檔案到記憶體上傳,所以只能用於小檔案上傳 B.實現1.設定POST請求(1)使用POST要求方法(2)佈建要求頭設定內容長度、內容類型、分割線 (3)佈建要求體NSMutableData *body = [NSMutableData data];分割線 + 換行內容描述 + 換行內容類型 + 換行換行檔案位元據 + 換行分割線--  multipart/form-data 中的內容 1 /** 取得本地檔案的MIMEType */ 2 - (void) getMIMEType { 3 // Socket 實現斷點上傳 4 5 //apache-tomcat-6.0.41/conf/web.xml 尋找 檔案的 mimeType 6 // UIImage *image = [UIImage imageNamed:@"test"]; 7 // NSData *filedata = UIImagePNGRepresentation(image); 8 // [self upload:@"file" filename:@"test.png" mimeType:@"image/png" data:filedata parmas:@{@"username" : @"123"}]; 9 10 // 給本地檔案發送一個請求11 NSURL *fileurl = [[NSBundle mainBundle] URLForResource:@"itcast.txt" withExtension:nil];12 NSURLRequest *request = [NSURLRequest requestWithURL:fileurl];13 NSURLResponse *repsonse = nil;14 NSData *data = [NSURLConnection sendSynchronousRequest:request returningResponse:&repsonse error:nil];15 16 // 得到mimeType17 NSLog(@"%@", repsonse.MIMEType);18 [self upload:@"file" filename:@"itcast.txt" mimeType:repsonse.MIMEType data:data parmas:@{@"username":@"tom", @"type":@"xml"}];19 }  附:常見檔案類型的MIMEType 1 // 2 // ViewController.m 3 // UploadFileDemo 4 // 5 // Created by hellovoidworld on 15/1/28. 6 // Copyright (c) 2015年 hellovoidworld. All rights reserved. 7 // 8 9 #import "ViewController.h" 10 11 #define UTF8Encode(str) [str dataUsingEncoding:NSUTF8StringEncoding] 12 13 @interface ViewController () 14 15 - (IBAction)upload; 16 17 @end 18 19 @implementation ViewController 20 21 - (void)viewDidLoad { 22 [super viewDidLoad]; 23 // Do any additional setup after loading the view, typically from a nib. 24 } 25 26 - (void)didReceiveMemoryWarning { 27 [super didReceiveMemoryWarning]; 28 // Dispose of any resources that can be recreated. 29 } 30 31 - (IBAction)upload { 32 UIImage *image = [UIImage imageNamed:@"IMG_0413"]; 33 NSData *imageData = UIImagePNGRepresentation(image); 34 [self upload:@"uploadedFile" filename:@"IMG_0413.PNG" mimeType:@"image/png" data:imageData parmas:nil]; 35 } 36 37 - (void)upload:(NSString *)name filename:(NSString *)filename mimeType:(NSString *)mimeType data:(NSData *)data parmas:(NSDictionary *)params 38 { 39 // 檔案上傳 40 NSURL *url = [NSURL URLWithString:@"http://192.168.0.21:8080/MyTestServer/upload"]; 41 NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url]; 42 request.HTTPMethod = @"POST"; 43 44 // 佈建要求體 45 NSMutableData *body = [NSMutableData data]; 46 47 /***************檔案參數***************/ 48 // 參數開始的標誌 49 [body appendData:UTF8Encode(@"--HelloVoidWorldBoundary\r\n")]; 50 // name : 指定參數名(必須跟伺服器端保持一致) 51 // filename : 檔案名稱 52 NSString *disposition = [NSString stringWithFormat:@"Content-Disposition: form-data; name=\"%@\"; filename=\"%@\"\r\n", name, filename]; 53 [body appendData:UTF8Encode(disposition)]; 54 NSString *type = [NSString stringWithFormat:@"Content-Type: %@\r\n", mimeType]; 55 [body appendData:UTF8Encode(type)]; 56 57 [body appendData:UTF8Encode(@"\r\n")]; 58 [body appendData:data]; 59 [body appendData:UTF8Encode(@"\r\n")]; 60 61 /***************普通參數***************/ 62 [params enumerateKeysAndObjectsUsingBlock:^(id key, id obj, BOOL *stop) { 63 // 參數開始的標誌 64 [body appendData:UTF8Encode(@"--HelloVoidWorldBoundary\r\n")]; 65 NSString *disposition = [NSString stringWithFormat:@"Content-Disposition: form-data; name=\"%@\"\r\n", key]; 66 [body appendData:UTF8Encode(disposition)]; 67 68 [body appendData:UTF8Encode(@"\r\n")]; 69 [body appendData:UTF8Encode(obj)]; 70 [body appendData:UTF8Encode(@"\r\n")]; 71 }]; 72 73 /***************參數結束***************/ 74 // HelloVoidWorldBoundary--\r\n 75 [body appendData:UTF8Encode(@"--HelloVoidWorldBoundary--\r\n")]; 76 request.HTTPBody = body; 77 78 // 佈建要求頭 79 // 請求體的長度 80 [request setValue:[NSString stringWithFormat:@"%zd", body.length] forHTTPHeaderField:@"Content-Length"]; 81 // 聲明這個POST請求是個檔案上傳 82 [request setValue:@"multipart/form-data; boundary=HelloVoidWorldBoundary" forHTTPHeaderField:@"Content-Type"]; 83 84 // 發送請求 85 [NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) { 86 NSLog(@"開始上傳~~~"); 87 if (data) { 88 NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableLeaves error:nil]; 89 NSLog(@"%@", dict); 90 } else { 91 NSLog(@"上傳失敗"); 92 } 93 }]; 94 } 95 96 /** 取得本地檔案的MIMEType */ 97 - (void) getMIMEType { 98 // Socket 實現斷點上傳 99 100 //apache-tomcat-6.0.41/conf/web.xml 尋找 檔案的 mimeType101 // UIImage *image = [UIImage imageNamed:@"test"];102 // NSData *filedata = UIImagePNGRepresentation(image);103 // [self upload:@"file" filename:@"test.png" mimeType:@"image/png" data:filedata parmas:@{@"username" : @"123"}];104 105 // 給本地檔案發送一個請求106 NSURL *fileurl = [[NSBundle mainBundle] URLForResource:@"itcast.txt" withExtension:nil];107 NSURLRequest *request = [NSURLRequest requestWithURL:fileurl];108 NSURLResponse *repsonse = nil;109 NSData *data = [NSURLConnection sendSynchronousRequest:request returningResponse:&repsonse error:nil];110 111 // 得到mimeType112 NSLog(@"%@", repsonse.MIMEType);113 [self upload:@"file" filename:@"itcast.txt" mimeType:repsonse.MIMEType data:data parmas:@{@"username":@"tom", @"type":@"xml"}];114 }115 116 117 @end    

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.