0

I am trying video based application. I am using below method to upload video file, when I am sending large size of video file it's getting more time to upload. So I need to compress the data and also video file size should be less than 30 MB.I am bad in English forgive me. Can anyone give me the solution for my question?

 private void doFileUpload(){
    HttpURLConnection conn = null;
    DataOutputStream dos = null;
    DataInputStream inStream = null;
    String lineEnd = "rn";
    String twoHyphens = "--";
    String boundary =  "*****";
    int bytesRead, bytesAvailable, bufferSize;
    byte[] buffer;
    int maxBufferSize = 1*1024*1024;
    String responseFromServer = "";
    String urlString = "http://mywebsite.com/upload_audio.php";
    try
    {
     //------------------ CLIENT REQUEST
    FileInputStream fileInputStream = new FileInputStream(new File(selectedPath) );
     // open a URL connection to the Servlet
     URL url = new URL(urlString);
     // Open a HTTP connection to the URL
     conn = (HttpURLConnection) url.openConnection();
     // Allow Inputs
     conn.setDoInput(true);
     // Allow Outputs
     conn.setDoOutput(true);
     // Don't use a cached copy.
     conn.setUseCaches(false);
     // Use a post method.
     conn.setRequestMethod("POST");
     conn.setRequestProperty("Connection", "Keep-Alive");
     conn.setRequestProperty("Content-Type", "multipart/form-data;boundary="+boundary);
     dos = new DataOutputStream( conn.getOutputStream() );
     dos.writeBytes(twoHyphens + boundary + lineEnd);
     dos.writeBytes("Content-Disposition: form-data; name="uploadedfile";filename="" + selectedPath + """ + lineEnd);
     dos.writeBytes(lineEnd);
     // create a buffer of maximum size
     bytesAvailable = fileInputStream.available();
     bufferSize = Math.min(bytesAvailable, maxBufferSize);
     buffer = new byte[bufferSize];
     // read file and write it into form...
     bytesRead = fileInputStream.read(buffer, 0, bufferSize);
     while (bytesRead > 0)
     {
      dos.write(buffer, 0, bufferSize);
      bytesAvailable = fileInputStream.available();
      bufferSize = Math.min(bytesAvailable, maxBufferSize);
      bytesRead = fileInputStream.read(buffer, 0, bufferSize);
     }
     // send multipart form data necesssary after file data...
     dos.writeBytes(lineEnd);
     dos.writeBytes(twoHyphens + boundary + twoHyphens + lineEnd);
     // close streams
     Log.e("Debug","File is written");
     fileInputStream.close();
     dos.flush();
     dos.close();
    }
    catch (MalformedURLException ex)
    {
         Log.e("Debug", "error: " + ex.getMessage(), ex);
    }
    catch (IOException ioe)
    {
         Log.e("Debug", "error: " + ioe.getMessage(), ioe);
    }
    //------------------ read the SERVER RESPONSE
    try {
          inStream = new DataInputStream ( conn.getInputStream() );
          String str;

          while (( str = inStream.readLine()) != null)
          {
               Log.e("Debug","Server Response "+str);
          }
          inStream.close();

    }
    catch (IOException ioex){
         Log.e("Debug", "error: " + ioex.getMessage(), ioex);
    }
  }
sshashank124
  • 31,495
  • 9
  • 67
  • 76
Mathankumar K
  • 117
  • 2
  • 8
  • possible duplicate of [Upload Video from android to server?](http://stackoverflow.com/questions/5017093/upload-video-from-android-to-server) – Rishabh Srivastava May 18 '15 at 07:58

1 Answers1

0

You can try HttpClient jar download the latest HttpClient jar, add it to your project, and upload the video using the following method:

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 video of the agent");
StringBody code = new StringBody(realtorCodeStr);

MultipartEntity reqEntity = new MultipartEntity();
reqEntity.addPart("videoFile", filebodyVideo);
reqEntity.addPart("title", title);
reqEntity.addPart("description", description);
reqEntity.addPart("code", code);
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( )
Vinay Jayaram
  • 1,030
  • 9
  • 29