I'm trying to send a single image from my iPhone app to my Google App Engine Java server. On the server, I'm getting a FileUploadBase$InvalidContentTypeException
, which is thrown when the request is not a multipart request. ServletFileUpload.isMultipartContent(req)
is printing false. I'm trying to send an image, encoded as a jpeg, and a few other parameters, but im testing with just "name" and the image. There are a few fragmented sources of info online demonstrating how to take an image from the iPhone and post it to a GAE server. I'd like to document both ends of the process here.
Here's my objective-c code:
NSData *imageData = [NSData dataWithData: UIImageJPEGRepresentation(theImageView.image, 1.0f)];
NSURL *url = [NSURL URLWithString:@"http://myurl.com/myservlet"];
NSMutableURLRequest *request = [[[NSMutableURLRequest alloc] init] autorelease];
[request setURL:url];
[request setCachePolicy:NSURLRequestReloadIgnoringLocalCacheData];
[request setTimeoutInterval:60.0];
[request setHTTPMethod:@"POST"];
NSMutableData *body = [NSMutableData data];
[body appendData:[[NSString stringWithString:@"Content-Type: multipart/form-data; boundary=\"6G+f\"\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithString:@"Content-Disposition: form-data; name=\"name\"\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithString:@"whatever"] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithString:@"Content-Disposition: form-data; name=\"myImage\" filename=\"ipodfile.jpeg\"\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithString:@"Content-Type: image/jpeg\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:imageData];
[body appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[request setHTTPBody:body];
NSData *returnData = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
NSString *returnString = [[NSString alloc] initWithData:returnData encoding:NSUTF8StringEncoding];
NSLog(@"return %@",returnString);
I took most of the java code from here: http://stackoverflow.com/questions/1513603/how-to-upload-and-store-an-image-with-google-app-engine-java . I'll post the complete solution once the objective-c problem is found. THANKS!!