1

我需要将大型 zip 文件(大约 10 MB 以上)从 Android 设备上传到服务器。我可以上传不到 8 MB ,而尝试上传超过 10 MB 时会抛出“OutOfMemoryError”,所以我添加
conn.setFixedLengthStreamingMode(fileInputStream.available());了我的代码,它还会抛出以下异常

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)

以下代码我用来上传 Zip 文件

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

我需要分块上传。如果有人能给我关于最佳方法的建议,我将非常感激。

提前致谢

4

1 回答 1

0

希望以下几点对您有所帮助-

1)使用POST方法将数据上传到服务器。

2)使用多部分图像

3)销毁您的活动时的空垃圾

于 2013-07-08T10:17:42.973 回答