2011-08-03 55 views
14

我在android中通過org.apache.http.client.HttpClient上傳文件,我需要實現進度條。是否有可能從以下方面獲得進展?如何在android中實現文件上傳進度條

HttpPost httppost = new HttpPost("some path"); 
HttpClient httpclient = new DefaultHttpClient(); 
try { 
File file = new File("file path"); 
InputStream in = new BufferedInputStream(new FileInputStream(file)); 
byte[] bArray = new byte[(int) file.length()]; 
in.read(bArray); 
String entity = Base64.encodeToString(bArray, Base64.DEFAULT); 
httppost.setEntity(new StringEntity(entity)); 
HttpResponse response = httpclient.execute(httppost); 
} 

如果不是,請展示另一種方法。謝謝

+2

這篇文章可能是你正在尋找:[Java FileUpload with progress](http://stackoverflow.com/questions/254719/file-upload-with-java-with-progress-bar)。 –

回答

22

你要做的是創建一個AsyncTask,可以爲你處理這個問題,覆蓋onProgressUpdate方法。

這是我在另一個應用程序中使用HttpURLConnection測試過的東西的精簡版本。可能會有一些小的冗餘,我認爲HttpURLConnection可能通常會被人忽視,但這應該起作用。只需通過調用new FileUploadTask().execute()將此類用於您正在使用的任何活動類(在此示例中,我將其稱爲TheActivity)。當然你可能需要調整這個以適應你的應用程序的需求。

private class FileUploadTask extends AsyncTask<Object, Integer, Void> { 

    private ProgressDialog dialog; 

    @Override 
    protected void onPreExecute() { 
     dialog = new ProgressDialog(TheActivity.this); 
     dialog.setMessage("Uploading..."); 
     dialog.setIndeterminate(false); 
     dialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL); 
     dialog.setProgress(0); 
     dialog.show(); 
    } 

    @Override 
    protected Void doInBackground(Object... arg0) { 
     try { 
      File file = new File("file path"); 
      FileInputStream fileInputStream = new FileInputStream(file); 
      byte[] bytes = new byte[(int) file.length()]; 
      fileInputStream.read(bytes); 
      fileInputStream.close(); 

      URL url = new URL("some path"); 
      HttpURLConnection connection = 
        (HttpURLConnection) url.openConnection(); 
      OutputStream outputStream = connection.getOutputStream(); 

      int bufferLength = 1024; 
      for (int i = 0; i < bytes.length; i += bufferLength) { 
       int progress = (int)((i/(float) bytes.length) * 100); 
       publishProgress(progress); 
       if (bytes.length - i >= bufferLength) { 
        outputStream.write(bytes, i, bufferLength); 
       } else { 
        outputStream.write(bytes, i, bytes.length - i); 
       } 
      } 
      publishProgress(100); 

      outputStream.close(); 
      outputStream.flush(); 

      InputStream inputStream = connection.getInputStream(); 
      // read the response 
      inputStream.close(); 

     } catch (MalformedURLException e) { 
      e.printStackTrace(); 
     } catch (IOException e) { 
      e.printStackTrace(); 
     } 
     return null; 
    } 

    @Override 
    protected void onProgressUpdate(Integer... progress) { 
     dialog.setProgress(progress[0]); 
    } 

    @Override 
    protected void onPostExecute(Void result) { 
     try { 
      dialog.dismiss(); 
     } catch(Exception e) { 
     } 

    } 

} 
+1

這個效率並不高,因爲它直到100進展順利,然後在該對話實際上被傳輸時取消關閉之後卡住了100%。所以上面的代碼中缺少一些東西? – Hunt

+8

當您刷新outputStream時,實際上傳將開始。 所以進度是虛假的進展 – Basbous

+3

不要忘記:獲得OutputStream之前的'connection.setDoOutput(true)'! – Nolesh

3

我不認爲HttpURLConnection類簡單的伎倆,如@Basbous指出,實際數據bufferred直到outputStream.flush()被調用。根據android issue 3164,它現在修復在post-froyo平臺(android 2.2,sdk version 8)中,您需要使用-java.net.HttpURLConnection.setFixedLengthStreamingMode來解決緩衝區行爲。