2010-08-09 216 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];