AFNetworking 3.0携帯パラメータアップロードファイルDemo


一、サービス側コード:
サービス側はjavaが国産nutzでやったもので、実際のmvcフレームワークは大きく異なり、ファイルを提出すると同時にフォームパラメータも持っています.
	@AdaptBy(type=UploadAdaptor.class, args={"${app.root}/WEB-INF/tmp/user_avatar", "8192", "utf-8", "20000", "102400"})
	@At
	@Aop("redis")
	public Object uploadAvatar(@Param("file")TempFile tf, @Param("token") String token, AdaptorErrorContext err) {
		
		// token         
		NutMap re = new NutMap();
		if (err != null && err.getAdaptorErr() != null) {
			return re.setv("code", 3).setv("msg", "         ");
		}
		if (tf == null) {
			return re.setv("code", 4).setv("msg", "   ");
		}
		
		if (Strings.isBlank(token)) {
			return re.setv("code", 2).setv("msg", "");//   token
		}
		String member_id = jedis().get(token);//  redis  token  member_id
		if (Strings.isBlank(member_id)) {
			return re.setv("code", 2).setv("msg", "");//    
		}
		File f = tf.getFile(); //           
		FieldMeta meta = tf.getMeta(); //          
		String oldName = meta.getFileLocalName(); //           

		PropertiesProxy property = new PropertiesProxy("/custom/my.properties");

		String filename = System.currentTimeMillis() + oldName.substring(oldName.lastIndexOf("."));
		StringBuffer sb =new StringBuffer(property.get("my.filepath"));
		sb.append("\\avator\\");
		sb.append(filename);
		
		StringBuffer sbdb =new StringBuffer("/avator/");
		sbdb.append(filename);

		try {
			Files.move(f, new File(sb.toString()));
			//     
			dao.update(Member.class,Chain.make("avator", sbdb.toString()),Cnd.where("member_id", "=", member_id));
			
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return re.setv("code", 1);
		}

		return re.setv("code", 0).setv("msg", "");
	}

二、クライアントコード:
htmlフォームで提出すると、普通はこうなります
		
token:

IOSやったらこっちはAFNetworking 3.0使ってるからこう書いてる
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
    NSLog(@"    ");
    //    
    UIImage *image = [info objectForKey:UIImagePickerControllerEditedImage];
    NSData *imageData = UIImageJPEGRepresentation(image, 0.001);
    self.mv.portraitImageView.image = image;
    
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"text/html",@"text/plain", nil];
    //   
    NSMutableDictionary *parameter = [NSMutableDictionary dictionary];
    parameter[@"token"] = "param....";
	//     
    NSString *stringURL = [NSString stringWithFormat:@"%@%@",HOSTURL,kUploadAvatar];
    
    [manager POST:stringURL parameters:parameter constructingBodyWithBlock:^(id formData) {
        //     
        NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
        formatter.dateFormat = @"yyyyMMddHHmmss";
        NSString *str = [formatter stringFromDate:[NSDate date]];
        NSString *fileName = [NSString stringWithFormat:@"%@.jpg", str];
        
        [formData appendPartWithFileData:imageData name:@"file" fileName:fileName mimeType:@"image/png"];
        
    } success:^(AFHTTPRequestOperation *operation, id responseObject) {
        NSLog(@"    ");
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        NSLog(@"    ");
    }];
}

転載は以下のことを明記してください.http://blog.csdn.net/wangqiuyun/article/details/50220465