0

I am trying to upload large video files to the server. I wrote a piece of code which works well for the image so I thought I should work it for the video too.

I wrote the below code.

public int uploadFile(String sourceFileUri) {


    String fileName = sourceFileUri;
    //Log.v("ONMESSAGE", "File type is " + filetype + "File name is " + fileName);
    HttpURLConnection conn = null;
    DataOutputStream dos = null; 
    String lineEnd = "\r\n";
    String twoHyphens = "--";
    String boundary = "*****";
    int bytesRead, bytesAvailable, bufferSize;
    byte[] buffer;
    int maxBufferSize = 1 * 1024 * 1024;
    File sourceFile = new File(sourceFileUri);

    if (!sourceFile.isFile()) {             
         dialog.dismiss();         
         runOnUiThread(new Runnable() {
             public void run() {
                Toast.makeText(GcmActivity.this, "File not found", Toast.LENGTH_LONG).show();
             }
         });

         return 0;

    }
    else
    {
         try {                 
               // open a URL connection to the Servlet
             FileInputStream fileInputStream = new FileInputStream(sourceFile);
             URL url = new URL("http://example.com/ccs-business/upload.php");
             conn = (HttpURLConnection) url.openConnection();
             conn.setDoInput(true); 
             conn.setDoOutput(true); 
             conn.setUseCaches(false); 
             conn.setRequestMethod("POST");
             conn.setRequestProperty("Connection", "Keep-Alive");
             conn.setRequestProperty("ENCTYPE", "multipart/form-data");
             conn.setRequestProperty("Content-Type", "multipart/form-data;boundary=" + boundary);                 
             conn.setRequestProperty("uploaded_file", fileName);                
             dos = new DataOutputStream(conn.getOutputStream());        
             dos.writeBytes(twoHyphens + boundary + lineEnd);
             //dos.writeBytes("Content-Disposition: form-data; name="uploaded_file";filename="+ fileName + """ + lineEnd);
             dos.writeBytes("Content-Disposition: form-data; name=\"uploaded_file\";filename=\"" + fileName +"\"" + lineEnd);
             dos.writeBytes(lineEnd);
             bytesAvailable = fileInputStream.available();

             bufferSize = Math.min(bytesAvailable, maxBufferSize);//1
             buffer = new byte[bufferSize];//2
             bytesRead = fileInputStream.read(buffer, 0, bufferSize); //3

             while (bytesRead > 0) {    //4                  
               dos.write(buffer, 0, bufferSize);//5
               bytesAvailable = fileInputStream.available();//6
               bufferSize = Math.min(bytesAvailable, maxBufferSize);//7
               bytesRead = fileInputStream.read(buffer, 0, bufferSize);//8 

              }//9

             // send multipart form data necesssary after file data...
             dos.writeBytes(lineEnd);
             dos.writeBytes(twoHyphens + boundary + twoHyphens + lineEnd);

             // Responses from the server (code and message)
             serverResponseCode = conn.getResponseCode();
             String serverResponseMessage = conn.getResponseMessage();

             Log.i("uploadFile", "HTTP Response is : "
                     + serverResponseMessage + ": " + serverResponseCode);

             if(serverResponseCode == 200){

                 runOnUiThread(new Runnable() {
                      public void run() {                             

                          Toast.makeText(GcmActivity.this, "File Upload Complete.",
                                       Toast.LENGTH_SHORT).show();
                          dialog.dismiss();
                      }
                  });               
             }   
             fileInputStream.close();
             dos.flush();
             dos.close();

        } catch (MalformedURLException ex) {

            dialog.dismiss(); 
            ex.printStackTrace();

            runOnUiThread(new Runnable() {
                public void run() {
                    Toast.makeText(GcmActivity.this, "MalformedURLException",
                                                        Toast.LENGTH_SHORT).show();
                }
            });

            Log.v("ONMESSAGE", "error: " + ex.getMessage(), ex); 
        } catch (Exception e) {

            dialog.dismiss(); 
            e.printStackTrace();

            runOnUiThread(new Runnable() {
                public void run() {

                    Toast.makeText(GcmActivity.this, "Got Exception : see logcat ",
                            Toast.LENGTH_SHORT).show();
                }
            });
            Log.v("ONMESSAGE", "Exception : "
                                             + e.getMessage(), e); 
        }
        dialog.dismiss();      
        return serverResponseCode;

     } // End else block
   }

This piece of code gives me java.lang.OutOfMemory error so I followed other's suggestion and added a key to manifest that is largeheap, did not work. so I followed other suggestion and changed the code to below

public int uploadFileVideo(String sourceFileUri) {


    String fileName = sourceFileUri;
    //Log.v("ONMESSAGE", "File type is " + filetype + "File name is " + fileName);
    HttpURLConnection conn = null;
    DataOutputStream dos = null; 
    String lineEnd = "\r\n";
    String twoHyphens = "--";
    String boundary = "*****";
    int bytesRead, bytesAvailable, bufferSize;
    byte[] buffer;
    int maxBufferSize = 1 * 1024 * 1024;
    File sourceFile = new File(sourceFileUri);

    if (!sourceFile.isFile()) {             
         dialog.dismiss();         
         runOnUiThread(new Runnable() {
             public void run() {
                Toast.makeText(GcmActivity.this, "File not found", Toast.LENGTH_LONG).show();
             }
         });

         return 0;

    }
    else
    {
         try {                 
               // open a URL connection to the Servlet
             FileInputStream fileInputStream = new FileInputStream(sourceFile);
             URL url = new URL("http://example.com/ccs-business/upload.php");
             conn = (HttpURLConnection) url.openConnection();
             conn.setDoInput(true); 
             conn.setDoOutput(true); 
             conn.setUseCaches(false); 
             conn.setRequestMethod("POST");
             conn.setRequestProperty("Connection", "Keep-Alive");
             conn.setRequestProperty("ENCTYPE", "multipart/form-data");
             conn.setRequestProperty("Content-Type", "multipart/form-data;boundary=" + boundary);                 
             conn.setRequestProperty("uploaded_video", fileName);                
             dos = new DataOutputStream(conn.getOutputStream());        
             dos.writeBytes(twoHyphens + boundary + lineEnd);
             //dos.writeBytes("Content-Disposition: form-data; name="uploaded_file";filename="+ fileName + """ + lineEnd);
             dos.writeBytes("Content-Disposition: form-data; name=\"uploaded_video\";filename=\"" + fileName +"\"" + lineEnd);
             dos.writeBytes(lineEnd);
             bytesAvailable = fileInputStream.available();

            /* bufferSize = Math.min(bytesAvailable, maxBufferSize);
             buffer = new byte[bufferSize];
             bytesRead = fileInputStream.read(buffer, 0, bufferSize); */



             bufferSize = Math.min(bytesAvailable, maxBufferSize);
             buffer = new byte[bufferSize];
             byte byt[]=new byte[bufferSize];

             fileInputStream.read(byt);

             bytesRead = fileInputStream.read(buffer, 0, bufferSize);

             dos.write(buffer, 0, bufferSize);



             // send multipart form data necesssary after file data...
             dos.writeBytes(lineEnd);
             dos.writeBytes(twoHyphens + boundary + twoHyphens + lineEnd);

             // Responses from the server (code and message)
             serverResponseCode = conn.getResponseCode();
             String serverResponseMessage = conn.getResponseMessage();

             Log.i("uploadFile", "HTTP Response is : "
                     + serverResponseMessage + ": " + serverResponseCode);

             if(serverResponseCode == 200){

                 runOnUiThread(new Runnable() {
                      public void run() {                             

                          Toast.makeText(GcmActivity.this, "File Upload Complete.",
                                       Toast.LENGTH_SHORT).show();
                          dialog.dismiss();
                      }
                  });               
             }   
             fileInputStream.close();
             dos.flush();
             dos.close();

        } catch (MalformedURLException ex) {

            dialog.dismiss(); 
            ex.printStackTrace();

            runOnUiThread(new Runnable() {
                public void run() {
                    Toast.makeText(GcmActivity.this, "MalformedURLException",
                                                        Toast.LENGTH_SHORT).show();
                }
            });

            Log.v("ONMESSAGE", "error: " + ex.getMessage(), ex); 
        } catch (Exception e) {

            dialog.dismiss(); 
            e.printStackTrace();

            runOnUiThread(new Runnable() {
                public void run() {

                    Toast.makeText(GcmActivity.this, "Got Exception : see logcat ",
                            Toast.LENGTH_SHORT).show();
                }
            });
            Log.v("ONMESSAGE", "Exception : "
                                             + e.getMessage(), e); 
        }
        dialog.dismiss();      
        return serverResponseCode;

     } // End else block
   }

Now files gets uploaded however all file size of 1 MB and that too can not be played.

halfer
  • 19,824
  • 17
  • 99
  • 186
Saty
  • 2,563
  • 3
  • 37
  • 88

2 Answers2

0
bufferSize = Math.min(bytesAvailable, maxBufferSize);
buffer = new byte[bufferSize];
byte byt[]=new byte[bufferSize];

fileInputStream.read(byt);

You are not writing the bytes read into byt out to the outputstream. Removing the first read into byt should fix a problem. But you will come across another problem where a file over 1MB only the first 1MB will be uploaded.

A proper way to copy a stream is to do something similar to

byte[] buf = new byte[ 1024 ];
int read = 0;
while( ( read = in.read( buf ) ) != -1 ) {
    out.write( buf, 0, read );
}
Smith_61
  • 2,078
  • 12
  • 12
  • could you please let me know the complete code..I am not atall good at this file input streaming – Saty Aug 13 '14 at 07:11
  • Basically you will need to replace lines you labeled 1-9 with something similar to what I posted in my second code block. You will need to change variable names though, as in "in" would be "fileInputStream" and "out" would be "dos". – Smith_61 Aug 13 '14 at 07:18
  • Yeah I just changed after reading and it seems to be working. I am trying to upload a very large file now and will let you know. Thanks for all your help. – Saty Aug 13 '14 at 07:24
  • You will also want to clean up the code a little bit. You are leaving streams open at the end of the method, and you will have a few unused variables and redundant/not needed code. None of that should keep it from working though. – Smith_61 Aug 13 '14 at 07:26
0

avoid buffering the whole POST data in RAM by adding conn.setChunkedStreamingMode(0); after : con.setDoOutput(true); hope this help :)

Linh Nguyen
  • 1,264
  • 1
  • 10
  • 22