RestKit上传图片参数

时间:2013-07-23 11:58:22

标签: ios image spring upload restkit

我必须使用Spring配置服务器端以使用RestKit 0.2上传图像。

我使用以下代码进行上传:

NSMutableURLRequest *request = [[RKObjectManager sharedManager] multipartFormRequestWithObject:obj
                                                                                        method:RKRequestMethodPOST
                                                                                          path:nil
                                                                                    parameters:nil
                                                                     constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {


    [formData appendPartWithFileData:UIImageJPEGRepresentation(obj.image, 1.0)
                                name:@"image"
                            fileName:@"image.jpg"
                            mimeType:@"image/jpeg"];

}];



RKObjectRequestOperation *operation = [[RKObjectManager sharedManager] objectRequestOperationWithRequest:request
                                                                                                 success:^(RKObjectRequestOperation *operation, RKMappingResult *mappingResult) {

                                                                                                     NSLog(@"%@", [mappingResult firstObject]);

                                                                                                 } failure:^(RKObjectRequestOperation *operation, NSError *error) {

                                                                                                 }];

[[RKObjectManager sharedManager] enqueueObjectRequestOperation:operation];

我的WS方面:

@RequestMapping(value = "findMatch", method = RequestMethod.POST)
public void findMatch(@RequestParam(value = "image") Part image){

    // ...
}

当我尝试上传图片时,我收到错误消息:客户端发送的请求在语法上不正确。

我的问题是,图像参数的名称是什么?

感谢。

1 个答案:

答案 0 :(得分:0)

<强>解决!

@RequestMapping(value = "findMatch", method = RequestMethod.POST)
public void findMatch(HttpServletRequest request){

     MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
     MultipartFile multipartFile = multipartRequest.getFile("image");

     // handle file...
}