我在当前项目的某个部分遇到了麻烦,感觉我现在被困住了。我正在尝试使用 HTTP 帖子和多部分表单数据上传视频。我觉得我在理解 HTTP 协议(protocol)和特别是多部分表单数据方面遇到了瓶颈。
我有一个可以上传视频的 URL,格式为 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 行后有评论添加多部分表单数据,但我不明白为什么。感谢您的帮助。
最佳答案
这是我提出的两步解决方案,主要来自找到的信息和链接 here .这个解决方案对我来说比一些相关 SO 帖子中的 upload2server() 方法更容易掌握。希望这对其他人有帮助。
1) 从图库中选择视频文件。
创建一个变量private static final int SELECT_VIDEO = 3;
——你使用什么数字并不重要,只要它是你稍后检查的那个。然后,使用 Intent 选择视频。
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() 方法失败后为我工作。通过一些小的调整,这也适用于图像和音频。
关于Android使用HTTP多部分表单数据上传视频到远程服务器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11164398/