Android的多部分上传 [英] Android Multipart Upload
问题描述
由于我的Android应用程序的一部分,我要上传的位图进行远程存储。我有简单的HTTP GET和POST的沟通工作完美,但对如何做一个多部分POST文件似乎是罕见的独角兽。
As part of my Android app, I'd like to upload bitmaps to be remotely stored. I have simple HTTP GET and POST communication working perfectly, but documentation on how to do a multipart POST seems to be as rare as unicorns.
另外,我想将图像直接从内存中的传输,而不是一个文件的工作。在下面的例子中code,我越来越被使用后与HttpClient的和MultipartEntity从文件的字节数组。
Furthermore, I'd like to transmit the image directly from memory, instead of working with a file. In the example code below, I'm getting a byte array from a file to be used later on with HttpClient and MultipartEntity.
File input = new File("climb.jpg");
byte[] data = new byte[(int)input.length()];
FileInputStream fis = new FileInputStream(input);
fis.read(data);
ByteArrayPartSource baps = new ByteArrayPartSource(input.getName(), data);
这一切似乎很清楚,我,但我不能为我的生命找出哪里得到这个ByteArrayPartSource。我已经联系到的HttpClient和httpmime JAR文件,但没有骰子。我听说包的结构彻底改变的HttpClient 3.x和4.x版之间
This all seems fairly clear to me, except that I can't for the life of me find out where to get this ByteArrayPartSource. I have linked to the httpclient and httpmime JAR files, but no dice. I hear that the package structure changed drastically between HttpClient 3.x and 4.x.
时使用此ByteArrayPartSource在Android的任何人,他们怎么导入?
Is anyone using this ByteArrayPartSource in Android, and how did they import it?
在文档中挖掘周围,在网上淘后,我想出了适合我的需要的东西。为了使multipart请求,如表单POST,下面code的伎俩对我来说:
After digging around in the documentation and scouring the Internet, I came up with something that fit my needs. To make a multipart request such as a form POST, the following code did the trick for me:
File input = new File("climb.jpg");
DefaultHttpClient client = new DefaultHttpClient();
HttpPost post = new HttpPost("http://localhost:3000/routes");
MultipartEntity multi = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE);
String line;
multi.addPart("name", new StringBody("test"));
multi.addPart("grade", new StringBody("test"));
multi.addPart("quality", new StringBody("test"));
multi.addPart("latitude", new StringBody("40.74"));
multi.addPart("longitude", new StringBody("40.74"));
multi.addPart("photo", new FileBody(input));
post.setEntity(multi);
HttpResponse resp = client.execute(post);
的HTTPMultipartMode.BROWSER_COMPATIBLE位是非常重要的。感谢<一href="http://radomirml.com/2009/02/13/file-upload-with-httpcomponents-successor-of-commons-httpclient/comment-page-1#comment-97"相对=nofollow>在这一个拉多米尔的博客。
推荐答案
试试这个:
HttpClient httpClient = new DefaultHttpClient() ;
HttpPost httpPost = new HttpPost("http://example.com");
MultipartEntity entity = new MultipartEntity();
entity.addPart("file", new FileBody(file));
httpPost.setEntity(entity );
HttpResponse response = null;
try {
response = httpClient.execute(httpPost);
} catch (ClientProtocolException e) {
Log.e("ClientProtocolException : "+e, e.getMessage());
} catch (IOException e) {
Log.e("IOException : "+e, e.getMessage());
}
这篇关于Android的多部分上传的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!