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)