使用HTTP多部分表单数据的Andr​​oid上传视频到远程服务器 [英] Android upload video to remote server using HTTP multipart form data

查看:161
本文介绍了使用HTTP多部分表单数据的Andr​​oid上传视频到远程服务器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个特定的一块当前项目的烦恼,感觉就像我坚持现在。我试图做一个视频上传与HTTP POST和多部分的表单数据。我觉得我理解HTTP协议和具体多部分表单数据碰了壁。

I'm having trouble on a certain piece of a current project and feel like I'm stuck right now. I'm trying to do a video upload with an HTTP post and multipart form data. I feel like I'm hit a wall in understanding the HTTP protocol and specifically multipart form data.

我要在格式视频上传到<一个网址href="http://videoupload.thecompany.com/VideoApp.xml?method=upload&objectType=person&objectId=777777">http://videoupload.thecompany.com/VideoApp.xml?method=upload&objectType=person&objectId=777777.我还需要包括标题,描述,当然还有videoFile。难道这些多部分数据?

I have a URL to upload videos to in the format http://videoupload.thecompany.com/VideoApp.xml?method=upload&objectType=person&objectId=777777. I also need to include a title, description, and the videoFile of course. Are these the "multipart data"?

我试着适应这种解决方案,以满足我的需求上传的视频由机器人服务器?,并设置以下额外的数据的所有其他conn.setRequestProperty()调用像这样:

I've tried adapting this solution to meet my needs Upload Video from android to server?, and setting the extra data following all the other conn.setRequestProperty() calls like so:

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

但是,这不是为我工作。有从的code原作者评论添加多部分表单数据约30线后,但我不明白为什么。感谢您的帮助。

But this isn't working for me. There's a comment from the original author of the code to add multipart form data about 30 lines later, but I don't understand why. Thanks for any help.

推荐答案

下面是我想出了两步解决方案,主要是从找到的信息和链接<一个href="http://stackoverflow.com/questions/3360957/how-use-multipart-form-data-upload-picture-image-on-android">here.该解决方案是我更容易掌握比一些相关的SO职位upload2server()方法。希望这可以帮助别人。

Here's the two step solution I came up with, largely from the information and links found here. This solution was easier for me to grasp than the upload2server() method in some of the related SO posts. Hope this helps someone else.

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

1) Select the video file from the gallery.

创建一个变量私有静态最终诠释SELECT_VIDEO = 3; - 它不会不管你用什么号码,只要这是一个你检查以后。然后,使用一个意图来选择一个视频。

Create a variable private static final int SELECT_VIDEO = 3; -- it doesn't matter what number you use, so long as that's to one you check for later on. Then, use an intent to select a video.

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

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

Use onActivityResult() to start the uploadVideo() method.

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的罐子,将其添加到您的项目,并用下面的方法上传视频:

2) Go to http://hc.apache.org/downloads.cgi, download the latest HttpClient jar, add it to your project, and upload the video using the following method:

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()方法。这也将适用于图像和音频进行了一些小的调整。

Once you have it working you'll probably want to put it in a thread and add an uploading dialog, but this will get you started. Working for me after I unsuccessfully tried the upload2Server() method. This will also work for images and audio with some minor tweaking.

这篇关于使用HTTP多部分表单数据的Andr​​oid上传视频到远程服务器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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