2013-07-08 3 views
1

Android 기기에서 서버에 대용량 zip 파일 (약 10MB 이상)을 업로드해야합니다. 그것은 "OutOfMemoryError가"를 던졌습니다 10메가바이트 이상 업로드하는 동안 나는 8메가바이트 적은 다음 업로드 할 수 있습니다, 그래서 난 내 코드에
conn.setFixedLengthStreamingMode(fileInputStream.available());을 추가, 또한 다음과 같은 예외Android : 대형 파일을 Android에서 업로드하지 못했습니다.

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 파일

내가 나에게 최선의 방법에 관해서는 제안을 줄 수 chunks.If 누군가에 업로드해야
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()); 

업로드, 나는 매우 감사하게 될 것입니다. 사전에

덕분 점 이하

+0

코드를 파괴에 널 쓰레기는 매우 혼란 보인다. 직면 한 문제는 데이터보다 큰 버퍼를 할당했다는 것입니다 (int maxBufferSize = 4 * 1024;). 언제든지 당신은 뭔가 복잡한 일을해야하고, Google은 그것을 할 수있는 오픈 소스 라이브러리가 있는지 찾아냅니다. http://loopj.com/android-async-http/를 통합하지 않는 이유는 무엇입니까? (RequestParams로 파일 업로드하기 섹션 참조) –

+0

늦게 응답하여 죄송합니다. 여기에 네트워크 문제가 있습니다. 나는 너의 제안을 시도 할 것이다. 사실 내 필요는 배경 서비스에서 압축 형식으로 선택한 사진 (최대 120 장의 사진)을 업로드하고 현재 업로드 진행률을 백분율로 얻습니다. – AndHobbiest

답변

0

희망은 서버에 데이터를 업로드하는 당신이 -

1)을 사용하여 POST 방법을 도움이 될 것입니다.

2)

3를 사용하여 멀티 파트 이미지) 활동

관련 문제