从Android设备通过ftp上传文件时的ProgressDialog

时间:2014-03-03 13:13:29

标签: android apache ftp

我试图通过ftp将文件(小型和大型)上传到我的服务器。

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

请帮我解决这个问题

1 个答案:

答案 0 :(得分:0)

您可以自定义对话框,您可以参考以下链接

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

Android change Horizonal Progress bar color

我认为这对你有所帮助。

以下是客户类的完整代码,

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

您必须在Async任务的preExecute方法中打开该对话框并将其关闭为postExecute方法。