Resolving java.lang.outofmemory error causing half of the file to be uploaded to server

佐手、 提交于 2020-01-17 00:40:50

问题


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.


回答1:


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 );
}



回答2:


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



来源:https://stackoverflow.com/questions/25280006/resolving-java-lang-outofmemory-error-causing-half-of-the-file-to-be-uploaded-to

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!