MFC VC++ FTP创建文件夹,上传文件,下载文件
1.从FTP下载文件代码
头文件 #include <afxinet.h>
1 BOOL CMFCApplication1Dlg::getFileFromFTP(CString strFtpPath, CString strLocalPath) 2 { 3 CInternetSession *pInternetSession; 4 CFtpConnection *pFtpConnection; 5 pInternetSession = new CInternetSession(AfxGetAppName(), 1, PRE_CONFIG_INTERNET_ACCESS); 6 try 7 { 8 pFtpConnection = pInternetSession->GetFtpConnection 9 (_T("127.0.0.1"),//FTP服务器IP 10 _T("user"),//用户名 11 _T("pwd"),//密码 12 21,//FTP服务器端口号 13 TRUE); 14 if (pFtpConnection) 15 { 16 BOOL res = pFtpConnection->GetFile(strFtpPath, strLocalPath, FALSE); 17 if (!res) 18 { 19 return FALSE; 20 } 21 22 if (pFtpConnection) 23 { 24 pFtpConnection->Close(); 25 pFtpConnection = NULL; 26 } 27 if (pInternetSession) 28 { 29 pInternetSession->Close(); 30 pInternetSession = NULL; 31 } 32 return TRUE; 33 } 34 else 35 { 36 return FALSE; 37 } 38 } 39 catch (CInternetException *pEx) 40 { 41 TCHAR szError[1024]; 42 if (pEx->GetErrorMessage(szError, 1024)) 43 { 44 AfxMessageBox(szError); 45 } 46 pEx->Delete(); 47 pFtpConnection = NULL; 48 if (pInternetSession) 49 { 50 pInternetSession->Close(); 51 pInternetSession = NULL; 52 } 53 return FALSE; 54 } 55 }
调用下载文件函数
1 void CMFCApplication1Dlg::OnBnClickedOk() 2 { 3 // TODO: 在此添加控件通知处理程序代码 4 CString strFTPPathName = _T("FTP根目录\\111\\123.doc"); 5 CString strPathName = _T("E:\\123.doc"); 6 BOOL res = getFileFromFTP(strFTPPathName, strPathName); 7 if (res) 8 { 9 MessageBox(_T("下载成功")); 10 } 11 else 12 { 13 MessageBox(_T("下载失败")); 14 } 15 }
2.在FTP上创建文件夹
1 BOOL CMFCApplication1Dlg::CreateFolderToFtp(CString &strFtpPath) 2 { 3 //要这一个传入参数好像没啥用,创建的文件夹固定为skonda//SN//image skonda文件夹已存在 4 CInternetSession *pInternetSession; 5 CFtpConnection *pFtpConnection; 6 pInternetSession = new CInternetSession(AfxGetAppName(), 1, PRE_CONFIG_INTERNET_ACCESS); 7 try 8 { 9 pFtpConnection = pInternetSession->GetFtpConnection 10 (_T("127.0.0.1"),//FTP服务器IP 11 _T("user"),//用户名 12 _T("pwd"),//密码 13 21,//FTP服务器端口号 14 TRUE); 15 if (pFtpConnection) 16 { 17 CString strFtp1Path, strFtp2Path; 18 strFtp1Path = _T("/1111"); 19 strFtp2Path = strFtp1Path + _T("/2222"); 20 BOOL ret = pFtpConnection->CreateDirectory(strFtp1Path);//不能创建多级目录 21 if (!ret) 22 { 23 MessageBox(_T("创建FTP文件夹失败")); 24 strFtpPath.Empty(); 25 return FALSE; 26 } 27 28 ret = pFtpConnection->CreateDirectory(strFtp2Path);//不能创建多级目录 29 if (!ret) 30 { 31 MessageBox(_T("创建FTP文件夹失败")); 32 strFtpPath = strFtp1Path; 33 return FALSE; 34 } 35 if (pFtpConnection) 36 { 37 pFtpConnection->Close(); 38 pFtpConnection = NULL; 39 } 40 if (pInternetSession) 41 { 42 pInternetSession->Close(); 43 pInternetSession = NULL; 44 } 45 strFtpPath = strFtp2Path; 46 return TRUE; 47 } 48 else 49 { 50 MessageBox(_T("FTP服务器连接失败")); 51 strFtpPath.Empty(); 52 return FALSE; 53 } 54 } 55 catch (CInternetException *pEx) 56 { 57 TCHAR szError[100]; 58 if (pEx->GetErrorMessage(szError, 100)) 59 { 60 AfxMessageBox(szError); 61 } 62 pEx->Delete(); 63 pFtpConnection = NULL; 64 if (pInternetSession) 65 { 66 pInternetSession->Close(); 67 pInternetSession = NULL; 68 } 69 return FALSE; 70 } 71 }
调用代码
1 CString strFtpPath; 2 BOOL res = CreateFolderToFtp(strFtpPath); 3 if (res) 4 { 5 CString strMsg; 6 strMsg.Format(_T("%s 创建成功"), strFtpPath); 7 MessageBox(strMsg); 8 } 9 else 10 { 11 MessageBox(_T("创建失败")); 12 }
3.向FTP上传文件
1 BOOL CMFCApplication1Dlg::putFileToFtp(CString strFilePathName, CString strFileFtpPathName) 2 { 3 CInternetSession *pInternetSession; 4 CFtpConnection *pFtpConnection; 5 pInternetSession = new CInternetSession(AfxGetAppName(), 1, PRE_CONFIG_INTERNET_ACCESS); 6 try 7 { 8 pFtpConnection = pInternetSession->GetFtpConnection 9 (_T("127.0.0.1"),//FTP服务器IP 10 _T("user"),//用户名 11 _T("pwd"),//密码 12 21,//FTP服务器端口号 13 TRUE); 14 if (pFtpConnection) 15 { 16 BOOL bPut = pFtpConnection->PutFile(strFilePathName, strFileFtpPathName); 17 if (!bPut) 18 { 19 MessageBox(_T("上传失败")); 20 return FALSE; 21 } 22 if (pFtpConnection) 23 { 24 pFtpConnection->Close(); 25 pFtpConnection = NULL; 26 } 27 if (pInternetSession) 28 { 29 pInternetSession->Close(); 30 pInternetSession = NULL; 31 } 32 return TRUE; 33 } 34 else 35 { 36 MessageBox(_T("FTP服务器连接失败")); 37 return FALSE; 38 } 39 } 40 catch (CInternetException *pEx) 41 { 42 TCHAR szError[100]; 43 if (pEx->GetErrorMessage(szError, 100)) 44 { 45 AfxMessageBox(szError); 46 } 47 pEx->Delete(); 48 pFtpConnection = NULL; 49 if (pInternetSession) 50 { 51 pInternetSession->Close(); 52 pInternetSession = NULL; 53 } 54 return FALSE; 55 } 56 }
调用代码
1 CString strFilePathName = _T("E:\\123.ini"); 2 CString sttFileFtpPathName = _T("1111\\2222\\qqq.ini"); 3 BOOL res = putFileToFtp(strFilePathName, sttFileFtpPathName); 4 if (res) 5 { 6 MessageBox(_T("上传成功")); 7 } 8 else 9 { 10 MessageBox(_T("上传失败")); 11 }