【问题标题】:Android upload video to remote server using HTTP multipart form dataAndroid 使用 HTTP 多部分表单数据将视频上传到远程服务器
【发布时间】:2012-06-25 05:40:38
【问题描述】:

我在当前项目的某个部分遇到问题,感觉自己现在陷入困境。我正在尝试使用 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 行之后添加多部分表单数据,但我不明白为什么。感谢您的帮助。

【问题讨论】:

    标签: android http multipartform-data


    【解决方案1】:

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

    【讨论】:

    • 太好了!这可用于大尺寸视频吗?还是可以用于其他文件类型?
    • @AliBagheriShakib 应该适用于较大的视频。是的,它可以非常相似地用于音频剪辑或其他文件类型。如果您有任何问题,请告诉我。
    • 是的@Kyle Clegg。它对我来说非常有效。我的问题解决了兄弟:)我也发布了我自己的经验:stackoverflow.com/q/23504191/2624611
    • 哦!我忘了问在某些情况下是否可能会出错,因为您的某些代码已被弃用!?
    • 嗨,我已经添加了它,但显示实例错误“java.lang.NoSuchFieldError: org.apache.http.message.BasicHeaderValueFormatter.INSTANCE”请任何帮助
    猜你喜欢
    • 2019-10-08
    • 1970-01-01
    • 2016-06-09
    • 2014-08-27
    • 1970-01-01
    • 1970-01-01
    • 2011-06-28
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多