2013-02-12 46 views
0

我使用此代码,我发现在一些页面,但我只能从我的Android应用程序上传图像到服务器,并正在工作,但是当我上传一个视频(.mp4)它保存为“文件”像未知。如何使用多部分邮政实体上传视频?

public void upload() throws Exception { 
    //Url of the server 
    String url = ""; 
    HttpClient client = new DefaultHttpClient(); 
    HttpPost post = new HttpPost(url); 
    MultipartEntity mpEntity = new MultipartEntity(); 
    //Path of the file to be uploaded 
    String filepath = ""; 
    File file = new File(filepath); 
    ContentBody cbFile = new FileBody(file);   

    //Add the data to the multipart entity 
    mpEntity.addPart("image", cbFile); 
    mpEntity.addPart("name", new StringBody("Test", Charset.forName("UTF-8"))); 
    mpEntity.addPart("data", new StringBody("This is test report", Charset.forName("UTF-8"))); 
    post.setEntity(mpEntity); 
    //Execute the post request 
    HttpResponse response1 = client.execute(post); 
    //Get the response from the server 
    HttpEntity resEntity = response1.getEntity(); 
    String Response=EntityUtils.toString(resEntity); 
    Log.d("Response:", Response); 
    //Generate the array from the response 
    JSONArray jsonarray = new JSONArray("["+Response+"]"); 
    JSONObject jsonobject = jsonarray.getJSONObject(0); 
    //Get the result variables from response 
    String result = (jsonobject.getString("result")); 
    String msg = (jsonobject.getString("msg")); 
    //Close the connection 
    client.getConnectionManager().shutdown(); 
} 

有什么办法可以使这项工作上传视频吗?

回答

0

这段代码没有问题,适用于上传视频和图片,问题是我缺少名称中的文件扩展名,所以当视频上传时,它应该是NAME.EXTENSION不只是名称。

NOTE: 对于所有尝试上传大型(2MB-10GB)图像或视频到服务器的人来说,我发现的唯一解决方案是将文件分块编码并将每个块上传到服务器,从那里你只需要再次对块进行编码。没有大小限制:)!!!!

+0

请用代码更新您的答案。 – user2251725 2013-07-25 13:19:48

+0

可以请你发布你的最终解决方案,因为在http实体上没有setChunk方法 – Ravi 2013-09-18 08:47:05