web服务器我查看了相关的几个问题(例如该代码张贴了很多,但对我不起作用由于某些原因:How do I add REQUEST values to an HTTP POST method using multipart to upload a file to a PHP server in Android?),并试图一对夫妇的解决方案,但我的文件在服务器上查看时不能解码为jpg。我上传的iPhone应用程序工作,但现在当我移植到Android我想不出这样做的正确方法。服务器端是一个asp.net流式文件上传API。读取JPG到安卓
这里是我的base64编码逻辑:
Bitmap fileToUpload = BitmapFactory.decodeFile(obs.getPhotoLocation());
ByteArrayOutputStream stream = new ByteArrayOutputStream();
fileToUpload.compress(Bitmap.CompressFormat.JPEG, 100, stream);
byte[] image = stream.toByteArray();
String data = Base64.encodeToString(image, true);
new HttpConnection(handler).postBitmap(url.toString(), data);
这里是postBitmap代码
HttpPost httpPostBitmap = new HttpPost(url);
httpPostBitmap.addHeader("Content-type", "image/jpg");
httpPostBitmap.addHeader("Connection", "Keep-Alive");
httpPostBitmap.setEntity(new StringEntity(data));
response = httpClient.execute(httpPostBitmap);
转移前和服务器上的文件是相同的大小,所以我不相信这是传输出现问题并且更可能出现编码问题。
这里是工作的iOS代码我试图端口:
NSURL *uploadUrl = [NSURL URLWithString:FILEUPLOAD_URL];
UIImage *testImage = [Helpers getImageFromPhoto:self.obsToTransmit.observationPhoto];
NSData *imageData = UIImageJPEGRepresentation(testImage, 1.0);
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
NSString *msgLength = [NSString stringWithFormat:@"%d", [imageData length]];
[request addValue:@"image/jpg" forHTTPHeaderField:@"Content-type"];
[request setHTTPMethod:@"POST"];
[request addValue:msgLength forHTTPHeaderField:@"Content-Length"];
[request setHTTPBody:imageData];
theConnection = [[NSURLConnection alloc] initWithRequest:request delegate:self];
感谢一大堆! 斯科特
在这篇文章中找到答案:http://stackoverflow.com/questions/7363577/streaming-file-from-android-to-net-http-service。 – 2012-03-25 04:37:23