一. 连接FTP服务器
BOOL flag; CString cstrFtpServer = TEXT("10.142.252.155"); //ftp 服务器地址 CString cstrFtpUserName = TEXT("pdmug"); //用户名 CString cstrFtpPassword = TEXT("pdmuguser"); //密码 CInternetSession* m_pInternetSession = NULL; CFtpConnection* m_pFtpConnection = NULL; try { m_pInternetSession = new CInternetSession(); m_pFtpConnection = m_pInternetSession->GetFtpConnection(cstrFtpServer, cstrFtpUserName, cstrFtpPassword, 21); //21 --- ftp port } catch (CInternetException* pEx) //error:can not connect to specific ftp { if (m_pInternetSession != NULL) { delete m_pInternetSession; } if (m_pFtpConnection != NULL) { delete m_pFtpConnection; } return; }二. 获取当前目录
CString cstrCurrDir; flag = m_pFtpConnection->GetCurrentDirectory(cstrCurrDir); if (!flag) //获取当前目录失败 { }三. 设置当前目录
CString cstrNewCurrDir = TEXT("//pdmpv/GOX/BACK_COVER/"); flag = m_pFtpConnection->SetCurrentDirectory(cstrNewCurrDir); if (!flag) //设置当前目录失败 { }四. 从FTP服务器下载文件
flag = m_pFtpConnection->GetFile(TEXT("CA110900_2ND_MD.ol"), TEXT("D:\\123.ol"),TRUE); if (!flag) //download file fail { }五. 上传文件到FTP服务器
flag = m_pFtpConnection->PutFile(TEXT("D:\\123.txt"), TEXT("456.txt")); if (!flag) //upload file fail { }六. 在FTP上重命名文件
flag = m_pFtpConnection->Rename(TEXT("456.txt"), TEXT("456_wy.txt")); if (!flag) //rename file fail { }七. 删除FTP上文件
flag = m_pFtpConnection->Remove(TEXT("456.txt")); if (!flag) //remove file fail { }八. 在FTP上创建文件
flag = m_pFtpConnection->CreateDirectory(TEXT("WangYao")); if (!flag) //create directory on ftp fail { }九. 删除FTP上的目录(注意:目录必须是空的,否则会导致错误。)
flag = m_pFtpConnection->RemoveDirectory(TEXT("WangYao")); if (!flag) //删除失败 { }十. 别忘了释放资源
delete m_pInternetSession; delete m_pFtpConnection;十一. FTP文件搜索
1. 如上:连接FTP
2. 如上:设置当前目录
3. 找到的文件(参考CFileFind)
CFtpFileFind fFinder(m_pFtpConnection); BOOL bFind = fFinder.FindFile(TEXT("*.*")); while (bFind) { bFind = fFinder.FindNextFile(); //当前文件夹及上层文件夹(名称分别为.和..)----------------- if (fFinder.IsDots()) { continue; } //子文件夹--------------------------------------------- if(fFinder.IsDirectory()) { CString cstrDirName = fFinder.GetFileName(); //directory name CString cstrDirPath = fFinder.GetFilePath(); //directory path continue; } //文件------------------------------------------------- CString cstrFileName = fFinder.GetFileName(); //file name CString cstrFilePath = fFinder.GetFilePath(); //file path } fFinder.Close();