如何在安卓中使用HttpPost上传图像和视频文件



我需要使用 HttpPost 方法上传具有多个参数的图像和视频文件,例如文件名、描述、高度和宽度。

谢谢建议赞赏。

对于上传文件,有效的方法是将HttpPost与多部分/表单

一起使用多部分/形式文件内容存储在内存中或临时存储在磁盘上。在任一情况下,用户都负责根据需要将文件内容复制到会话级或持久存储。临时存储将在请求处理结束时清除

参考此 使用Post将文件从Android上传到网站/HTTP服务器

试试这段代码

  HttpClient client = new DefaultHttpClient();
    HttpPost post = new HttpPost(URL);
    MultipartEntity mpEntity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE);
    //Path of the file to be uploaded
    String filepath = params[0];
    File file = new File(filepath);
    ContentBody cbFile = new FileBody(file, SET_MIMETYPE);//"audio/basic"
    try {
        mpEntity.addPart(FILE_NAME, cbFile);
        post.setEntity(mpEntity);
        HttpResponse response1 = client.execute(post);
        HttpEntity resEntity = response1.getEntity();
    } catch (Exception e) {
        e.printStackTrace();
    }

或另请参阅此链接"http://www.androidhive.info/2014/12/android-uploading-camera-image-video-to-server-with-progress-bar/"谢谢

最新更新