2013-02-12 42 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