#pragma once #include <afxinet.h> #include <vector> #define FTP_DEFAULT_PORT 21 struct FileProperty { CString strFileName; // FILE_ATTRIBUTE_DIRECTORY // GetFileAttributes DWORD dwFileType; }; class CYWFtpSession { public: CYWFtpSession(void); ~CYWFtpSession(void); // 设置FTP服务器地址、端口 void SetServerParam(CString strServerAddr, INTERNET_PORT wServerPort); // 设置登录名和密码 void SetUserInfo(CString strUserName, CString strPassWord, BOOL bInitiativeMode = FALSE); // 连接到指定FTP服务器 BOOL ConnectToServer(); // 得到指定目录下的所有文件 void GetFileList(CString strDirPath, std::vector<FileProperty> &vctFileList); // 上传一个文件到FTP服务器 BOOL PutFileToServer(CString strLocalFile, CString strPutPath); // 从FTP服务器下载一个文件 BOOL GetFileFromServer(CString strRemoteFile, CString strLocalPath, BOOL bFailIfExists = FALSE); // 关闭连接 void CloseConnection(); // 检测对象是否存在 BOOL IsConnectionActive(); // 获取最后一个错误信息 CString GetLastErrorMessage(); private: // FTP服务器地址 CString m_strServerAddr; // 服务端口 INTERNET_PORT m_wServerPort; // 登录用户名 CString m_strUserName; // 登录密码 CString m_strPassWord; // 被动模式-FALSE 主动模式-TRUE BOOL m_bInitiativeMode; // 记录最后一个错误信息 CString m_strLastErrorMsg; // 创建并初始化一个或多个同时的Internet 会话 CInternetSession m_cInetSession; // 管理与Internet服务器的FTP连接并允许直接操纵服务器中的目录和文件 CFtpConnection *m_pFtpConn; }; #include "StdAfx.h" #include "YWFtpSession.h" CYWFtpSession::CYWFtpSession(void) { m_pFtpConn = NULL; m_strServerAddr.Empty(); m_wServerPort = FTP_DEFAULT_PORT; m_strUserName.Empty(); m_strPassWord.Empty(); m_bInitiativeMode = FALSE; m_strLastErrorMsg.Empty(); } CYWFtpSession::~CYWFtpSession(void) { CloseConnection(); } // 设置FTP服务器地址、端口 void CYWFtpSession::SetServerParam(CString strServerAddr, INTERNET_PORT wServerPort) { m_strServerAddr = strServerAddr; m_wServerPort = wServerPort; } // 设置登录名和密码 void CYWFtpSession::SetUserInfo(CString strUserName, CString strPassWord, BOOL bInitiativeMode) { m_strUserName = strUserName; m_strPassWord = strPassWord; m_bInitiativeMode = bInitiativeMode; } // 连接到指定FTP服务器 BOOL CYWFtpSession::ConnectToServer() { try { CloseConnection(); // m_bInitiativeMode = TRUE 为被动模式 m_pFtpConn = m_cInetSession.GetFtpConnection(m_strServerAddr, m_strUserName, m_strPassWord, m_wServerPort, m_bInitiativeMode); if (NULL != m_pFtpConn) return TRUE; } catch (CInternetException *e) { e->Delete(); } return FALSE; } // 得到指定目录下的所有文件 void CYWFtpSession::GetFileList(CString strDirPath, std::vector<FileProperty> &vctFileList) { if (NULL == m_pFtpConn && !ConnectToServer()) return ; vctFileList.empty(); CFtpFileFind cFtpFileFind(m_pFtpConn); // 查找指定目录 BOOL bWorking = cFtpFileFind.FindFile(strDirPath); while (bWorking) { bWorking = cFtpFileFind.FindNextFile(); // .或者.. if (cFtpFileFind.IsDots()) continue; FileProperty sOneFile; // 得到文件名 sOneFile.strFileName = cFtpFileFind.GetFileName(); sOneFile.dwFileType = 0; // 文件夹 if (cFtpFileFind.IsDirectory()) sOneFile.dwFileType |= FILE_ATTRIBUTE_DIRECTORY; else sOneFile.dwFileType |= FILE_ATTRIBUTE_NORMAL; // 普通文件 // 添加进文件列表 vctFileList.push_back(sOneFile); } } // 上传一个文件到FTP服务器 BOOL CYWFtpSession::PutFileToServer(CString strLocalFile, CString strPutPath) { if (NULL == m_pFtpConn && !ConnectToServer()) return FALSE; return m_pFtpConn->PutFile(strLocalFile, strPutPath); } // 从FTP服务器下载一个文件 BOOL CYWFtpSession::GetFileFromServer(CString strRemoteFile, CString strLocalPath, BOOL bFailIfExists) { if (NULL == m_pFtpConn && !ConnectToServer()) return FALSE; return m_pFtpConn->GetFile(strRemoteFile, strLocalPath, bFailIfExists); } // 关闭连接 void CYWFtpSession::CloseConnection() { if (NULL != m_pFtpConn) { m_pFtpConn->Close(); delete m_pFtpConn; } m_pFtpConn = NULL; } // 检测对象是否存在 BOOL CYWFtpSession::IsConnectionActive() { if (NULL == m_pFtpConn) return FALSE; return TRUE; } // 获取最后一个错误信息 CString CYWFtpSession::GetLastErrorMessage() { m_strLastErrorMsg.Format(_T("错误码:%ld"), ::GetLastError()); return m_strLastErrorMsg; } 自己代码 FTPClient ftp; CInternetSession *m_pInetSession; CFtpConnection *m_pFtpConnection; Thr_Obj->m_pInetSession = new CInternetSession(AfxGetAppName(), 1, PRE_CONFIG_INTERNET_ACCESS); //Thr_Obj->m_pInetSession = new CInternetSession(); try{ Thr_Obj->m_pFtpConnection = Thr_Obj->m_pInetSession->GetFtpConnection(Thr_Obj->m_host, Thr_Obj->m_username, Thr_Obj->m_password, Thr_Obj->m_port, FALSE); } catch (CInternetException *e) { AfxMessageBox("无网络连接"); return FALSE; } if (!Thr_Obj->m_pFtpConnection) { Thr_Obj->logstrdlg = Thr_Obj->m_host + "," + Thr_Obj->m_username + "," + Thr_Obj->m_password + "," + "21 获取目录登陆失败"; CTD_writeLog(Thr_Obj->logstrdlg); return 0; } //遍历文件获取文件列表 void CCTD_XID8690_AppDlg::List() { CString m_i; CString m_ii; CStringArray m_Dir; CFtpFileFind finder(m_pFtpConnection); BOOL bWorking = finder.FindFile(_T("*")); if (!bWorking) ftp.IWriteLog("不存在文件!"); logstrdlg.Empty(); logstrdlg = "《《文件列表》》"; ftp.IWriteLog(logstrdlg); while (bWorking) { bWorking = finder.FindNextFile(); if (finder.IsDots()) continue; if (finder.IsDirectory()) { m_Dir.Add(finder.GetFileName()); } else { //m_i = finder.GetFileName() + "\r\n"; m_i = finder.GetFileName(); FileNameList->push_back(m_i); logstrdlg.Empty(); logstrdlg += m_i; ftp.IWriteLog(logstrdlg); /*for (int j = 0; j<n; j++) { m_ii = "\t"; m_ftpinfo = m_ftpinfo + m_ii; } m_ftpinfo += m_i; UpdateData(FALSE);*/ } } finder.Close(); for (int i = 0; i<m_Dir.GetSize(); i++) { //n++; /*m_i = "[" + m_Dir.GetAt(i) + "]" + "\r\n"; for (int j = 1; j<n; j++) { m_ii = "\t"; m_ftpinfo = m_ftpinfo + m_ii; } m_ftpinfo += m_i; UpdateData(FALSE);*/ BOOL m_suc = 0; while (!m_suc) { m_suc = m_pFtpConnection->SetCurrentDirectory(m_Dir.GetAt(i)); } List(); BOOL m_suc1 = 0; while (!m_suc1) { m_suc1 = m_pFtpConnection->SetCurrentDirectory(".."); } //n--; } m_pInetSession->Close(); delete m_pInetSession; ListConnectStatue =FALSE; logstrdlg.Empty(); logstrdlg = "《《文件列表》》\r\n\r\n"; ftp.IWriteLog(logstrdlg); } libcurl下载操作 BOOL RetrFile1(char * FileName) { CURL *curl; CURLcode res; char Pathdown[200] = {0}; Dolwnpath = "D:/PrintImage/"; Dolwnpath += FileName; strcpy(Pathdown, Dolwnpath); struct FtpFile ftpfile = {Pathdown, NULL }; //初始化一个FtpFile结构 curl_global_init(CURL_GLOBAL_DEFAULT); //XID8690_LoadLib(); curl = curl_easy_init(); CString addrInfo = _T(""); logstrdlg.Empty(); logstrdlg = "下载源文件:"; logstrdlg += FileName; //logstrdlg += "\r\n"; ftp.IWriteLog(logstrdlg); logstrdlg.Empty(); logstrdlg = "目标文件:"; logstrdlg += Dolwnpath; ftp.IWriteLog(logstrdlg); if (curl) { addrInfo = "ftp://192.168.30.1/"; addrInfo += FileName; curl_easy_setopt(curl, CURLOPT_URL, addrInfo); curl_easy_setopt(curl, CURLOPT_USERPWD, "ftp:Abcd1234"); curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, my_fwrite); curl_easy_setopt(curl, CURLOPT_WRITEDATA, &ftpfile); //给相关函数的第四个参数传递一个结构体的指针 curl_easy_setopt(curl, CURLOPT_VERBOSE, TRUE);//CURLOPT_VERBOSE 这个选项很常用用来在屏幕上显示对服务器相关操作返回的信息 res = curl_easy_perform(curl); curl_easy_cleanup(curl); if (CURLE_OK != res) { //fprintf(stderr, "curl told us %d\n", res); logstrdlg.Format("curl told us %d\n", res); ftp.IWriteLog(logstrdlg); return FALSE; } logstrdlg.Empty(); logstrdlg.Format("curl told us %d\n", res); logstrdlg.Remove('\r'); logstrdlg.Remove('\n'); ftp.IWriteLog(logstrdlg); } if (ftpfile.stream) fclose(ftpfile.stream); curl_global_cleanup(); return TRUE; }