1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104
| #import "ViewController.h" #define FileBoundary @"Boundary" #define NewLien @"\r\n" #define Encode(str) [str dataUsingEncoding:NSUTF8StringEncoding]
@interface ViewController ()
@end
@implementation ViewController
- (NSString *)MIMEType:(NSURL *)url { // 1.创建一个请求 NSURLRequest *request = [NSURLRequest requestWithURL:url]; // 2.发送请求(返回响应) NSURLResponse *response = nil; [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:nil]; // 3.获得MIMEType return response.MIMEType; }
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { [self upload]; } - (void)upload { // 非文件参数 NSDictionary *params = @{ @"username" : @"李四", @"pwd" : @"123", @"age" : @30, @"height" : @"1.55" }; NSURL *url = [[NSBundle mainBundle] URLForResource:@"autolayout" withExtension:@"txt"]; NSData *data = [NSData dataWithContentsOfURL:url]; NSString *MIMEType = [self MIMEType:url]; [self upload:@"test.txt" mimeType:MIMEType fileData:data params:params]; }
- (void)upload:(NSString *)filename mimeType:(NSString *)mimeType fileData:(NSData *)fileData params:(NSDictionary *)params { // 1.请求路径 NSURL *url = [NSURL URLWithString:@"http://localhost:8080/MJServer/upload"]; // 2.创建一个POST请求 NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url]; request.HTTPMethod = @"POST"; // 3.设置请求体 NSMutableData *body = [NSMutableData data]; // 3.1.文件参数 [body appendData:Encode(@"--")]; [body appendData:Encode(FileBoundary)]; [body appendData:Encode(NewLien)]; NSString *disposition = [NSString stringWithFormat:@"Content-Disposition: form-data; name=\"file\"; filename=\"%@\"", filename]; [body appendData:Encode(disposition)]; [body appendData:Encode(NewLien)]; NSString *type = [NSString stringWithFormat:@"Content-Type: %@", mimeType]; [body appendData:Encode(type)]; [body appendData:Encode(NewLien)]; [body appendData:Encode(NewLien)]; [body appendData:fileData]; [body appendData:Encode(NewLien)]; // 3.2.非文件参数 [params enumerateKeysAndObjectsUsingBlock:^(id key, id obj, BOOL *stop) { [body appendData:Encode(@"--")]; [body appendData:Encode(FileBoundary)]; [body appendData:Encode(NewLien)]; NSString *disposition = [NSString stringWithFormat:@"Content-Disposition: form-data; name=\"%@\"", key]; [body appendData:Encode(disposition)]; [body appendData:Encode(NewLien)]; [body appendData:Encode(NewLien)]; [body appendData:Encode([obj description])]; [body appendData:Encode(NewLien)]; }]; // 3.3.结束标记 [body appendData:Encode(@"--")]; [body appendData:Encode(FileBoundary)]; [body appendData:Encode(@"--")]; [body appendData:Encode(NewLien)]; request.HTTPBody = body; // 4.设置请求头(告诉服务器这次传给你的是文件数据,告诉服务器现在发送的是一个文件上传请求) NSString *contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@", FileBoundary]; [request setValue:contentType forHTTPHeaderField:@"Content-Type"]; // 5.发送请求 [NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) { NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableLeaves error:nil]; NSLog(@"%@", dict); }]; } @end
|