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

Java 如何在android中显示FTP下载的进度条更新,java,android,android-intent,Java,Android,Android Intent,我试图下载一个apk文件来更新我的应用程序,apk被放在ftp服务器中,我正在使用ftp客户端下载该apk 即使我调用mProgress.setProgress(百分比) ProgressBar没有从我通过ftp下载apk文件的功能中得到更新 public class UpdateAppByFTP extends AsyncTask<String,Void,Void> { private Context context; CopyStreamAdapter streamListene

我试图下载一个apk文件来更新我的应用程序,apk被放在ftp服务器中,我正在使用ftp客户端下载该apk

即使我调用
mProgress.setProgress(百分比)
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;
 }
 }
public类UpdateAppByFTP扩展异步任务{
私人语境;
CopyStreamAdapter streamListener;
public void setContext(Context mContext){
上下文=mContext;
}
私人进程;
@凌驾
受保护的void onPreExecute(){
super.onPreExecute();
mProgress=newprogressdialog(this.context);
设置消息(“正在下载新的apk..请稍候…”);
mProgress.setProgressStyle(ProgressDialog.STYLE_水平);
//mProgress.setUndeterminate(真);
mProgress.show();
}
@凌驾
受保护的void onPostExecute(void避免){
super.onPostExecute(避免);
mProgress.disclose();//取消上面的对话
}
@凌驾
受保护的Void doInBackground(字符串…arg0){
试一试{
字符串serverName=arg0[0];
字符串用户名=arg0[1];
字符串密码=arg0[2];
字符串serverFilePath=arg0[3];
字符串localFilePath=arg0[4];if(getFileByFTP(serverName、用户名、密码、serverFilePath、localFilePath)){
意向意向=新意向(意向.行动\视图);
setDataAndType(Uri.fromFile(新文件(localFilePath)),“application/vnd.android.package archive”);
intent.setFlags(intent.FLAG_ACTIVITY_NEW_TASK);//没有这个标志,android返回了一个intent错误!
背景。开始触觉(意图);
}否则{
//什么都不做,无法下载
}
字符串apkLocation=“/download/”+“SmartPOS.apk”;
意向意向1=新意向(意向.行动\视图);
intent1.setDataAndType(Uri.fromFile(新文件(Environment.getExternalStorageDirectory()+apkLocation)),“application/vnd.android.package archive”);
intent1.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);//没有这个标志,android返回了一个Intent错误!
背景。起始触觉(意图1);
}捕获(例外e){
}
返回null;
}
//下面是使用FTP下载的代码
公共布尔getFileByFTP(字符串服务器名、字符串用户名、,
字符串密码、字符串服务器文件路径、字符串本地文件路径)
抛出异常{
FTPClient ftp=新的FTPClient();
试一试{
连接(服务器名);
int reply=ftp.getReplyCode();
如果(!FTPReply.isPositiveCompletion(回复)){
ftp.disconnect();
返回false;
}
}捕获(IOE异常){
如果(ftp.isConnected()){
试一试{
ftp.disconnect();
}捕获(IOF异常){
投掷e;
}
}
投掷e;
}捕获(例外e){
投掷e;
}
试一试{
如果(!ftp.login(用户名、密码)){
ftp.logout();
}
setFileType(FTPClient.BINARY文件类型);
ftp.enterLocalPassiveMode();
final int lenghtOfFile=(int)getFileSize(ftp,serverFilePath);
OutputStream输出=新文件OutputStream(localFilePath);
CountingOutputStream cos=新的CountingOutputStream(输出){
写入前受保护的空(int n){
super.beforewite(n);
整数百分比=数学四舍五入((getCount()*100)/lenghtOfFile);
Log.d(“FTP_下载”、“ByTestTransferred/downloaded”+百分比);
System.err.println(“下载”+getCount()+“/”+百分比);
M进度。设置进度(百分比);
}
};
ftp.setBufferSize(2024*2048);//提高下载速度
ftp.retrieveFile(serverFilePath,输出);
output.close();
ftp.noop();//检查控制连接是否正常工作
ftp.logout();
返回true;
}
锁扣(FTP连接闭合e){
Log.d(“FTP_下载”,“错误FTPConnectionClosedException:”+e.toString());
投掷e;
}捕获(IOE异常){
Log.d(“FTP_下载”,“错误IOException:+e.toString());
投掷e;
}捕获(例外e){
Log.d(“FTP_下载”,“错误异常:+e.toString());
投掷e;
}最后{
如果(ftp.isConnected()){
试一试{
ftp.disconnect();
}捕获(IOF异常){
投掷f;
}
}
}
}
私有静态长getFileSize(FTPClient ftp,字符串文件路径)引发异常{
长文件大小=0;
FTPFile[]files=ftp.listFiles(文件路径);
如果(files.length==1&&files[0].isFile()){
fileSize=文件[0]。getSize();
}
Log.d(“FTP_下载”,“文件大小=”+文件大小);
返回文件大小;
}
}
基本上,UI不会得到更新,而且我不确定
CountingOutputStream
是否是查找文件下载大小的正确方法


提前谢谢。

我更改了代码的这个retrieveFile部分,现在一切正常


ftp.retrieveFile(serverFilePath,cos)

我更改了代码的这个retrieveFile部分,现在可以了

ftp.retrieveFile(serverFilePath,cos)