首页 > 代码库 > iOS用AFN上传图片到java后台

iOS用AFN上传图片到java后台

-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    
    
    [manager POST:@"http://localhost:8080/FileUpdateDemo/FileDemo" parameters:nil constructingBodyWithBlock:^(id  _Nonnull formData) {
        
        UIImage *image = [UIImage imageNamed:@"22.jpg"];
        
        NSData *imageData =http://www.mamicode.com/ UIImagePNGRepresentation(image);
        
        
        //使用formData来拼接数据
        /*
         第一个参数:二进制数据 要上传的文件参数
         第二个参数:服务器规定的
         第三个参数:该文件上传到服务器以什么名称保存
         */
        [formData appendPartWithFileData:imageData name:@"file" fileName:@"xxxx.png" mimeType:@"image/png"];
        
//        [formData appendPartWithFileURL:[NSURL fileURLWithPath:@"/Users/zyyt/Desktop/11.jpg"] name:@"file" fileName:@"123.png" mimeType:@"image/png" error:nil];
        
      //  [formData appendPartWithFileURL:[NSURL fileURLWithPath:@"/Users/zyyt/Desktop/11.jpg"] name:@"file" error:nil];
        
    } progress:^(NSProgress * _Nonnull uploadProgress) {
        
        NSLog(@"%f",1.0 * uploadProgress.completedUnitCount/uploadProgress.totalUnitCount);
        
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        NSLog(@"上传成功---%@",responseObject);
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        NSLog(@"上传失败---%@",error);
    }];

    
}

 

iOS用AFN上传图片到java后台