Android使用HTTP多部分表单数据将视频上传到远程服务器

时间:2012-06-22 21:30:08

标签: android http multipartform-data

我在某个当前项目的某个部分遇到了麻烦,觉得我现在被卡住了。我正在尝试使用HTTP帖子和多部分表单数据进行视频上传。我觉得我在理解HTTP协议,特别是多部分表单数据方面遇到了障碍。

我有一个以http://videoupload.thecompany.com/VideoApp.xml?method=upload&objectType=person&objectId=777777格式上传视频的网址。我还需要包含标题,说明和视频文件。这些是“多部分数据”吗?

我已尝试调整此解决方案以满足我的需求Upload video from Android to server?,并在所有其他conn.setRequestProperty()调用之后设置额外数据,如下所示:

conn.setRequestProperty("title", "video title");
conn.setRequestProperty("description", "video description");

但这不适合我。代码的原始作者发表评论,以便在以后添加大约30行的多部分表单数据,但我不明白为什么。谢谢你的帮助。

1 个答案:

答案 0 :(得分:20)

这是我提出的两步解决方案,主要来自找到的信息和链接here。这个解决方案比一些相关的SO帖子中的upload2server()方法更容易掌握。希望这有助于其他人。

1)从图库中选择视频文件。

创建一个变量private static final int SELECT_VIDEO = 3; - 你使用的是什么号码并不重要,只要这是你稍后检查的号码。然后,使用意图选择视频。

Intent intent = new Intent();
intent.setType("video/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(Intent.createChooser(intent,"Select a Video "), SELECT_VIDEO);

使用onActivityResult()启动uploadVideo()方法。

public void onActivityResult(int requestCode, int resultCode, Intent data) {

    if (resultCode == RESULT_OK) {

        if (requestCode == SELECT_VIDEO) {
            System.out.println("SELECT_VIDEO");
            Uri selectedVideoUri = data.getData();
            selectedPath = getPath(selectedVideoUri);
            System.out.println("SELECT_VIDEO Path : " + selectedPath);

            uploadVideo(selectedPath);
        }      
    }
}

private String getPath(Uri uri) {
    String[] projection = { MediaStore.Video.Media.DATA, MediaStore.Video.Media.SIZE, MediaStore.Video.Media.DURATION}; 
    Cursor cursor = managedQuery(uri, projection, null, null, null);
    cursor.moveToFirst(); 
    String filePath = cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.Video.Media.DATA));
    int fileSize = cursor.getInt(cursor.getColumnIndexOrThrow(MediaStore.Video.Media.SIZE));
    long duration = TimeUnit.MILLISECONDS.toSeconds(cursor.getInt(cursor.getColumnIndexOrThrow(MediaStore.Video.Media.DURATION)));


    //some extra potentially useful data to help with filtering if necessary
    System.out.println("size: " + fileSize);
    System.out.println("path: " + filePath);
    System.out.println("duration: " + duration);

    return filePath;
}

2)转到http://hc.apache.org/downloads.cgi,下载最新的HttpClient jar,将其添加到您的项目中,然后使用以下方法上传视频:

private void uploadVideo(String videoPath) throws ParseException, IOException {

    HttpClient httpclient = new DefaultHttpClient();
    HttpPost httppost = new HttpPost(YOUR_URL);

    FileBody filebodyVideo = new FileBody(new File(videoPath));
    StringBody title = new StringBody("Filename: " + videoPath);
    StringBody description = new StringBody("This is a description of the video");

    MultipartEntity reqEntity = new MultipartEntity();
    reqEntity.addPart("videoFile", filebodyVideo);
    reqEntity.addPart("title", title);
    reqEntity.addPart("description", description);
    httppost.setEntity(reqEntity);

    // DEBUG
    System.out.println( "executing request " + httppost.getRequestLine( ) );
    HttpResponse response = httpclient.execute( httppost );
    HttpEntity resEntity = response.getEntity( );

    // DEBUG
    System.out.println( response.getStatusLine( ) );
    if (resEntity != null) {
      System.out.println( EntityUtils.toString( resEntity ) );
    } // end if

    if (resEntity != null) {
      resEntity.consumeContent( );
    } // end if

    httpclient.getConnectionManager( ).shutdown( );
} // end of uploadVideo( )

一旦你有了它,你可能想把它放在一个线程中并添加一个上传对话框,但这会让你开始。我在尝试upload2Server()方法失败后为我工作。这也适用于图像和音频,只需稍加调整即可。