使用 RESTKit 上传 jpeg 文件时 Tomcat 上的 HTTP/1.1 200 213

发布于 2024-11-29 08:15:08 字数 1259 浏览 1 评论 0原文

全部, 我正在使用 RestKit for iOS 将 JPEG 文件上传到我的 Java Web 服务。我参考了这个

教程进行开发文件上传网络服务,当我通过网络浏览器使用它时,它工作得非常好。

但是,当我尝试使用 RESTKit 上传文件时,在 TOMCAT 日志中我收到 HTTP/1.1 200 213 状态代码,但我的文件未上传。

这是我的 RESTKit 代码:

 RKObjectManager* manager = [RKObjectManager sharedManager]; 
RKObjectLoader* objectLoader = [manager objectLoaderWithResourcePath:@"/fileuploaded" delegate:self];
objectLoader.method = RKRequestMethodPOST;

UIImage *image = [UIImage imageNamed:@"rental_car_bill.jpeg"];
NSData *imageData = UIImageJPEGRepresentation(image, 1.0); 
// attach image 
RKParams *params = [RKParams paramsWithDictionary:(NSDictionary*)objectLoader.params]; 
RKParamsAttachment *attachment = [params setData:imageData 
                                        MIMEType:@"image/jpeg" forParam:@"photo"]; 
attachment.fileName = @"samplejpeg";
objectLoader.params = params;
objectLoader.targetObject = self;
[objectLoader send];

编辑: 上面的实现确实有效并且文件确实被上传。但是,在委托方法中: - (void)objectLoader:(RKObjectLoader*)objectLoader didLoadObjects:(NSArray*)objects

超出此范围后,我的应用程序在 [RKObjectLoader dealloc]; 处崩溃;

All,
I am using RestKit for iOS to upload a JPEG file to my Java web service. I referred to this

tutorial for developing file upload web service and it works perfectly fine when i use it through my web browser.

However, when i try to upload a file using RESTKit then in TOMCAT logs i get HTTP/1.1 200 213 status code and my file is not uploaded.

Here is my RESTKit code:

 RKObjectManager* manager = [RKObjectManager sharedManager]; 
RKObjectLoader* objectLoader = [manager objectLoaderWithResourcePath:@"/fileuploaded" delegate:self];
objectLoader.method = RKRequestMethodPOST;

UIImage *image = [UIImage imageNamed:@"rental_car_bill.jpeg"];
NSData *imageData = UIImageJPEGRepresentation(image, 1.0); 
// attach image 
RKParams *params = [RKParams paramsWithDictionary:(NSDictionary*)objectLoader.params]; 
RKParamsAttachment *attachment = [params setData:imageData 
                                        MIMEType:@"image/jpeg" forParam:@"photo"]; 
attachment.fileName = @"samplejpeg";
objectLoader.params = params;
objectLoader.targetObject = self;
[objectLoader send];

EDIT:
Above implementation does work and the file does get uploaded. However, in the delegate method: - (void)objectLoader:(RKObjectLoader*)objectLoader didLoadObjects:(NSArray*)objects

After it gets out of the scope of this, then my application crashes at [RKObjectLoader dealloc];

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

放飞的风筝 2024-12-06 08:15:08

要使用restkit上传图片,我使用以下方法,我更喜欢使用块来避免我的应用程序出现一些问题,也许这样你就可以避免崩溃:

- (void) upload: (KFMedia *) pic onLoad:(RKObjectLoaderDidLoadObjectBlock) loadBlock onError:(RKRequestDidFailLoadWithErrorBlock)failBlock{

    //pic.image returns an UIImage
    RKParams* imageParams = [RKParams params];
    NSData* imageData = UIImageJPEGRepresentation(pic.image, 0.7f);
    [imageParams setData:imageData MIMEType:@"image/jpg" forParam:@"FileUpload"];
    NSString *resourcePath = @"/api/upload/";


    //My API Server will return a JSON that represents my KFMedia Class after uploading the image, so here I get the propper mapping for that
    RKObjectMapping *mapping = [[RKObjectManager sharedManager].mappingProvider objectMappingForClass:[KFMedia class]];

    [[RKObjectManager sharedManager] loadObjectsAtResourcePath:resourcePath usingBlock:^(RKObjectLoader *loader) {
        loader.method = RKRequestMethodPOST;
        loader.params = imageParams;
        [self settingsForLoader:loader withMapping:mapping onLoad:loadBlock onError:failBlock];

    }];

}

- (void) settingsForLoader: (RKObjectLoader *) loader withMapping: (RKObjectMapping *) mapping onLoad:(RKObjectLoaderDidLoadObjectBlock) loadBlock onError:(RKRequestDidFailLoadWithErrorBlock)failBlock{

    loader.objectMapping = mapping;
    loader.delegate = self;
    loader.onDidLoadObject = loadBlock;
    loader.onDidFailWithError = ^(NSError * error){
        NSLog(@"%@",error);
    };
    loader.onDidFailLoadWithError = failBlock;
    loader.onDidLoadResponse = ^(RKResponse *response) {
        [self fireErrorBlock:failBlock onErrorInResponse:response];
    };

}

To upload pictures using restkit I use the following methods, I prefer using blocks to avoid some problems on my app, maybe that way you can avoid your crash:

- (void) upload: (KFMedia *) pic onLoad:(RKObjectLoaderDidLoadObjectBlock) loadBlock onError:(RKRequestDidFailLoadWithErrorBlock)failBlock{

    //pic.image returns an UIImage
    RKParams* imageParams = [RKParams params];
    NSData* imageData = UIImageJPEGRepresentation(pic.image, 0.7f);
    [imageParams setData:imageData MIMEType:@"image/jpg" forParam:@"FileUpload"];
    NSString *resourcePath = @"/api/upload/";


    //My API Server will return a JSON that represents my KFMedia Class after uploading the image, so here I get the propper mapping for that
    RKObjectMapping *mapping = [[RKObjectManager sharedManager].mappingProvider objectMappingForClass:[KFMedia class]];

    [[RKObjectManager sharedManager] loadObjectsAtResourcePath:resourcePath usingBlock:^(RKObjectLoader *loader) {
        loader.method = RKRequestMethodPOST;
        loader.params = imageParams;
        [self settingsForLoader:loader withMapping:mapping onLoad:loadBlock onError:failBlock];

    }];

}

- (void) settingsForLoader: (RKObjectLoader *) loader withMapping: (RKObjectMapping *) mapping onLoad:(RKObjectLoaderDidLoadObjectBlock) loadBlock onError:(RKRequestDidFailLoadWithErrorBlock)failBlock{

    loader.objectMapping = mapping;
    loader.delegate = self;
    loader.onDidLoadObject = loadBlock;
    loader.onDidFailWithError = ^(NSError * error){
        NSLog(@"%@",error);
    };
    loader.onDidFailLoadWithError = failBlock;
    loader.onDidLoadResponse = ^(RKResponse *response) {
        [self fireErrorBlock:failBlock onErrorInResponse:response];
    };

}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文