如何使用 http 将 Android 中的文件从移动设备发送到服务器? [英] How do I send a file in Android from a mobile device to server using http?

查看:25
本文介绍了如何使用 http 将 Android 中的文件从移动设备发送到服务器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 android 中,如何使用 http 将文件(数据)从移动设备发送到服务器.

In android, how do I send a file(data) from a mobile device 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
}

这篇关于如何使用 http 将 Android 中的文件从移动设备发送到服务器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆