您的位置:首页 > 编程语言

文件夹下面的图片压缩上传服务器-----之代码封装

2015-09-06 08:58 357 查看

首先导入头文件以及宏定义

#import "SSZipArchive.h"

#define HMFileBoundary @"heima"

#define HMNewLine @"\r\n"

#define HMEncode(str) [str dataUsingEncoding:NSUTF8StringEncoding]

//封装 获取一个文件的 MIMEType

- (NSString *)MIMEType:(NSURL *)url{

//1.创建一个请求

NSURLRequest *request=[NSURLRequest requestWithURL:url];

//2.发送请求(返回响应)

NSURLResponse *response=nil;

[NSURLConnection sendSynchronousRequest:request returningResponse:&response error:nil];

//获得MIMEType

return response.MIMEType;

}

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{

NSString *caches=[NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES)lastObject];

//0.获得需要压缩的文件

NSString *images=[caches stringByAppendingPathComponent:@"images"];

//1.创建一个zip文件

NSString *zipFile=[caches stringByAppendingPathComponent:@"images.zip"];

BOOL result=[SSZipArchive createZipFileAtPath:zipFile withContentsOfDirectory:images];

if (result) {

NSString *MIMEType=[self MIMEType:[NSURL URLWithString:zipFile]];

NSData *data=[NSData dataWithContentsOfFile:zipFile];

[self upload:@"images.zip" mimeType:MIMEType fileData:data params:nil];

}

}

//封装好了 下面是上传的文件参数和非文件参数的拼接

- (void)upload:(NSString *)filename mimeType:(NSString *)mimeType fileData:(NSData *)fileData params:(NSDictionary *)params{

//1.请求路径(上传文件)

NSURL *url=[NSURL URLWithString:@"http://192.168.1.121:8090/upload"];

//2.创建一个POST请求

NSMutableURLRequest *request=[NSMutableURLRequest requestWithURL:url];

request.HTTPMethod=@"POST";

//3.设置请求体积

NSMutableData *body=[NSMutableData data];

//3.1文件参数

[body appendData:HMEncode(@"--")];

[body appendData:HMEncode(HMFileBoundary)];

[body appendData:HMEncode(HMNewLine)];

// string.Format("Content-Disposition:form-data;name=\"uploadedfile\";filename=\"{0}\"\r\nContent-Type:application/octet-stream\r\n\r\n", fileName);

NSString *Disposition=[NSString stringWithFormat:@"Content-Disposition:form-data;name=\"uploadedfile\"; filename=\"%@\"",filename];

[body appendData:HMEncode(Disposition)];

[body appendData:HMEncode(HMNewLine)];

//[body appendData:HMEncode([NSString stringWithFormat:@"Content-Type: %@", mimeType])];

[body appendData:HMEncode(@"Content-Type: application/octet-stream")];

[body appendData:HMEncode(HMNewLine)];

[body appendData:HMEncode(HMNewLine)];

[body appendData:fileData];

[body appendData:HMEncode(HMNewLine)];

//3.2非文件参数

[params enumerateKeysAndObjectsUsingBlock:^(id key, id obj, BOOL *stop) {

[body appendData:HMEncode(@"--")];

[body appendData:HMEncode(HMFileBoundary)];

[body appendData:HMEncode(HMNewLine)];

NSString *disposition=[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"%@\"",key];

[body appendData:HMEncode(disposition)];

[body appendData:HMEncode(HMNewLine)];

[body appendData:HMEncode(HMNewLine)];

[body appendData:HMEncode([obj description])];

[body appendData:HMEncode(HMNewLine)];

}];

//3.3结束标记

[body appendData:HMEncode(@"--")];

[body appendData:HMEncode(HMFileBoundary)];

[body appendData:HMEncode(@"--")];

[body appendData:HMEncode(HMNewLine)];

request.HTTPBody = body;

//4设置请求头(告诉服务器这次传得是文件数据 现在发送的是一个文件上传请求)

NSString *contentType=[NSString stringWithFormat:@"multipart/form-data; boundary=%@", HMFileBoundary];

[request setValue:contentType forHTTPHeaderField:@"Content-Type"];

NSString *contentLenth=[NSString stringWithFormat:@"%ld",body.length];

[request setValue:contentLenth forHTTPHeaderField:@"Content-Length"];

//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);

}];

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: