2010-08-09 294 views
0

我有一个上传图像到服务器的问题。此外我需要上传一个字符串值到服务器与该图像。图片上传没问题,但目前我没有成功上传字符串。以下是我的上传代码。请帮助我。图像上传到JSP服务器

NSData *imageData = UIImageJPEGRepresentation(theImage,0.9); 



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


NSString* width [email protected]"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; 
[email protected]"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]; 

回答

0

我得到了解决,

这是我们需要做的代码如下。问题不在于使用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];