新手入門,借鑒了很多地方寫的
vector <CString> m_string; //創建Vector保存目錄 vector <CString> m_dotstring; //保存文件 vector <CString> V_counterpart; //進入下一層按鈕時創建當前層副本,記錄列表項的數據 CString m_ftppath; //函數需要的輸入路徑,及搜索當前路徑的文件以及目錄 vector <CString> V_ftppath; //副本保存路徑,顯示下一層時用m_ftppath加上**,返回上層時減去** int n; //記錄當前顯示的層數,根目錄為0層, int dir[20]; //創建一個可以保存20個數的數組來保存目錄存儲時每一層有多少個文件和目錄,現在可以存儲10層目錄,控制這個數組大小可以控制目錄的層數 int size[20]; //存儲每層多少目錄BOOL FtpList(CString &ftppath,CString ftpaddress,CString username,CString &Freason,CString passWord,int port) //定義ftp目錄遍歷函數{CInternetsession sess(_T("List Files Session")); //創建CInternetSession類對象 sess CFtpConnection *pFtpCon=NULL; //創建cftpconnection類對象指針* pFtpCon 為默認值try{pFtpCon=sess.GetFtpConnection(ftpaddress,username,password,port); //調用GetFtpConnection連接FTP服務器if(pFtpCon==NULL) { Freason = "connection is false";return FALSE;} CFtpFileFind ftpDirFinder (pFtpCon); //創建CFtpFileFind類對象ftpDirFinder,"()"內為格式中要求填寫的cftpconnection類對象指針CStringArray m_dir1; //定義一個數組保存目錄,一個保存文件CStringArray m_dir2; BOOL bWorking = ftpDirFinder.FindFile(ftppath); while (bWorking) //循環進行目錄調用,保存FTP第一層文件或者文件夾在數組中 {bWorking=ftpDirFinder.FindNextFile(); //循環條件:如果bWork為找不到下一個文件了,循環跳出 if(ftpDirFinder.IsDots()) //這一段為跳過"."和"..“文件,不跳過會無線重復。 { continue; } if(ftpDirFinder.IsDirectory()) { m_dir1.Add( ftpDirFinder.GetFileName()); //如果是目錄的話,就保存在數組1//只需要一層,不用遞歸,如果用遞歸,則遍歷多層。 } else //找到的不是目錄(即文件),保存在數組2 { m_dir2.Add( ftpDirFinder.GetFileName());//文件保存在數組2中 }}ftpDirFinder.Close(); //關閉ftp連接,開始顯示數組中每一項目錄for (int i=0;i<m_dir1.GetSize();i++) //通過循環保存每一項目錄或者文件{m_string.push_back(m_dir1.GetAt(i)); //目錄保存在m_string中} for (int j=0;j<m_dir2.GetSize();j++) {m_dotstring.push_back(m_dir2.GetAt(j)); //文件保存在m_dotstring中}}catch(CInternetException* pEx){ TCHAR sz[1024]; pEx->GetErrorMessage(sz, 1024); Freason = sz; PRintf("%s",sz); pEx->Delete();}if (pFtpCon != NULL) { pFtpCon->Close(); delete pFtpCon; pFtpCon = NULL; } return TRUE;}BOOL FolderExists(CString s) { DWORD attr; attr = GetFileAttributes(s); return (attr != (DWORD)(-1) ) && ( attr & FILE_ATTRIBUTE_DIRECTORY); }BOOL CreateMuliteDirectory(CString P) { int len=P.GetLength(); if ( len <2 )return false; if('/'==P[len-1]) { P=P.Left(len-1); len=P.GetLength(); } if ( len <=0 ) return false; if (len <=3) { if (FolderExists(P))return true; elsereturn false; } if (FolderExists(P))return true; CString Parent; Parent=P.Left(P.ReverseFind('/') ); if(Parent.GetLength()<=0)return false; BOOL Ret=CreateMuliteDirectory(Parent); if(Ret) { SECURITY_ATTRIBUTES sa; sa.nLength=sizeof(SECURITY_ATTRIBUTES); sa.lpSecurityDescriptor=NULL; sa.bInheritHandle=0; Ret=(CreateDirectory(P,&sa)==TRUE); return Ret; } else return FALSE; }BOOL DownloadFromFTP(CString remotepath,CString localpath,CString ftpaddr,CString username,CString &falreason,CString password,int port) //定義布爾函數DownloadFromFTP{ int index = remotepath.ReverseFind('/'); //定義索引本index表示找到與remotepath中最后一個'/'時的 if (index == -1) { falreason = "'/' can't found"; return FALSE; //表示沒有找到 } CString remotefile = remotepath.Mid(index+1,remotepath.GetLength()); //返回'/'后的字符 CInternetSession sess(_T("Download Files Session")); //創建class sess CFtpConnection* pFtpCon = NULL; //創建class pFtpCon為默認值 BOOL b; try { pFtpCon = sess.GetFtpConnection(ftpaddr,username,password,port); //調用GetFtpConnection連接FTP服務器 if (pFtpCon == NULL) //如果連接失敗 { falreason = "connection is false"; return FALSE; //連接失敗時的返回結果 } CFtpFileFind ftpFinder(pFtpCon); //定義CFtpFileFind對象 BOOL bWorking = ftpFinder.FindFile(remotepath); // 在FTP上查找remotepath(findfile為文件搜索函數,這里應該是查找FTP上有無此路徑) if (bWorking != TRUE) //如果在FTP服務器上沒有找到remotepath { falreason = remotepath+ L"can't found"; return FALSE; //沒找到路徑時的返回結果 }CString remotepath1=remotepath.Left(index+1);;//如果最后一個'/'后面還有文件名,那么此程序會創一個名為此文件名的文件夾if(!FolderExists(localpath+'/'+remotepath1)){CreateMuliteDirectory(localpath+'/'+remotepath1); } b=pFtpCon->GetFile(remotepath, localpath+'/'+remotepath1+'/'+remotefile ,TRUE, FILE_ATTRIBUTE_NORMAL, FTP_TRANSFER_TYPE_BINARY, 1); if ( !b) { while(bWorking) //此循環為下載目錄文件的循環 { bWorking = ftpFinder.FindNextFile(); if (ftpFinder.IsDots()) //如果找到的是"."(表示返回頂級目錄)或者".."(表示返回上一級目錄) { TRACE("%s/n",ftpFinder.GetFileName()); continue; } else if(ftpFinder.IsDirectory()) //如果找到的是文件夾,遞歸調用DownloadFromFTP() { TRACE("%s/n",ftpFinder.GetFileName()); DownloadFromFTP(remotepath+'/'+ftpFinder.GetFileName(),localpath+'/'+remotefile,ftpaddr,username,falreason,password, port); } else //如果找到的是文件,直接下載 { TRACE("%s/n",ftpFinder.GetFileName()); if(!FolderExists(localpath+'/'+remotepath)) { CreateMuliteDirectory(localpath+'/'+remotepath); } pFtpCon->GetFile(remotepath+'/'+ftpFinder.GetFileName(), localpath+'/'+remotepath+'/'+ftpFinder.GetFileName(),TRUE, FILE_ATTRIBUTE_NORMAL, FTP_TRANSFER_TYPE_BINARY, 1); } } } } catch (CInternetException* pEx) { TCHAR sz[1024]; pEx->GetErrorMessage(sz, 1024); falreason = sz; printf("%s",sz); pEx->Delete(); } if (pFtpCon != NULL) { pFtpCon->Close(); delete pFtpCon; pFtpCon = NULL; } return TRUE; } void CftpDLDlg::OnBnClickedconnectButton1(){// TODO: Add your control notification handler code herem_string.clear();m_dotstring.clear();m_ftppath=(_T(""));m_listBox.ResetContent();V_counterpart.clear();n=0;UpdateData(TRUE); //定義一個初始文件搜索不清楚"*"在此處代表的意思,第二次遍歷次層文件時必須重新賦值 FtpList(m_ftppath,(_T("192.168.1.161")),(_T("admin")),m_Freason,(_T("admin")),74);UpdateData(FALSE);size[n]=m_string.size(); for(unsigned int i=0;i<m_string.size();i++) //顯示根目錄 0層{m_listBox.AddString(m_string.at(i));}if (m_dotstring.size()!=0){for(unsigned int j=0;j<m_dotstring.size();j++){m_listBox.AddString(m_dotstring.at(j));}}}void CftpDLDlg::OnBnClickedshownextdirButton(){// TODO: Add your control notification handler code hereCString n_ftppath;int NcurSel;NcurSel = m_listBox.GetCurSel();if(m_listBox.GetCount()!=0) //判斷是否為空目錄,果然為空目錄就不執行循環{ if(NcurSel!=-1) //加一個判斷,如果未選擇列表項則不執行程序并彈出提示框, 未選擇時不為1 { if(NcurSel<size[n]) //加一個判斷,如果是文件則報錯,不執行下面創建副本以及進入下一層目錄操作 { if(n+1) //按下顯示下一層按鈕時創建副本存儲當前層數據 { vector <CString> V_n; //創建臨時副本V_n CString strText; int numlist; numlist= m_listBox.GetCount(); //當前列表項數量 for(int k=0;k<numlist;k++) { m_listBox.GetText(k,strText); V_n.push_back(strText); //把當前的列表信息保存在當臨時副本,數據數量為numlist } for(int l=0;l<numlist;l++) { V_counterpart.push_back(V_n[l]) ; //當前副本信息轉移到公共副本 } dir[n]=numlist; //當前目錄包含文件數,記錄此層存放在公共副本的數據數dir【0】=4 dir【1】=3 //第0層為"" 第一層為/521 numlist=m_listBox.GetCurSel(); m_listBox.GetText(numlist,strText); V_ftppath.push_back(L"/"+strText); V_n.clear(); //清除臨時副本 n++; //當前層數 } m_string.clear(); //此處開始為進入下一層目錄的代碼 m_dotstring.clear(); m_listBox.GetText(NcurSel, n_ftppath); m_ftppath=m_ftppath+L"/"+n_ftppath; //"/520" m_listBox.ResetContent(); FtpList(m_ftppath,(_T("192.168.1.161")),(_T("admin")),m_Freason,(_T("admin")),74);UpdateData(FALSE);size[n]=m_string.size(); for(unsigned int i=0;i<m_string.size();i++) { m_listBox.AddString(m_string.at(i)); } if (m_dotstring.size()!=0) { for(unsigned int j=0;j<m_dotstring.size();j++) { m_listBox.AddString(m_dotstring.at(j)); } } } else { CString d_false; d_false="選擇項為文件,無法進入下一層"; AfxMessageBox(d_false); } } else { CString m_false; m_false="請選擇列表項"; AfxMessageBox(m_false); } }else {CString n_false;n_false="空目錄,無法進入下一層";AfxMessageBox(n_false);}}void CftpDLDlg::OnBnClickedReturnButton2(){// TODO: Add your control notification handler code here if(n>0) //n現在是2{m_string.clear(); m_dotstring.clear(); m_listBox.ResetContent(); //先清除當前列表數據int l=0;for(int j=0;j<n-1;j++) //除了返回層以外的其他數據數量{ l=l+dir[j];}//需要顯示第1層數據,即后面3個數據,V_counterpart[4] [5] [6] dir[0]=4 dir [1]=3 V_counterpart.size()=7 循環為 for(unsigned int i=l;i<V_counterpart.size();i++) //根據N顯示哪一個副本 { m_listBox.AddString(V_counterpart.at(i)); //根據條件顯示副本中數據 } V_counterpart.resize(l);m_ftppath.TrimRight(V_ftppath[n-1]);V_ftppath.resize(n-1); //在公共副本中清除當前顯示數據層以及之后層的數據dir[n-1]=0; size[n]=0; }n--; //返回處理之后層數,邏輯為顯示下一層n+1,返回上一層n-1}void CftpDLDlg::OnLbnSelchangedirshowList1() //此函數還要加一些點擊什么的功能{// TODO: Add your control notification handler code here CString strText; int nCurSel;nCurSel = m_listBox.GetCurSel(); // 獲取當前選中列表項 m_listBox.GetText(nCurSel, strText); // 獲取選中列表項的字符串 }void CftpDLDlg::OnBnClickeddownloadButton2(){// TODO: Add your control notification handler code hereCString n_ftppath;int NcurSel;NcurSel = m_listBox.GetCurSel();if(m_listBox.GetCount()!=0) //判斷是否為空目錄,果然為空目錄就不執行循環{ if(NcurSel!=-1) //加一個判斷,如果未選擇列表項則不執行程序并彈出提示框, 未選擇時不為1 { m_listBox.GetText(NcurSel, n_ftppath); m_ftppath=m_ftppath+L"/"+n_ftppath; DownloadFromFTP(m_ftppath,(_T("D:/ftp")),(_T("192.168.1.161")),(_T("admin")),m_Freason,(_T("admin")),74); UpdateData(FALSE); } else { CString m_false; m_false="請選擇列表項"; AfxMessageBox(m_false); } }else {CString n_false;n_false="空目錄,無法下載";AfxMessageBox(n_false);}}
新聞熱點
疑難解答