linux ftp下载文件显示进度条,ftplib实战-下载(带进度显示)

class MyFtp

{

private:

string _host;

string _user;

string _password;

string _err;

public:

MyFtp(const string& host, const string& user, const string& password);

~MyFtp();

public:

bool connect();

bool down(const string& sourceFileName, const string& targetFileName);

string getErr();

};实现:MyFtp* _ftp;

static netbuf *conn = NULL;

struct REMFILE {

struct REMFILE *next;

int fsz;

char *fnm;

};

static int log_progress(netbuf *ctl, int xfered, void *arg)

{

struct REMFILE *f = (struct REMFILE *) arg;

int pct = (int)(xfered*1.0/f->fsz*100);

//printf("%s %3d%%/r", f->fnm, pct);

//fflush(stdout);

::PostMessage((HWND)(AfxGetApp()->GetMainWnd()->GetSafeHwnd()),WM_PROGRESS,pct,NULL);

return 1;

}

MyFtp::MyFtp(const string& host, const string& user, const string& password)

{

_host = host;

_user = user;

_password = password;

FtpInit();

}

MyFtp::~MyFtp()

{

if (conn)

FtpClose(conn);

}

bool MyFtp::connect()

{

if (!FtpConnect(_host.c_str(), &conn))

{

_err = ftplib_lastresp;

return false;

}

if (!FtpLogin(_user.c_str(), _password.c_str(), conn))

{

_err = FtpLastResponse(conn);

return false;

}

return true;

}

bool MyFtp::down(const string& sourceFileName, const string& targetFileName)

{

int sts=0;

int fsz;

if (!connect())

return false;

FtpOptions(FTPLIB_CALLBACK, (long) NULL, conn);

struct REMFILE *f;

f = (struct REMFILE *) malloc(sizeof(struct REMFILE));

memset(f,0,sizeof(struct REMFILE));

f->fnm = strdup(sourceFileName.c_str());

if (!FtpSize(f->fnm, &fsz, 'I', conn))

fsz = 0;

f->fsz = fsz;

fsz = fsz/100;

if (fsz)

{

FtpOptions(FTPLIB_CALLBACK, (long) log_progress, conn);

FtpOptions(FTPLIB_IDLETIME, (long) 1000, conn);

FtpOptions(FTPLIB_CALLBACKARG, (long) f, conn);

FtpOptions(FTPLIB_CALLBACKBYTES, (long) fsz, conn);

}

sts = FtpGet(targetFileName.c_str(),f->fnm,'I',conn);

if (sts)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值