如何使用多部分帖子实体上传视频?

时间:2013-02-12 16:28:11

标签: android

我使用我在某些页面中找到的代码,但我只能将我的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();
}

还有什么方法可以让这项工作上传视频吗?

1 个答案:

答案 0 :(得分:0)

此代码没有问题,上传视频和图片的工作正常,问题是我错过了名称中的文件扩展名,所以当视频上传时,它应该是NAME.EXTENSION,不仅仅是NAME。

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