用于上传图像的 Iphone 代码有效,但在 android 中尝试相同的代码会给出状态代码 200 但图像无法上传。
手机密码
NSString *str = [NSString stringWithFormat:@"%@/s/sz/mws/%@?%@", appDel.baseUrl, request, parameter];
NSString *boundary = @"0xKhTmLbOuNdArY";
NSMutableURLRequest *mutRequest = [[NSMutableURLRequest alloc] init];
[mutRequest setCachePolicy:NSURLRequestReloadIgnoringLocalCacheData];
[mutRequest setHTTPShouldHandleCookies:NO];
[mutRequest setHTTPMethod:@"POST"];
NSString *contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@", boundary];
[mutRequest setValue:contentType forHTTPHeaderField: @"Content-Type"];
NSMutableData *body = [NSMutableData data];
UIGraphicsBeginImageContext(CGSizeMake(80,80));
[img drawInRect:CGRectMake(0,0,80,80)];
UIImage* newImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
NSData *imageData = UIImageJPEGRepresentation(newImage, 1.0);
if (imageData) {
[body appendData:[[NSString stringWithFormat:@"--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"picture\"; filename=\"image.jpg\"\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[@"Content-Type: image/jpeg\r\n\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:imageData];
[body appendData:[[NSString stringWithFormat:@"\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
}
[body appendData:[[NSString stringWithFormat:@"--%@--\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[mutRequest setHTTPBody:body];
[mutRequest setURL:[NSURL URLWithString:str]];
NSData *postData=[NSURLConnection sendSynchronousRequest:mutRequest returningResponse:nil error:nil];
NSDictionary *resultDict;
if (postData != nil) {
resultDict = [NSJSONSerialization JSONObjectWithData:postData options:0 error:nil];
} else {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Error" message:@"Please check your network connection" delegate:self cancelButtonTitle:@"Ok" otherButtonTitles:nil];
[alert show];
}
return resultDict;
}
安卓代码
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost(
"<URL>");
String boundary = "0xKhTmLbOuNdArY";
httppost.setHeader("Content-Type", "multipart/form-data");
File file = new File(Environment.getExternalStoragePublicDirectory(
Environment.DIRECTORY_DCIM).toString()
+ "/Camera/Test.jpg");
Bitmap bitmap = BitmapFactory.decodeResource(getResources(),
R.drawable.ic_lock_idle_alarm);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.PNG, 60, baos);
byte[] BitmapData = baos.toByteArray();
ByteBuffer buffer = ByteBuffer.allocate(800 * 800 * 4);
// StringBuffer buffer = new StringBuffer();
String p1 = new String(("--\r\n" + boundary).getBytes(), "UTF-8");
String p2 = new String(
("Content-Disposition: form-data; name=\"picture\"; filename=\"Temp.jpg\"\r\n")
.getBytes(), "UTF-8");
String p3 = new String(("Content-Type: image/jpeg\r\n\r\n").getBytes(),
"UTF-8");
String p4 = new String(("\r\n").getBytes(), "UTF-8");
String p5 = new String(("--" + boundary + "--\r\n").getBytes(), "UTF-8");
buffer.put(p1.getBytes());
buffer.put(p2.getBytes());
buffer.put(p3.getBytes());
buffer.put(BitmapData);
buffer.put(p4.getBytes());
buffer.put(p5.getBytes());
ByteArrayEntity entity = new ByteArrayEntity(buffer.array());
httppost.setEntity(entity);