Android: downloading large files with MultipartEntity

According to this answer, “Android uploads the video to a remote server using HTTP data in several formats” I am doing all the steps.

But I do not know how I encode the server side! I mean a simple PHP page that serves my download using reauest.

And one more question: YOUR_URL (3rd line of the next fragment) should be the address of this PHP page?

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( ); } 
+2
android php upload multipartform-data multipart
May 6 '14 at 20:26
source share
1 answer

This code worked correctly, and the PHP code I have to use is simple like this:

 <?php $file_path = "uploads/"; $file_path = $file_path . basename( $_FILES['videoFile']['name']); if(move_uploaded_file($_FILES['videoFile']['tmp_name'], $file_path)) { echo "success"; } else{ echo "upload_fail_php_file"; } ?> 

NOTE that videoFile must exactly match

reqEntity.addPart("videoFile", filebodyVideo);

And the MOST problem. Important , which you are likely to encounter, is the default value of post_max_size and upload_max_filesize in the server configuration! The default is too small, and when you try to upload large files, the PHP script returns: "upload_fail_php_file" without any errors or exceptions. Therefore, remember to set these values ​​as large enough ...

Enjoy the coding.

+4
May 6 '14 at
source share



All Articles