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

 

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

#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);
    }];
    
    
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值