iOS:将图像上传到服务器,无法打开

时间:2013-12-03 19:49:43

标签: ios objective-c http upload http-post

我使用以下代码将图像上传到我的servlet。

NSData *data = [NSData dataWithData:UIImagePNGRepresentation([UIImage imageNamed:@"jesus.png"])];
    NSString *urlString = @"http://localhost:8080/MyServlet/MyServlet?filename=jesus.png";

    NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
    [request setURL:[NSURL URLWithString:urlString]];
    [request setHTTPMethod:@"POST"];

    NSString *boundary = @"_187934598797439873422234";
    NSString *contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@",boundary];
    [request addValue:contentType forHTTPHeaderField: @"Content-Type"];

    NSMutableData *body = [NSMutableData data];
    [body appendData:[[NSString stringWithFormat:@"Content-Length %d\r\n\r\n", [data length] ] dataUsingEncoding:NSUTF8StringEncoding]];
    [body appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];


    [body appendData:[NSData dataWithData:data]];

    [body appendData:[[NSString stringWithFormat:@"\r\n--%@--\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];


    [request setHTTPBody:body];
    [request addValue:[NSString stringWithFormat:@"%d", [body length]] forHTTPHeaderField:@"Content-Length"];


    NSData *returnData = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
    NSString *returnString = [[NSString alloc] initWithData:returnData encoding:NSUTF8StringEncoding];
    NSLog(@"%@", returnString);

上传图像并将此图像保存到特定的Mac桌面路径中。但是,当我尝试打开该图像时,它会显示“文件无法打开,可能会损坏或使用预览无法识别的文件格式”。

我看到图像文件的大小正确。

从我的iOs客户端上传后无法打开此图像文件的原因是什么?有人可以建议我吗?

谢谢。

2 个答案:

答案 0 :(得分:1)

我认为你必须以边界开始身体,而不是内容长度。

试试这个:

NSMutableData *body = [NSMutableData data];
[body appendData:[[NSString stringWithFormat:@"--%@\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:@"Content-Length %d\r\n\r\n", [data length] ] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[NSData dataWithData:data]];
[body appendData:[[NSString stringWithFormat:@"\r\n--%@--\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];

答案 1 :(得分:0)

通过使代码简单如下解决。

NSString *urlString = [NSString stringWithFormat:@"http://localhost:8080/MyServlet/MyServlet?filename=myimage"];
NSData *data = [NSData dataWithData:UIImagePNGRepresentation([UIImage imageNamed:@"jesus.png"])];

NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
[request setURL:[NSURL URLWithString:urlString]];
[request setHTTPMethod:@"POST"];


[request addValue:@"image/png" forHTTPHeaderField:@"Content-Type"];

NSMutableData *body = [NSMutableData data];
[body appendData:[NSData dataWithData:data]];
[request setHTTPBody:body];

NSData *returnData = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
NSString *returnString = [[NSString alloc] initWithData:returnData encoding:NSUTF8StringEncoding];

NSLog(@"Ret: %@",returnString);