我有一个以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()方法失败后为我工作.这也适用于图像和音频,只需稍微调整一下.
总结以上是内存溢出为你收集整理的Android使用HTTP多部分表单数据将视频上传到远程服务器全部内容,希望文章能够帮你解决Android使用HTTP多部分表单数据将视频上传到远程服务器所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)