如何从Android的手机使用HTTP发送文件到服务器? [英] How to send a file in Android from mobile to server using http?
本文介绍了如何从Android的手机使用HTTP发送文件到服务器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在Android中,如何从移动使用HTTP发送文件(数据)的服务器。
In android, how to send a file(data) from mobile to server using http.
推荐答案
很简单,你可以使用一个POST请求,并提交您的文件为二进制(字节数组)。
Easy, you can use a Post request and submit your file as binary (byte array).
String url = "http://yourserver";
File file = new File(Environment.getExternalStorageDirectory().getAbsolutePath(),
"yourfile");
try {
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost(url);
InputStreamEntity reqEntity = new InputStreamEntity(
new FileInputStream(file), -1);
reqEntity.setContentType("binary/octet-stream");
reqEntity.setChunked(true); // Send in multiple parts if needed
httppost.setEntity(reqEntity);
HttpResponse response = httpclient.execute(httppost);
//Do something with response...
} catch (Exception e) {
// show error
}
这篇关于如何从Android的手机使用HTTP发送文件到服务器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文