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

前端之家收集整理的这篇文章主要介绍了Android使用HTTP多部分表单数据将视频上传到远程服务器前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我在当前项目的某个部分遇到了麻烦,觉得我现在被卡住了.我正在尝试使用HTTP帖子和多部分表单数据进行视频上传.我觉得我在理解HTTP协议,特别是多部分表单数据方面遇到了障碍.

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

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

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

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

解决方法

这是我提出的两步解决方案,主要来自于 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);
    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()方法失败后为我工作.这也适用于图像和音频,只需稍微调整一下.

原文链接:https://www.f2er.com/android/316239.html

猜你在找的Android相关文章