标签:
一、文件上传的步骤
1.设置请求头
* 目的:告诉服务器请求体里面的内容并非普通的参数,而是包含了文件参数
[request setValue:@"multipart/form-data; boundary=iCocos" forHTTPHeaderField:@"Content-Type"];
2.设置请求体
* 作用:存放参数(文件参数和非文件参数)
1> 非文件参数
[body appendData:iCocosEncode(@"--iCocos\r\n")];
[body appendData:iCocosEncode(@"Content-Disposition: form-data; name=\"username\"\r\n")];
[body appendData:iCocosEncode(@"\r\n")];
[body appendData:iCocosEncode(@"张三")];
[body appendData:iCocosEncode(@"\r\n")];
2> 文件参数
[body appendData:iCocosEncode(@"--iCocos\r\n")];
[body appendData:iCocosEncode(@"Content-Disposition: form-data; name=\"file\"; filename=\"test123.png\"\r\n")];
[body appendData:iCocosEncode(@"Content-Type: image/png\r\n")];
[body appendData:iCocosEncode(@"\r\n")];
[body appendData:imageData];
[body appendData:iCocosEncode(@"\r\n")];
3> 结束标记 :参数结束的标记
[body appendData:iCocosEncode(@"--iCocos--\r\n")];
二、文件的MIMEType
1.百度搜索
2.查找服务器下面的某个xml文件
apache-tomcat-6.0.41\conf\web.xml
3.加载文件时通过Reponse获得
- (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;
}
4.通过C语言函数
+ (NSString *)mimeTypeForFileAtPath:(NSString *)path
{
if (![[NSFileManager alloc] init] fileExistsAtPath:path]) {
return nil;
}
CFStringRef UTI = UTTypeCreatePreferredIdentifierForTag(kUTTagClassFilenameExtension, (CFStringRef)[path pathExtension], NULL);
CFStringRef MIMEType = UTTypeCopyPreferredTagWithClass (UTI, kUTTagClassMIMEType);
CFRelease(UTI);
if (!MIMEType) {
return @"application/octet-stream";
}
return NSMakeCollectable(MIMEType);
}
文件上传一:
1 /* 2 参数1 3 参数2 4 结束标记 5 */ 6 7 /* 8 * 文件参数 9 iCocos 10 Content-Disposition: form-data; name="参数名"; filename="文件名" 11 Content-Type: 文件类型(MIMEType) 12 13 文件具体数据 14 15 * 非文件参数 16 iCocos 17 Content-Disposition: form-data; name="参数名" 18 19 参数值 20 */ 21 22 - (void)upload 23 { 24 // 1.请求路径 25 NSURL *url = [NSURL URLWithString:@"http://192.168.15.172:8080/MJServer/upload"]; 26 27 // 2.创建一个POST请求 28 NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url]; 29 request.HTTPMethod = @"POST"; 30 31 // 3.设置请求体 32 NSMutableData *body = [NSMutableData data]; 33 34 // 3.1.文件参数 35 [body appendData:iCocosEncode(@"--")]; 36 [body appendData:iCocosEncode(iCocosFileBoundary)]; 37 [body appendData:iCocosEncode(iCocosNewLien)]; 38 39 [body appendData:iCocosEncode(@"Content-Disposition: form-data; name=\"file\"; filename=\"test123.png\"")]; 40 [body appendData:iCocosEncode(iCocosNewLien)]; 41 42 [body appendData:iCocosEncode(@"Content-Type: image/png")]; 43 [body appendData:iCocosEncode(iCocosNewLien)]; 44 45 [body appendData:iCocosEncode(iCocosNewLien)]; 46 UIImage *image = [UIImage imageNamed:@"minion_03"]; 47 NSData *imageData = UIImagePNGRepresentation(image); 48 [body appendData:imageData]; 49 [body appendData:iCocosEncode(iCocosNewLien)]; 50 51 // 3.2.用户名参数 52 [body appendData:iCocosEncode(@"--")]; 53 [body appendData:iCocosEncode(iCocosFileBoundary)]; 54 [body appendData:iCocosEncode(iCocosNewLien)]; 55 56 [body appendData:iCocosEncode(@"Content-Disposition: form-data; name=\"username\"")]; 57 [body appendData:iCocosEncode(iCocosNewLien)]; 58 59 [body appendData:iCocosEncode(iCocosNewLien)]; 60 [body appendData:iCocosEncode(@"张三")]; 61 [body appendData:iCocosEncode(iCocosNewLien)]; 62 63 // 3.3.结束标记 64 [body appendData:iCocosEncode(@"--")]; 65 [body appendData:iCocosEncode(iCocosFileBoundary)]; 66 [body appendData:iCocosEncode(@"--")]; 67 [body appendData:iCocosEncode(iCocosNewLien)]; 68 69 request.HTTPBody = body; 70 71 // 4.设置请求头(告诉服务器这次传给你的是文件数据,告诉服务器现在发送的是一个文件上传请求) 72 NSString *contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@", iCocosFileBoundary]; 73 [request setValue:contentType forHTTPHeaderField:@"Content-Type"]; 74 75 // 5.发送请求 76 [NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) { 77 NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableLeaves error:nil]; 78 NSLog(@"%@", dict); 79 }]; 80 }
文件上传二
1 - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event 2 { 3 [self upload]; 4 // NSURL *url = [[NSBundle mainBundle] URLForResource:@"autolayout" withExtension:@"txt"]; 5 6 // NSURL *url = [NSURL fileURLWithPath:@"/Users/apple/Desktop/课堂共享/第一次考试/考试说明.docx"]; 7 // NSLog(@"%@", url); 8 // NSLog(@"%@", [self MIMEType:url]); 9 } 10 11 /* 12 参数1 13 参数2 14 结束标记 15 */ 16 17 /* 18 * 文件参数 19 iCocos 20 Content-Disposition: form-data; name="参数名"; filename="文件名" 21 Content-Type: 文件类型(MIMEType) 22 23 文件具体数据 24 25 * 非文件参数 26 iCocos 27 Content-Disposition: form-data; name="参数名" 28 29 参数值 30 */ 31 32 - (NSString *)MIMEType:(NSURL *)url 33 { 34 // 1.创建一个请求 35 NSURLRequest *request = [NSURLRequest requestWithURL:url]; 36 // 2.发送请求(返回响应) 37 NSURLResponse *response = nil; 38 [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:nil]; 39 // 3.获得MIMEType 40 return response.MIMEType; 41 } 42 43 44 - (void)upload 45 { 46 // 非文件参数 47 NSDictionary *params = @{ 48 @"username" : @"李四", 49 @"pwd" : @"123", 50 @"age" : @30, 51 @"height" : @"1.55" 52 }; 53 NSURL *url = [[NSBundle mainBundle] URLForResource:@"autolayout" withExtension:@"txt"]; 54 NSData *data = [NSData dataWithContentsOfURL:url]; 55 NSString *MIMEType = [self MIMEType:url]; 56 [self upload:@"test.txt" mimeType:MIMEType fileData:data params:params]; 57 58 // 文件数据 59 // UIImage *image = [UIImage imageNamed:@"minion_03"]; 60 // NSData *imageData = UIImagePNGRepresentation(image); 61 // 62 // [self upload:@"777.png" mimeType:@"image/png" fileData:imageData params:params]; 63 } 64 65 - (void)upload:(NSString *)filename mimeType:(NSString *)mimeType fileData:(NSData *)fileData params:(NSDictionary *)params 66 { 67 // 1.请求路径 68 NSURL *url = [NSURL URLWithString:@"http://192.168.15.172:8080/MJServer/upload"]; 69 70 // 2.创建一个POST请求 71 NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url]; 72 request.HTTPMethod = @"POST"; 73 74 // 3.设置请求体 75 NSMutableData *body = [NSMutableData data]; 76 77 // 3.1.文件参数 78 [body appendData:iCocosEncode(@"--")]; 79 [body appendData:iCocosEncode(iCocosFileBoundary)]; 80 [body appendData:iCocosEncode(iCocosNewLien)]; 81 82 NSString *disposition = [NSString stringWithFormat:@"Content-Disposition: form-data; name=\"file\"; filename=\"%@\"", filename]; 83 [body appendData:iCocosEncode(disposition)]; 84 [body appendData:iCocosEncode(iCocosNewLien)]; 85 86 NSString *type = [NSString stringWithFormat:@"Content-Type: %@", mimeType]; 87 [body appendData:iCocosEncode(type)]; 88 [body appendData:iCocosEncode(iCocosNewLien)]; 89 90 [body appendData:iCocosEncode(iCocosNewLien)]; 91 [body appendData:fileData]; 92 [body appendData:iCocosEncode(iCocosNewLien)]; 93 94 // 3.2.非文件参数 95 [params enumerateKeysAndObjectsUsingBlock:^(id key, id obj, BOOL *stop) { 96 [body appendData:iCocosEncode(@"--")]; 97 [body appendData:iCocosEncode(iCocosFileBoundary)]; 98 [body appendData:iCocosEncode(iCocosNewLien)]; 99 100 NSString *disposition = [NSString stringWithFormat:@"Content-Disposition: form-data; name=\"%@\"", key]; 101 [body appendData:iCocosEncode(disposition)]; 102 [body appendData:iCocosEncode(iCocosNewLien)]; 103 104 [body appendData:iCocosEncode(iCocosNewLien)]; 105 [body appendData:iCocosEncode([obj description])]; 106 [body appendData:iCocosEncode(iCocosNewLien)]; 107 }]; 108 109 // 3.3.结束标记 110 [body appendData:iCocosEncode(@"--")]; 111 [body appendData:iCocosEncode(iCocosFileBoundary)]; 112 [body appendData:iCocosEncode(@"--")]; 113 [body appendData:iCocosEncode(iCocosNewLien)]; 114 115 request.HTTPBody = body; 116 117 // 4.设置请求头(告诉服务器这次传给你的是文件数据,告诉服务器现在发送的是一个文件上传请求) 118 NSString *contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@", iCocosFileBoundary]; 119 [request setValue:contentType forHTTPHeaderField:@"Content-Type"]; 120 121 // 5.发送请求 122 [NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) { 123 NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableLeaves error:nil]; 124 NSLog(@"%@", dict); 125 }]; 126 }
标签:
原文地址:http://www.cnblogs.com/iCocos/p/4550324.html