</pre><pre code_snippet_id="652139" snippet_file_name="blog_20150424_2_1055172" name="code" class="objc">引用了 http://www.cnblogs.com/skyblue/archive/2013/05/08/3067108.html 里的部分文章
<pre name="code" class="objc">- (void)upImage {
NSMutableDictionary * dir=[NSMutableDictionary dictionaryWithCapacity:7];
NSString *cookie = [FileIO readStrByFileName:cStrCookie];
[dir setValue:cookie forKey:@"cookie"];
[dir setValue:@"张宇" forKey:@"nick"];
// NSString *url= [NSString stringWithFormat:@"http://193.168.1.128:9000/mobile/faceup"];
NSString *url= [NSString stringWithFormat:@"%@mobile/faceup", ServiceAdd];
NSLog(@"=======上传");
if([TMP_UPLOAD_IMG_PATH isEqualToString:@""]){
[HTTPImage postRequestWithURL:url postParems:dir picFilePath:nil picFileName:nil];
}else{
NSLog(@"有图标上传"); //<span style="font-family: Arial, Helvetica, sans-serif;">TMP_UPLOAD_IMG_PATH 图片路径</span>
NSArray *nameAry=[TMP_UPLOAD_IMG_PATH componentsSeparatedByString:@"/"];
[HTTPImage postRequestWithURL:url postParems:dir picFilePath:TMP_UPLOAD_IMG_PATH picFileName:[nameAry objectAtIndex:[nameAry count]-1]];;
}
}
<pre name="code" class="objc">/**
* 上传图片 与原文不太相同,在结束符的后面在加两个换行 一定要注意换行符是否正确添加。
* ps:我们的服务器和原文里的结束符都是在起始符尾部加“--”生成的,不知是否是规定。
**/
static NSString * const FORM_FLE_INPUT = @"pic";
+ (NSString *)postRequestWithURL: (NSString *)url // IN
postParems: (NSMutableDictionary *)postParems // IN
picFilePath: (NSString *)picFilePath // IN
picFileName: (NSString *)picFileName // IN
{
NSString *TWITTERFON_FORM_BOUNDARY = @"0xKhTmLbOuNdArY";
//根据url初始化request
NSMutableURLRequest* request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:url]
cachePolicy:NSURLRequestReloadIgnoringLocalCacheData
timeoutInterval:10];
//分界线 --AaB03x
NSString *boundary=[[NSString alloc]initWithFormat:@"---------------------------%@",TWITTERFON_FORM_BOUNDARY]; //起始符
NSString *boundaryInContent = [NSString stringWithFormat:@"--%@", boundary];
// //结束符 AaB03x--
NSString *endBoundary=[[NSString alloc]initWithFormat:@"%@--\r\n",boundaryInContent];//少
//得到图片的data
NSData* data;
if(picFilePath){
UIImage *image=[UIImage imageWithContentsOfFile:picFilePath];
//判断图片是不是png格式的文件
if (UIImagePNGRepresentation(image)) {
//返回为png图像。
data = UIImagePNGRepresentation(image);
}else {
//返回为JPEG图像。
data = UIImageJPEGRepresentation(image, 1.0);
}
}
//http body的字符串
NSMutableString *body=[[NSMutableString alloc]init];
//参数的集合的所有key的集合
NSArray *keys= [postParems allKeys];
//遍历keys
for(int i=0;i<[keys count];i++)
{
//得到当前key
NSString *key=[keys objectAtIndex:i];
//添加分界线,换行
[body appendFormat:@"%@\r\n",boundaryInContent];
//添加字段名称,换2行
[body appendFormat:@"Content-Disposition: form-data; name=\"%@\"\r\n\r\n",key];
//添加字段的值
[body appendFormat:@"%@\r\n",[postParems objectForKey:key]];
// NSLog(@"添加字段的值==%@",[postParems objectForKey:key]);
}
if(picFilePath){
添加分界线,换行
[body appendFormat:@"%@\r\n",boundaryInContent];
//声明pic字段,文件名为boris.png
[body appendFormat:@"Content-Disposition: form-data; name=\"%@\"; filename=\"%@.png\"\r\n",FORM_FLE_INPUT,picFileName];
//声明上传文件的格式
// [body appendFormat:@"Content-Type: image/jpge,image/gif, image/jpeg, image/pjpeg, image/pjpeg\r\n\r\n"];
// [body appendFormat:@"Content-Type: image/png\r\n\r\n"];
[body appendFormat:@"Content-Type: application/octet-stream\r\n\r\n"];
}
//声明结束符
NSString *end=[[NSString alloc]initWithFormat:@"\r\n%@",endBoundary];
//声明myRequestData,用来放入http body
NSMutableData *myRequestData=[NSMutableData data];
//将body字符串转化为UTF8格式的二进制
[myRequestData appendData:[body dataUsingEncoding:NSUTF8StringEncoding]];
if(picFilePath){
//将image的data加入
[myRequestData appendData:data];
}
//加入结束符--AaB03x--
[myRequestData appendData:[end dataUsingEncoding:NSUTF8StringEncoding]];
//设置HTTPHeader中连接类型
[request setValue:@"keep-alive" forHTTPHeaderField:@"Connection"];
//设置HTTPHeader中Content-Type的值
NSString *content=[[NSString alloc]initWithFormat:@"multipart/form-data; boundary=%@",boundary];
//设置HTTPHeader
[request setValue:content forHTTPHeaderField:@"Content-Type"];
//设置Content-Length
[request setValue:[NSString stringWithFormat:@"%d", [myRequestData length]] forHTTPHeaderField:@"Content-Length"];
//设置http body
[request setHTTPBody:myRequestData];
//http method
[request setHTTPMethod:@"POST"];
NSLog(@"-------%@,",request.allHTTPHeaderFields);
NSHTTPURLResponse *urlResponese = nil;
NSError *error = [[NSError alloc]init];
NSData* resultData = [NSURLConnection sendSynchronousRequest:request returningResponse:&urlResponese error:&error];
NSString* result= [[NSString alloc] initWithData:resultData encoding:NSUTF8StringEncoding];
int httpStatusCode =(int)[urlResponese statusCode];
if ( httpStatusCode != 200) {
DLog(@"服务器状态不正确,%d",httpStatusCode);
}
NSLog(@"返回结果=====%@",result);
return result;
}