java - 如何在android中显示FTP下载的进度条更新

标签 java android android-intent

我正在尝试下载 apk 文件来更新我的应用程序,并且 apk 已放置在 ftp 服务器中,并且我正在使用 FTP 客户端下载该 apk。

即使我调用mProgress.setProgress(percent); ProgressBar 没有从我通过 ftp 下载 apk 文件的函数中更新

public class UpdateAppByFTP extends AsyncTask<String,Void,Void> {
private Context context;
CopyStreamAdapter streamListener;
public void setContext(Context mContext){
    context = mContext;
}
private ProgressDialog mProgress;
@Override
protected void onPreExecute(){
    super.onPreExecute();
    mProgress = new ProgressDialog(this.context);
    mProgress.setMessage("Downloading new apk .. Please wait...");
    mProgress.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
    //mProgress.setIndeterminate(true);
    mProgress.show();
}
@Override
protected void onPostExecute(Void aVoid) {
    super.onPostExecute(aVoid);
    mProgress.dismiss(); //Dismiss the above Dialogue
}
@Override
protected Void doInBackground(String... arg0) {
    try {
        String serverName       = arg0[0];
        String userName         = arg0[1];
        String password         = arg0[2];
        String serverFilePath   = arg0[3];
        String localFilePath    = arg0[4];            if(getFileByFTP(serverName,userName,password,serverFilePath,localFilePath)){
            Intent intent = new Intent(Intent.ACTION_VIEW);
            intent.setDataAndType(Uri.fromFile(new File(localFilePath)), "application/vnd.android.package-archive");
            intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); // without this flag android returned a intent error!
            context.startActivity(intent);
        }else{
            //Do nothing could not download
        }
        String apkLocation="/download/"+"SmartPOS.apk";
        Intent intent1 = new Intent(Intent.ACTION_VIEW);
        intent1.setDataAndType(Uri.fromFile(new File(Environment.getExternalStorageDirectory() +apkLocation)), "application/vnd.android.package-archive");
        intent1.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); // without this flag android returned a intent error!
        context.startActivity(intent1);
    } catch (Exception e) {
    }
    return null;
}
//Below code to download using FTP
public  boolean getFileByFTP(String serverName, String userName,
                                   String password, String serverFilePath, String localFilePath)
        throws Exception {
    FTPClient ftp = new FTPClient();
    try {
        ftp.connect(serverName);
        int reply = ftp.getReplyCode();
        if (!FTPReply.isPositiveCompletion(reply)) {
            ftp.disconnect();
            return false;
        }
    } catch (IOException e) {
        if (ftp.isConnected()) {
            try {
                ftp.disconnect();
            } catch (IOException f) {
                throw e;
            }
        }
        throw e;
    } catch (Exception e) {
        throw e;
    }
    try {
        if (!ftp.login(userName, password)) {
            ftp.logout();
        }
        ftp.setFileType(FTPClient.BINARY_FILE_TYPE);
        ftp.enterLocalPassiveMode();
        final int lenghtOfFile =(int)getFileSize(ftp,serverFilePath); 
        OutputStream output = new FileOutputStream(localFilePath);
        CountingOutputStream cos = new CountingOutputStream(output) {
            protected void beforeWrite(int n) {
                super.beforeWrite(n);
                int percent = Math.round((getCount() * 100) / lenghtOfFile);
                Log.d("FTP_DOWNLOAD", "bytesTransferred /downloaded"+percent);
                System.err.println("Downloaded "+getCount() + "/" + percent);
                mProgress.setProgress(percent);
            }
        };
        ftp.setBufferSize(2024*2048);//To increase the  download speed
        ftp.retrieveFile(serverFilePath, output);
        output.close();
        ftp.noop(); // check that control connection is working OK
        ftp.logout();
        return true;
    }
    catch (FTPConnectionClosedException e) {
        Log.d("FTP_DOWNLOAD", "ERROR FTPConnectionClosedException:"+e.toString());
        throw e;
    } catch (IOException e) {
        Log.d("FTP_DOWNLOAD", "ERROR IOException:"+e.toString());
        throw e;
    } catch (Exception e) {
        Log.d("FTP_DOWNLOAD", "ERROR Exception:"+e.toString());
        throw e;
    } finally {
        if (ftp.isConnected()) {
            try {
                ftp.disconnect();
            } catch (IOException f) {
                throw f;
            }
        }
    }
}
private static long getFileSize(FTPClient ftp, String filePath) throws Exception {
    long fileSize = 0;
    FTPFile[] files = ftp.listFiles(filePath);
    if (files.length == 1 && files[0].isFile()) {
        fileSize = files[0].getSize();
    }
    Log.d("FTP_DOWNLOAD", "File size = " + fileSize);
    return fileSize;
 }
 }

基本上,UI 不会更新,而且我不确定 CountingOutputStream 是否是查找文件下载大小的正确方法。

提前致谢。

最佳答案

我更改了代码的retrieveFile部分,现在没问题了

ftp.retrieveFile(serverFilePath, cos);

关于java - 如何在android中显示FTP下载的进度条更新,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56579462/

相关文章:

java - Debug模式下的 Docker 和 jre 11

java - Android:onTouchEvent启动新 Activity

来自 onCreate 的 Android 启动 Intent

android - 如何在android中使用wifi访问我的网络中的打印机

Java多线程在单元测试中不更新值

java - 防止 JBoss 7 (Jeeves DBMS) 上 JNDI 数据源的 Oracle 连接自动提交

java - jpype 和 java.util.Properties

android - 更改 Espresso 的注入(inject)模块

java - 为什么使用 Android 插件的 Jenkins 多配置作业失败?

android - 如何读取android中xmpp消息标签的自定义属性?