FTP
- 格式:doc
- 大小:74.50 KB
- 文档页数:6
1 建立工程MYFTP基于对话框添加控件如下图所示2 为控件添加成员变量3 在MYFTPDlg.h文件的前部包含头文件#include “afxinet.h”在该文件的类定义中添加如下成员声明BOOL bconnect;CInternetSession *pInternetSession;CFtpConnection *pFtpConnection;void ConnectFtp();//连接void UpdateDir();//目录操作4 添加复选框按钮”匿名”的成员函数代码int icheck=m_noname.GetCheck();if(icheck==1){m_usr.EnableWindow(false);m_pwd.EnableWindow(false);m_usr.SetWindowTextA("anonymous");m_pwd.SetWindowTextA("");UpdateData(true);if(!ServerIP.IsBlank()&&!strport.IsEmpty()){m_connect.EnableWindow(true);}}else{m_usr.EnableWindow(true);m_pwd.EnableWindow(true);m_usr.SetWindowTextA("");m_pwd.SetWindowTextA("");m_connect.EnableWindow(false);}5 在MYFTPDlg.cpp中添加两个成员函数的实现void ConnectFtp();//连接BYTE nFild[4];UpdateData(true);ServerIP.GetAddress(nFild[0],nFild[1],nFild[2],nFild[3]);CString sip;sip.Format("%d.%d.%d.%d", nFild[0],nFild[1],nFild[2],nFild[3]);if(sip.IsEmpty()){AfxMessageBox("请指定IP地址!");return;}if(strport.IsEmpty()){AfxMessageBox("请指定端口号!");return;}if(strusr.IsEmpty())return;pInternetSession=new CInternetSession("MR",INTERNET_OPEN_TYPE_PRECONFIG);try{pFtpConnection=pInternetSession->GetFtpConnection(sip,strusr,strpwd,atoi(strport));bconnect=true;}catch(CInternetException *pEx){TCHAR szErr[1024];pEx->GetErrorMessage(szErr,1024);AfxMessageBox(szErr);pEx->Delete();}void UpdateDir();//目录操作m_lst.ResetContent();CFtpFileFind ftpfind(pFtpConnection);BOOL bfind=ftpfind.FindFile(NULL);while(bfind){bfind=ftpfind.FindNextFile();CString strpath;if(!ftpfind.IsDirectory()){strpath=ftpfind.GetFileName();m_lst.InsertString(-1,strpath);}else{strpath=ftpfind.GetFilePath();m_lst.InsertString(-1,strpath);}}6 添加”连接”、”断开”、“上传”、“下载”、“删除”、“进入”、“返回”的成员函数的代码“连接”按钮this->ConnectFtp();this->UpdateDir();ServerIP.EnableWindow(false);m_port.EnableWindow(false);m_connect.EnableWindow(false);m_disconnect.EnableWindow(true);m_enterdir.EnableWindow(true);m_upload.EnableWindow(true);m_download.EnableWindow(true);m_delete.EnableWindow(true);m_noname.EnableWindow(false);m_exit.EnableWindow(false);”断开”按钮pInternetSession->Close();m_lst.ResetContent();m_lst.InsertString(-1,"连接已断开!");ServerIP.EnableWindow(true);m_port.EnableWindow(true);m_connect.EnableWindow(true);m_disconnect.EnableWindow(false);m_enterdir.EnableWindow(false);m_goback.EnableWindow(false);m_upload.EnableWindow(false);m_download.EnableWindow(false);m_delete.EnableWindow(false);m_noname.EnableWindow(true);m_exit.EnableWindow(true);”上传”按钮CString str;CString strname;CFileDialogfile(true,NULL,NULL,OFN_HIDEREADONL Y|OFN_OVERWRITEPROMPT,"所有文件(*.*)|*.*|",this);if(file.DoModal()==IDOK){str=file.GetPathName();strname=file.GetFileName();}if(bconnect){CString strdir;pFtpConnection->GetCurrentDirectory(strdir);BOOL bput=pFtpConnection->PutFile((LPCTSTR)str,(LPCTSTR)strname);if(bput){pInternetSession->Close();this->ConnectFtp();pFtpConnection->SetCurrentDirectory(strdir);this->UpdateDir();AfxMessageBox("上传成功!");}}”下载”按钮CString selfile;m_lst.GetText(m_lst.GetCurSel(),selfile);if(!selfile.IsEmpty()){CFileDialogfile(false,NULL,selfile,OFN_HIDEREADONL Y|OFN_OVERWRITEPROMPT,"所有文件(*.*)|*.*|",this);if(file.DoModal()==IDOK){CString strname;strname=file.GetFileName();CString strdir;pFtpConnection->GetCurrentDirectory(strdir);pFtpConnection->GetFile(selfile,strname);pInternetSession->Close();this->ConnectFtp();AfxMessageBox("下载成功!");}}”删除”按钮CString selfile;m_lst.GetText(m_lst.GetCurSel(),selfile);if(!selfile.IsEmpty()){if(AfxMessageBox("您真的要删除这个文件吗?",4+48)==6){pFtpConnection->Remove(selfile);}CString strdir;pFtpConnection->GetCurrentDir ectory(strdir);pInternetSession->Close();this->ConnectFtp();pFtpConnection->SetCurrentDirectory(strdir);this->UpdateData();}”进入”按钮CString selfile;m_lst.GetText(m_lst.GetCurSel(),selfile);CString strdir;pFtpConnection->GetCurrentDirectory(strdir);if(!selfile.IsEmpty()){strdir+=selfile;pInternetSession->Close();this->ConnectFtp();pFtpConnection->SetCurrentDirectory(strdir);this->UpdateDir();m_goback.EnableWindow(true);}”返回”按钮CString strdir;pFtpConnection->GetCurrentDirectory(strdir);int pos;pos=strdir.ReverseFind('/');strdir=strdir.Left(pos);pInternetSession->Close();this->ConnectFtp();pFtpConnection->SetCurrentDirectory(strdir);this->UpdateDir();7 添加MYFTPDlg类的初始化成员函数bconnect=false;m_lst.ResetContent();m_lst.InsertString(-1,"尚未连接服务器,无法浏览FTP资源");m_connect.EnableWindow(false);m_disconnect.EnableWindow(false);m_enterdir.EnableWindow(true);m_upload.EnableWindow(true);m_download.EnableWindow(true);m_delete.EnableWindow(true);。