1

I need to upload large sized zip file (approx above 10 MB) to server from Android device. I can upload less then 8 MB , while trying to upload above 10 MB it throws "OutOfMemoryError", so I add
conn.setFixedLengthStreamingMode(fileInputStream.available()); in my code,It also throws the following Exception

07-08 10:57:22.649: W/System.err(31383): java.io.IOException: expected 860 bytes but received 4096
07-08 10:57:22.649: W/System.err(31383):    at libcore.net.http.FixedLengthOutputStream.write(FixedLengthOutputStream.java:39)
07-08 10:57:22.649: W/System.err(31383):    at java.io.DataOutputStream.write(DataOutputStream.java:98)
07-08 10:57:22.649: W/System.err(31383):    at com.ams.utilities.PhotoUploadAsyncTask.doInBackground(PhotoUploadAsyncTask.java:273)
07-08 10:57:22.649: W/System.err(31383):    at com.ams.utilities.PhotoUploadAsyncTask.doInBackground(PhotoUploadAsyncTask.java:1)
07-08 10:57:22.656: W/System.err(31383):    at android.os.AsyncTask$2.call(AsyncTask.java:287)
07-08 10:57:22.656: W/System.err(31383):    at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
07-08 10:57:22.656: W/System.err(31383):    at java.util.concurrent.FutureTask.run(FutureTask.java:137)
07-08 10:57:22.664: W/System.err(31383):    at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
07-08 10:57:22.672: W/System.err(31383):    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
07-08 10:57:22.688: W/System.err(31383):    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)
07-08 10:57:22.703: W/System.err(31383):    at java.lang.Thread.run(Thread.java:856)

the following code i use to Upload Zip file

DataOutputStream os = null;
HttpsURLConnection conn = null ;

String strBoundary = "---------------------------14737809831466499882746641449";
String endLine = "\r\n";                

conn = (HttpsURLConnection) new URL(url).openConnection();

if(fileInputStream != null)
    conn.setFixedLengthStreamingMode(fileInputStream.available());


conn.setRequestMethod("POST");
conn.setRequestProperty(
    "Content-Type",
    "multipart/form-data;boundary="+strBoundary);
conn.setDoOutput(true);
conn.setDoInput(true);
conn.setRequestProperty("Connection", "Keep-Alive");
conn.setRequestProperty("Accept", "text/xml");
conn.connect();
os = new DataOutputStream(conn.getOutputStream());


os.write(("--" + strBoundary +endLine).getBytes());
os.write((encodePostBody(postData, strBoundary)).getBytes());
os.write((endLine + "--" + strBoundary + endLine).getBytes());


os.write(("Content-Disposition: form-data; name=\"cloud_photos\"; filename=\"cloud_photos.zip\"" + endLine).getBytes());


  os.write(("Content-Type: application/octet-stream" + endLine + endLine).getBytes());


    if(fileInputStream != null){

        long lengthOfFile = zipfilePath.length(); //fileInputStream.available();//

        int bytesAvailable = fileInputStream.available();



        int maxBufferSize = 4 * 1024;

        LogAMS.d(TAG, "bytesAvailable="+bytesAvailable);

        int bufferSize = Math.min(bytesAvailable, maxBufferSize);

        byte[] buffer = new byte[bufferSize];

        int total = 0;

        // read file and write it into form...
        int bytesRead = fileInputStream.read(buffer, 0, bufferSize);


        while (bytesRead > 0){   

            total += bytesRead;
        os.write(buffer, 0, bufferSize);
        bytesAvailable = fileInputStream.available();

        int progress = (int)((total*100) / lengthOfFile);

        if(progress <= 98){
            if(progress % 20 == 0)
                publishProgress(progress);
        }else{

            if(progress == 99)
                publishProgress(progress);
        }

        bufferSize = Math.min(bytesAvailable,maxBufferSize);

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



        }

    }


    }

    os.write((endLine + "--" + strBoundary + endLine).getBytes());

   // fileInputStream.close();

    os.flush();

    os.close();

    os = null;


return parseUploadResponse(conn.getInputStream());

I need to upload it in chunks.If anyone could give me a suggestions as to the best approach, I would be very grateful.

Thanks in advance

AndHobbiest
  • 131
  • 1
  • 1
  • 8
  • Your code looks very confusing. Problem you are facing is that you have allocated larger buffer than your data is ( int maxBufferSize = 4 * 1024;). Anytime you have to do something complicated, google and find out if there is open-source library to do that. Why don't you integrate http://loopj.com/android-async-http/ (refer to section - Uploading Files with RequestParams) – Wand Maker Jul 08 '13 at 10:18
  • Sorry for the late response, here is some network problem. I will try your suggestions. Actually my need is upload selected photo(maximum 120 photo) as zipped format in background service and get current upload progress in percentage. – AndHobbiest Jul 09 '13 at 08:22

1 Answers1

0

Hope below point will help you-

1)Use POST method to upload data on server.

2)Use Multi Part Image

3)Null garbage on destroy your activity

Manish Srivastava
  • 1,649
  • 2
  • 15
  • 23