1 // 2 // ViewController.m 3 // IOS_0206_文件上传 4 // 5 // Created by ma c on 16/2/6. 6 // Copyright © 2016年 博文科技. All rights reserved. 7 // 8 9 #import "ViewController.h" 10 #define BWFileBoundary @"----------BowenKeJi" 11 #define BWNewLine @" " 12 #define BWEncode(str) [str dataUsingEncoding:NSUTF8StringEncoding] 13 14 @interface ViewController () 15 16 @end 17 18 @implementation ViewController 19 20 /* 21 一、文件上传的概括 22 参数1 23 参数2 24 结束标记3 25 26 -------------------------------------------------------------- 27 二、文件上传的格式 28 29 1.文件参数 30 BowenKeJi 31 32 Content-Disposition: form-data; name="参数名"; filename="文件名" 33 34 Content-Type: 文件类型/MIMEType 35 36 37 文件具体数据 38 39 2.非文件参数 40 BowenKeJi 41 42 Content-Disposition: form-data; name="参数名" 43 44 45 参数值 46 47 3.结束标记 48 BowenKeJi-- 49 50 ------------------------------------------------------------------- 51 三、文件的MIMEType 52 1.百度搜索 53 2.apache-tomcat-版本号/conf/web.xml 54 3.加载文件时通过Reponse获得 55 ------------------------------------------------------------------- 56 */ 57 58 - (void)viewDidLoad { 59 [super viewDidLoad]; 60 61 self.view.backgroundColor = [UIColor cyanColor]; 62 63 NSString *name = @"jack"; 64 [self test:&name]; 65 NSLog(@"%@",name); 66 67 } 68 69 ///在方法中更改字符串的值 70 - (void)test:(NSString **)str 71 { 72 *str = @"bowen"; 73 } 74 75 - (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event 76 { 77 //[self upload]; 78 79 //文件参数 80 NSDictionary *params = @{ 81 @"username" : @"bowen" 82 }; 83 //文件数据 84 // UIImage *image = [UIImage imageNamed:@"abc"]; 85 // NSData *imgData = UIImageJPEGRepresentation(image, 1); 86 // [self upload:@"text.png" AndMIMEType:@"image/png" AndfileData:imgData AndParams:params]; 87 88 NSURL *url = [[NSBundle mainBundle] URLForResource:@"abc" withExtension:@"jpg"]; 89 NSData *data = [NSData dataWithContentsOfURL:url]; 90 NSString *MIMEType = [self MIMEType:url]; 91 [self upload:@"cba.jpg" AndMIMEType:MIMEType AndfileData:data AndParams:params]; 92 93 94 // NSURL *url1 = [[NSBundle mainBundle] URLForResource:@"abc" withExtension:@"jpg"]; 95 // //NSURL *url1 = [NSURL fileURLWithPath:@"/Users/apple/Desktop/hehe.text"]; 96 // NSString *mimeType = [self MIMEType:url1]; 97 // NSLog(@"%@",mimeType); 98 99 } 100 101 ///文件的MIMEType 102 - (NSString *)MIMEType:(NSURL *)url 103 { 104 //1.创建一个请求 105 NSURLRequest *request = [NSURLRequest requestWithURL:url]; 106 107 NSURLResponse *response = nil; 108 //2.发送请求(返回响应) 109 [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:nil]; 110 //3.获得MIMEType 111 return response.MIMEType; 112 } 113 114 ///文件上传未封装 115 - (void)upload 116 { 117 // 1.请求路径 118 NSURL *url = [NSURL URLWithString:@"http://localhost:8080/MJServer/upload"]; 119 // 2.创建一个POST请求 120 NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url]; 121 request.HTTPMethod = @"POST"; 122 // 2.设置请求头(告诉服务器这次上传的是文件数据) 123 NSString *contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@",BWFileBoundary]; 124 [request setValue:contentType forHTTPHeaderField:@"Content-Type"]; 125 // 3.设置请求体 126 NSMutableData *body = [NSMutableData data]; 127 128 // 4.1文件参数 129 [body appendData:BWEncode(@"--")]; 130 [body appendData:BWEncode(BWFileBoundary)]; 131 [body appendData:BWEncode(BWNewLine)]; 132 133 [body appendData:BWEncode(@"Content-Disposition: form-data; name="file"; filename="ts.jpg"")]; 134 [body appendData:BWEncode(BWNewLine)]; 135 136 [body appendData:BWEncode(@"Content-Type: image/jpg")]; 137 [body appendData:BWEncode(BWNewLine)]; 138 139 //具体内容 140 [body appendData:BWEncode(BWNewLine)]; 141 UIImage *image = [UIImage imageNamed:@"abc"]; 142 NSData *imgData = UIImageJPEGRepresentation(image, 1); 143 [body appendData:imgData]; 144 [body appendData:BWEncode(BWNewLine)]; 145 146 // 4.2非文件参数(用户名参数) 147 [body appendData:BWEncode(@"--")]; 148 [body appendData:BWEncode(BWFileBoundary)]; 149 [body appendData:BWEncode(BWNewLine)]; 150 151 [body appendData:BWEncode(@"Content-Disposition: form-data; name="username"")]; 152 [body appendData:BWEncode(BWNewLine)]; 153 154 [body appendData:BWEncode(BWNewLine)]; 155 [body appendData:BWEncode(@"bowen")]; 156 [body appendData:BWEncode(BWNewLine)]; 157 158 // 4.3结束标记 159 [body appendData:BWEncode(@"--")]; 160 [body appendData:BWEncode(BWFileBoundary)]; 161 [body appendData:BWEncode(@"--")]; 162 [body appendData:BWEncode(BWNewLine)]; 163 164 request.HTTPBody = body; 165 166 // 5.发送请求 167 [NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse * _Nullable response, NSData * _Nullable data, NSError * _Nullable connectionError) { 168 NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableLeaves error:nil]; 169 NSLog(@"%@",dict); 170 }]; 171 } 172 173 ///文件上传封装 174 - (void)upload:(NSString *)filename AndMIMEType:(NSString *)mimeType AndfileData:(NSData *)fileData 175 AndParams:(NSDictionary *)dict 176 { 177 // 1.请求路径 178 NSURL *url = [NSURL URLWithString:@"http://localhost:8080/MJServer/upload"]; 179 // 2.创建一个POST请求 180 NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url]; 181 request.HTTPMethod = @"POST"; 182 // 2.设置请求头(告诉服务器这次上传的是文件数据) 183 NSString *contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@",BWFileBoundary]; 184 [request setValue:contentType forHTTPHeaderField:@"Content-Type"]; 185 // 3.设置请求体 186 NSMutableData *body = [NSMutableData data]; 187 188 // 4.1文件参数 189 [body appendData:BWEncode(@"--")]; 190 [body appendData:BWEncode(BWFileBoundary)]; 191 [body appendData:BWEncode(BWNewLine)]; 192 NSString *disposition = [NSString stringWithFormat:@"Content-Disposition: form-data; name="file"; filename="%@"",filename]; 193 [body appendData:BWEncode(disposition)]; 194 [body appendData:BWEncode(BWNewLine)]; 195 196 NSString *type = [NSString stringWithFormat:@"Content-Type: %@",mimeType]; 197 [body appendData:BWEncode(type)]; 198 [body appendData:BWEncode(BWNewLine)]; 199 200 //具体内容 201 [body appendData:BWEncode(BWNewLine)]; 202 [body appendData:fileData]; 203 [body appendData:BWEncode(BWNewLine)]; 204 205 // 4.2非文件参数(用户名参数) 206 207 [dict enumerateKeysAndObjectsUsingBlock:^(id _Nonnull key, id _Nonnull obj, BOOL * _Nonnull stop) { 208 [body appendData:BWEncode(@"--")]; 209 [body appendData:BWEncode(BWFileBoundary)]; 210 [body appendData:BWEncode(BWNewLine)]; 211 212 NSString *disposition = [NSString stringWithFormat:@"Content-Disposition: form-data; name="%@"",key]; 213 214 [body appendData:BWEncode(disposition)]; 215 [body appendData:BWEncode(BWNewLine)]; 216 217 [body appendData:BWEncode(BWNewLine)]; 218 [body appendData:BWEncode([obj description])]; 219 [body appendData:BWEncode(BWNewLine)]; 220 221 }]; 222 // 4.3结束标记 223 [body appendData:BWEncode(@"--")]; 224 [body appendData:BWEncode(BWFileBoundary)]; 225 [body appendData:BWEncode(@"--")]; 226 [body appendData:BWEncode(BWNewLine)]; 227 228 request.HTTPBody = body; 229 230 // 5.发送请求 231 [NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse * _Nullable response, NSData * _Nullable data, NSError * _Nullable connectionError) { 232 NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableLeaves error:nil]; 233 NSLog(@"%@",dict); 234 }]; 235 } 236 @end