android - 如何使用 http 将 Android 中的文件从移动设备发送到服务器?

标签 android http post

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

最佳答案

很简单,您可以使用 Post 请求并将文件作为二进制(字节数组)提交。

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 将 Android 中的文件从移动设备发送到服务器?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4126625/

相关文章:

android - 选择 ListView 项时更改 TextView 的字体颜色

node.js - 在 Express 中使用 Multer 设置响应状态?

http - 为什么HTTP协议(protocol)要设计成明文方式?

http - 错误 TS2346 : Supplied parameters do not match any signature of call target

java - 无法在第二个屏幕上制作全屏 Android 演示文稿

java - 通过签名比较 SSL 证书 : is it enough?

android - 如何使用 LocationManager 在 android 中获取当前位置

android - 来自Android手机的POST请求中的特殊字符消失

c# - MVC 发布到另一个操作

ruby-on-rails - 使用 curl 将 JSON 数据发布到简单的 Rails 应用程序