文件夹下面的图片压缩上传服务器-----之代码封装
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);
}];
}
相关文章推荐
- [leetcode-275]H-Index II(java)
- C语言中网络地址与二进制数之间转换的函数小结
- 代码笔记——Toast显示文本,图片,图文
- asp.net中使用AutoCompleteExtender具体方法
- C#进阶系列——AOP?AOP!
- Eclipse中Axis2发布WebService
- C#将科学计数法转换为普通数字
- Java反射机制剖析(一)-定义和API
- Struts2_2
- VS2010/MFC编程入门教程之目录和总结
- Spring笔记――14.Spring3新增加的缓存机制
- Item 21:需要返回对象时,不要返回引用 Effective C++笔记
- IO流(Input Output)--引入
- php解析json
- Java I/O解读与使用实例
- jdk1.5新特性之-----枚举
- python+opencv2生成随机字母
- jdk1.5新特性之------->可变参数
- jdk1.5出现的新特性---->增强for循环
- C#中的线程(三) 使用多线程