iOS開發網絡篇—文件的上傳web
說明:文件上傳使用的時POST請求,一般把要上傳的數據保存在請求體中。本文介紹如何不借助第三方框架實現iOS開發中得文件上傳。apache
因爲過程較爲複雜,所以本文只貼出部分關鍵代碼。tomcat
主控制器的關鍵代碼:服務器
YYViewController.m網絡
1 #import "YYViewController.h" 2 3 #define YYEncode(str) [str dataUsingEncoding:NSUTF8StringEncoding] 4 5 @interface YYViewController () 6 7 @end 8 9 @implementation YYViewController 10 11 - (void)viewDidLoad 12 { 13 [super viewDidLoad]; 14 // Do any additional setup after loading the view, typically from a nib. 15 } 16 17 - (void)upload:(NSString *)name filename:(NSString *)filename mimeType:(NSString *)mimeType data:(NSData *)data parmas:(NSDictionary *)params 18 { 19 // 文件上傳 20 NSURL *url = [NSURL URLWithString:@"http://192.168.1.200:8080/YYServer/upload"]; 21 NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url]; 22 request.HTTPMethod = @"POST"; 23 24 // 設置請求體 25 NSMutableData *body = [NSMutableData data]; 26 27 /***************文件參數***************/ 28 // 參數開始的標誌 29 [body appendData:YYEncode(@"--YY\r\n")]; 30 // name : 指定參數名(必須跟服務器端保持一致) 31 // filename : 文件名 32 NSString *disposition = [NSString stringWithFormat:@"Content-Disposition: form-data; name=\"%@\"; filename=\"%@\"\r\n", name, filename]; 33 [body appendData:YYEncode(disposition)]; 34 NSString *type = [NSString stringWithFormat:@"Content-Type: %@\r\n", mimeType]; 35 [body appendData:YYEncode(type)]; 36 37 [body appendData:YYEncode(@"\r\n")]; 38 [body appendData:data]; 39 [body appendData:YYEncode(@"\r\n")]; 40 41 /***************普通參數***************/ 42 [params enumerateKeysAndObjectsUsingBlock:^(id key, id obj, BOOL *stop) { 43 // 參數開始的標誌 44 [body appendData:YYEncode(@"--YY\r\n")]; 45 NSString *disposition = [NSString stringWithFormat:@"Content-Disposition: form-data; name=\"%@\"\r\n", key]; 46 [body appendData:YYEncode(disposition)]; 47 48 [body appendData:YYEncode(@"\r\n")]; 49 [body appendData:YYEncode(obj)]; 50 [body appendData:YYEncode(@"\r\n")]; 51 }]; 52 53 /***************參數結束***************/ 54 // YY--\r\n 55 [body appendData:YYEncode(@"--YY--\r\n")]; 56 request.HTTPBody = body; 57 58 // 設置請求頭 59 // 請求體的長度 60 [request setValue:[NSString stringWithFormat:@"%zd", body.length] forHTTPHeaderField:@"Content-Length"]; 61 // 聲明這個POST請求是個文件上傳 62 [request setValue:@"multipart/form-data; boundary=YY" forHTTPHeaderField:@"Content-Type"]; 63 64 // 發送請求 65 [NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) { 66 if (data) { 67 NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableLeaves error:nil]; 68 NSLog(@"%@", dict); 69 } else { 70 NSLog(@"上傳失敗"); 71 } 72 }]; 73 } 74 75 - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event 76 { 77 // Socket 實現斷點上傳 78 79 //apache-tomcat-6.0.41/conf/web.xml 查找 文件的 mimeType 80 // UIImage *image = [UIImage imageNamed:@"test"]; 81 // NSData *filedata = UIImagePNGRepresentation(image); 82 // [self upload:@"file" filename:@"test.png" mimeType:@"image/png" data:filedata parmas:@{@"username" : @"123"}]; 83 84 // 給本地文件發送一個請求 85 NSURL *fileurl = [[NSBundle mainBundle] URLForResource:@"itcast.txt" withExtension:nil]; 86 NSURLRequest *request = [NSURLRequest requestWithURL:fileurl]; 87 NSURLResponse *repsonse = nil; 88 NSData *data = [NSURLConnection sendSynchronousRequest:request returningResponse:&repsonse error:nil]; 89 90 // 獲得mimeType 91 NSLog(@"%@", repsonse.MIMEType); 92 [self upload:@"file" filename:@"itcast.txt" mimeType:repsonse.MIMEType data:data parmas:@{ 93 @"username" : @"999", 94 @"type" : @"XML"}]; 95 } 96 97 @end
補充說明:app
文件上傳請求數據格式框架
部分文件的MIMETypeurl