Objective c 将图像上载到JSP服务器

Objective c 将图像上载到JSP服务器,objective-c,iphone,Objective C,Iphone,我在将图像上载到服务器时遇到问题。我还需要将字符串值上载到带有该图像的服务器。图像上传很好,但我还没有成功上传字符串。下面是我上传的代码。请帮帮我 NSData *imageData = UIImageJPEGRepresentation(theImage,0.9); NSMutableURLRequest *request = [[[NSMutableURLRequest alloc] init] autorelease]; [request setURL:[NSURL URLWithS

我在将图像上载到服务器时遇到问题。我还需要将字符串值上载到带有该图像的服务器。图像上传很好,但我还没有成功上传字符串。下面是我上传的代码。请帮帮我

NSData *imageData = UIImageJPEGRepresentation(theImage,0.9);



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


NSString* width =@"10";

[request setValue:@"300"       forHTTPHeaderField:@"Keep-Alive"];
[request setValue:@"keep-live" forHTTPHeaderField:@"Connection"];
[request setValue:width forHTTPHeaderField:@"spotfk"];

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

NSMutableData *body = [NSMutableData data];
[body appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithString:@"Content-Disposition: form-data; name=\"userfile\"; filename=\"ipodfile.jpg\"\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithString:@"Content-Type: application/octet-stream\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[NSData dataWithData:imageData]];
[body appendData:[[NSString stringWithFormat:@"\r\n--%@--\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];

[request setHTTPBody:body];


Spots* spotObj=[spotsList objectAtIndex:spotIdValue-1];

SpotItems* spotItemObj=[[SpotItems alloc]init];

spotItemObj.imageX_Coordinate=0.0;
spotItemObj.imageY_Coordinate=0.0;
spotItemObj.imageWidth=spotObj.spotWidth;
spotItemObj.imageHight=spotObj.spotHight;
spotItemObj.imageName=@"logo.png";

UIView* subView=[[UIView alloc]initWithFrame:CGRectMake(0, 0, spotObj.spotWidth, spotObj.spotHight)];
subView.backgroundColor=[UIColor clearColor];
spotItemObj.subView=subView;

UIImageView* newSpotItemImageView=[[UIImageView alloc]initWithFrame:CGRectMake(0, 0, spotObj.spotWidth, spotObj.spotHight)];
newSpotItemImageView.image=[UIImage imageNamed:spotItemObj.imageName];
[spotItemObj.subView addSubview:newSpotItemImageView];

[spotObj.spotsItemsList addObject:spotItemObj];
[spotItemObj release];

if([spotObj.spotsItemsList count]==2)
{
    [self startInternalAnimation:spotObj.spotId-1];
}

NSData *returnData = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
NSString *returnString = [[NSString alloc] initWithData:returnData encoding:NSUTF8StringEncoding];
我找到了解决办法

也就是说,我们需要按如下方式编写代码。问题是没有使用dataUsingEndcoding方法和边界,现在代码如下

NSMutableData *postBody = [NSMutableData data];
[postBody appendData:[[NSString stringWithFormat:@"--%@\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[postBody appendData:[[NSString stringWithString:@"Content-Disposition: form-data; name=\"spotfk\"\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
[postBody appendData:[[NSString stringWithString:spotFk] dataUsingEncoding:NSUTF8StringEncoding]];
[postBody appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[postBody appendData:[[NSString stringWithString:@"Content-Disposition: form-data; name=\"media\"; filename=\"test.png\"\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
[postBody appendData:[[NSString stringWithString:@"Content-Type: application/octet-stream\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
[postBody appendData:imageData];
[postBody appendData:[[NSString stringWithFormat:@"\r\n--%@--\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];

[request setHTTPBody:postBody];