简体   繁体   English

Android FTP下载进度栏

[英]Android FTP DownLoad Progressbar

FTP code, but only the download... FTP代码,但只有下载...

public class ImageUploadTask extends AsyncTask <String, Void, String>{
String e;


protected String doInBackground(String...args) {
    String strResponce = "";
    ftpClient = new FTPClient();
    s="finish";
    try {

        ftpClient.connect(InetAddress.getByName(SERVER));
        ftpClient.login(USERNAME, PASSWORD);
        ftpClient.setFileType(FTP.BINARY_FILE_TYPE);

        textTargetUri.setText("good");


        final String remote = "/1.jpg";//FTP adress/filename
        String savefilepath = "/sdcard/download"+remote;
        File downloadfile = new File(savefilepath);//download

        local = new FileOutputStream(downloadfile);

        ftpClient.retrieveFile(remote, local);

        local.close();
        ftpClient.disconnect();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    finally{

    }
    return s;
}
}

BUT!! 但!! FTP DownLoad code do insert CountingOutputStream function. FTP下载代码确实插入了CountingOutputStream函数。 CountingOutputStream function is commons-io-2.4.jar . CountingOutputStream函数是commons-io-2.4.jar project in commons-io-2.4.jar and commons-net-3.3.jar to there. commons-io-2.4.jarcommons-net-3.3.jar进行项目。 T_T T_T

public class ImageUploadTask extends AsyncTask <String, Void, String>{
String e;


protected String doInBackground(String...args) {
    String strResponce = "";
    ftpClient = new FTPClient();
    s="finish";
    try {

        ftpClient.connect(InetAddress.getByName(SERVER));
        ftpClient.login(USERNAME, PASSWORD);
        ftpClient.setFileType(FTP.BINARY_FILE_TYPE);

        textTargetUri.setText("good");

        final String remote = "/1.jpg";//FTP adress/filename
        String savefilepath = "/sdcard/download"+remote;
        File downloadfile = new File(savefilepath);//download

        local = new FileOutputStream(downloadfile);
        long fileSize = 0;
        FTPFile[] files = ftpClient.listFiles(remote);
        if (files.length == 1 && files[0].isFile()) {
            fileSize = files[0].getSize();

            String a = String.valueOf(fileSize);
            Log.d("File Size", a);

        }

        CountingOutputStream cos = new CountingOutputStream(local) {
              protected void beforeWrite(int n) {
                      super.beforeWrite(n);

                      int progress = Math.round((getCount() * 100) / 879394);
                      //String b = String.valueOf(progress);
                      //Log.d("File persent", b);

              }
        };
        ftpClient.retrieveFile(remote, cos);

        local.close();
        ftpClient.disconnect();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } finally{

    }
    return s;
}


    }

What's wrong with the code, I do not know how? 代码有什么问题,我不知道怎么办?

code no error. 代码没有错误。

but APP start error. 但是APP启动错误。

In a background tread (such as in doInBackGround() ) no UI elements can be changed at all. 在背景区域中(例如在doInBackGround() ),根本无法更改UI元素。 If in activity use runOnUiThread or else use textTargetUri.post . 如果处于活动状态,请使用runOnUiThread ,否则请使用textTargetUri.post This way the setText part takes place on the UI thread as needed. 这样,setText部分会根据需要在UI线程上发生。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM