机器人通过HTTP POST多文件上传到服务器 [英] Android upload multi files to server via http post
本文介绍了机器人通过HTTP POST多文件上传到服务器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我需要从Android客户端文件上传到通过HTTP POST的URL。 如果上传仅有1档,比它是为我好。但我的目标URL页面code是看起来像下面的
I need to upload file from Android client to an URL via HTTP POST. If upload just 1 file, than it is OK for me. but my target URL page code is looks like following
<form action="file_upload.php" enctype ="multipart/form-data" method="post">
<input type="file" name="uploadedfile">
<input type="file" name="uploadedfile2">
<input type="submit">
</form>
如何上传2个文件通过HTTP POST在同一时间?
How can I upload 2 more files via HTTP POST at same time ?
推荐答案
我试着以下code,并确认这是可行的解决办法:
I tried following code and confirm that's workable solution:
StringBuffer responseBody=new StringBuffer();
Log.i(Constants.TAG, "Ready to upload file...");
HttpClient client=new DefaultHttpClient();
client.getParams().setParameter(CoreProtocolPNames.PROTOCOL_VERSION, HttpVersion.HTTP_1_1);
Log.i(Constants.TAG, "Set remote URL...");
HttpPost post=new HttpPost("http://IP.IP.IP.IP/file_upload.php");
MultipartEntity entity=new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE);
Log.i(Constants.TAG, "Adding file(s)...");
entity.addPart("uploadedfile", new FileBody((FileObj), "application/zip"));
entity.addPart("uploadedfile2", new FileBody((FileObj), "application/zip"));
Log.i(Constants.TAG, "Set entity...");
post.setEntity(entity);
BufferedReader bs=null;
try
{
Log.i(Constants.TAG, "Upload...");
HttpEntity hEntity=client.execute(post).getEntity();
bs=new BufferedReader(new InputStreamReader(hEntity.getContent()));
Log.i(Constants.TAG, "Response length - "+hEntity.getContentLength());
String s="";
while(s!=null)
{
responseBody.append(s);
s=bs.readLine();
Log.i(Constants.TAG, "Response body - "+s);
}
bs.close();
}
catch(IOException ioe)
{
Log.i(Constants.TAG, "Error on getting response from Server, "+ioe.toString());
ioe.printStackTrace();
responseBody.append("...");
}
我的平台是Android 2.2系统,并使用该解决方案需要得到httpmime.jar的项目库。
My platform is Android 2.2, and use this solution need to get httpmime.jar as project library.
这篇关于机器人通过HTTP POST多文件上传到服务器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文