/************************************************************************//*FTP download the specified folder or file parameter description: FtpPath ftp to download Directory or file LocalPath saved to local directory ftpaddress FTP address Ftpsuser login FTP username ftppass login FTP password SERR error message returned when error occurs */ /******************************************************************* /bool getfilefromftp (CString ftppath, CString LocalPath, CString ftpaddress, CString ftpuser, CString Ftppass, Cstri ng &serr) {CString stemp; CString slocaltemp; CString Stempftppath; CInternetSession Pinternetcon (AfxGetAppName (), 1,pre_config_internet_access); CFtpConnection *pftpcon; try {pftpcon =pinternetcon.getftpconnection (ftpaddress, Ftpuser, FtpPass); if (pFtpCon! = N ULL) {CFtpFileFind ftpfinder (Pftpcon); BOOL bwork = Ftpfinder.findfile (Ftppath), while (bwork) {bwork = Ftpfinder.findnextfile (); if (Ftpfinder.isdots ()) con Tinue;if (Ftpfinder.isdirectory ()) {//Is the directory Stemp.empty (); stemp = Ftpfinder.getfilenamE (); Stempftppath.empty (); Stempftppath = Ftppath;stempftppath + = Stemp;stempftppath + = "\\*.*"; Slocaltemp.empty (); Slocaltemp = LocalPath + stemp; CFileStatus rstatus;if (!) ( CFile::GetStatus (Slocaltemp, Rstatus))) {//folder does not exist, created if (! ( CreateDirectory ((LPCTSTR) slocaltemp, NULL))) {//create failed DWORD Derr = GetLastError (); Serr.format ("Create new directory failed: ERROR%d"), D ERR); return FALSE; }}getfilefromftp (Stempftppath, Slocaltemp, FTPAddress, Ftpuser, Ftppass, SERR);} Else{stemp.empty (); stemp = Ftpfinder.getfilename (); Slocaltemp.empty (); slocaltemp = LocalPath + "\ \" + stemp; stemp.empt Y (); stemp = Ftpfinder.getfilepath ();//cfilestatus rstatus;if (! ( CFile::GetStatus (Slocaltemp, Rstatus))) {//File not present, download if (! (Pftpcon->getfile (Stemp, Slocaltemp, True, file_attribut E_normal, Ftp_transfer_type_binary, 1)) {//download failed DWORD Derr = GetLastError (); Serr.format (_t ("Download failed: ERROR%d"), Derr); return FALSE;} }}}ftpfinder.close ();} }catch (CInternetException * pEx) {TCHAR szerror[1024];p ex->geterrormessage (szerror,1024); Serr.format (_t ("Connection FTP failed:%s"), pEx);p ex->delete ();p Ftpcon=null;return FALSE ;} return TRUE;}
MFC implements the download of a specified folder or file