2014-03-03 76 views
1

iam嘗試通過ftp將文件(大小)上傳到我的服務器。ProgressDialog當通過ftp從Android設備上傳文件

ftp的上傳部分工作正常。

現在我需要.. 1)progressDialog顯示傳輸(上傳)的數據。 2)如果可能的話,ETA時間要一起顯示。

發佈我到目前爲止所做的。

private class uploadFileTask extends AsyncTask<String, Integer, Long> { 

    //void FTP_DATA_UPLOAD(String FULL_PATH_TO_LOCAL_FILE) 
    private ProgressDialog progressDialog; 
    int progressInput = 0; 
    @Override 
    protected void onPreExecute() { 
     super.onPreExecute(); 
     progressDialog = new ProgressDialog(MainActivity.this); 
     progressDialog.setTitle("Upload file"); 
     progressDialog.setMessage("Sending file please wait..."); 
     progressDialog.setCancelable(false); 
     progressDialog.setIndeterminate(false); 
     progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL); 
     progressDialog.setProgress(0); 
     progressDialog.setMax((int)uploadFilePath1.length()); 
     progressDialog.incrementProgressBy((int)((progressInput*100)/(uploadFilePath1.length()))); 
     progressDialog.show(); 
    } 

    protected Long doInBackground(String... FULL_PATH_TO_LOCAL_FILE) { 
     // encapsulate FTP inside a A sync task 

     { 
      System.out.println("Entered FTP transfer function"); 

      FTPClient ftpClient = new FTPClient(); 
      int reply; 
      try { 
       System.out.println("Entered Data Upload loop!"); 
       ftpClient.connect("103.27.**.***",21); 
       ftpClient.login("send", "gelvin"); 
       //ftpClient.changeWorkingDirectory("/directory/"); 
       System.out.println("Entered Data Upload loop!"); 


       int reply1 = ftpClient.getReplyCode(); 


       if(FTPReply.isPositiveCompletion(reply1)){ 
        System.out.println("Connected Success"); 
       }else { 
        System.out.println("Connection Failed"); 
        ftpClient.disconnect(); 
       } 




       ftpClient.setFileType(org.apache.commons.net.ftp.FTP.BINARY_FILE_TYPE); 
       BufferedInputStream buffIn = null; 
       System.out.println("Created an input stream buffer"); 
       System.out.println(FULL_PATH_TO_LOCAL_FILE.toString()); 

       buffIn = new BufferedInputStream(new FileInputStream(uploadFilePath1)); 
       ftpClient.enterLocalPassiveMode(); 

       System.out.println("Entered binary and passive modes"); 
       // Handler progressHandler=null; 
       //ProgressInputStream progressInput = new ProgressInputStream(buffIn, progressHandler); 

       result = ftpClient.storeFile(uploadFileName, buffIn); 



       //boolean result = ftpClient.storeFile(uploadFileName, buffIn); //localAsset.getFileName() 
       //ProgressInputStream progressInput = new ProgressInputStream(buffIn, progressHandler); 

       if (result){ 
        System.out.println("Success"); 
       } 

       //boolean result = ftpClient.storeFile("TEST.jpg", progressInput); 
       System.out.println("File saved"); 


       buffIn.close(); 
       ftpClient.logout(); 
       ftpClient.disconnect(); 


      } catch (SocketException e) { 
       Log.e("SocketException", e.getStackTrace().toString()); 
       System.out.println("Socket Exception!"); 
      } catch (UnknownHostException e) { 
       Log.e("UnknownHostException", e.getStackTrace().toString()); 
      } catch (IOException e) { 
       Log.e("IOException", e.getStackTrace().toString()); 
       System.out.println("IO Exception!"); 
      } 

      return null; 
     } 


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



    @Override 
    protected void onPostExecute(Long result) { 
     progressDialog.dismiss(); 
    } 
} 

請幫我這個

+0

你的問題是什麼?什麼不起作用? 「請幫助我」通常不是要走的路。 –

+0

@PhilippJahoda iam嘗試通過ftp將文件(小和大)上傳到我的服務器。 ftp的上傳部分工作正常。 現在我需要.. 1)progressDialog顯示傳輸(上傳)的數據。 2)如果可能的話,ETA時間要一起顯示。 –

回答

0

您可以自定義對話框中,你可以參考以下鏈接

http://custom-android-dn.blogspot.in/2013/01/how-to-create-custom-progress-bar-and.html

Android change Horizonal Progress bar color

我認爲這可能是幫助你。

這裏是custome類的完整代碼,

http://www.learn-android-easily.com/2013/05/custom-progress-bar-in-android.html

你只打開異步任務的preExecute方法對話框,並關閉到postExecute方法。

+0

嘿感謝那些更新,但我希望它在asynctask內工作... –

+0

你可以做到這一點,我已經寫了一行。 – Solution