FTP的界面设计如下:
定义的对应变量如下,已经设置了对应的按钮响应事件:
相关函数完成的代码如下:
void CMFCApplication1Dlg::OnConnect() //连接
{
// TODO: 在此添加控件通知处理程序代码
this->ConnectFtp();
this->UpdateDir();
FtpIP.EnableWindow(FALSE);
FtpLogin.EnableWindow(FALSE);
FtpDisconnect.EnableWindow(TRUE);
FileInside.EnableWindow(TRUE);
FileUpload.EnableWindow(TRUE);
FileDownload.EnableWindow(TRUE);
FileDelete.EnableWindow(TRUE);
FtpNonameLog.EnableWindow(FALSE);
}
void CMFCApplication1Dlg::OnEnterDir() //进入文件夹
{
// TODO: 在此添加控件通知处理程序代码
CString selfile;
//获取用户选择的目录名
FileName.GetText(FileName.GetCurSel(), selfile);
if (!selfile.IsEmpty())
{
pFtpConnection->Close(); //关闭废弃的会话句柄
this->ConnectFtp(); //重新连接
pFtpConnection->SetCurrentDirectory(selfile); //改变目录到当前目录
this->UpdateDir(); //更新目录列表
}
}
void CMFCApplication1Dlg::OnGoBack() //返回上一级文件
{
// TODO: 在此添加控件通知处理程序代码
CString strdir;
pFtpConnection->GetCurrentDirectory(strdir);
int pos;
//用字符串截取的方法获得上一级目录
pos = strdir.ReverseFind('/');
strdir = strdir.Left(pos);
pInternetSession->Close(); //关闭废弃会话
this->ConnectFtp();
pFtpConnection->SetCurrentDirectory(strdir);
this->UpdateDir();//更新目录
}
void CMFCApplication1Dlg::OnDisconnect() //断开连接
{
// TODO: 在此添加控件通知处理程序代码
pInternetSession->Close();
FileName.ResetContent();
FileName.AddString(L"连接已经断开!!");
FtpIP.EnableWindow(true);
FtpLogin.EnableWindow(true);
FtpDisconnect.EnableWindow(false);
FileInside.EnableWindow(false);
FileOutside.EnableWindow(false);
FileUpload.EnableWindow(false);
FileDownload.EnableWindow(false);
FileDelete.EnableWindow(false);
FtpNonameLog.EnableWindow(true);
}
void CMFCApplication1Dlg::OnUpLoad() //上传文件
{
// TODO: 在此添加控件通知处理程序代码
CString str;
CString strname;
//弹出“打开”对话框
CFileDialog file(true, NULL, NULL, OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT, L"所有文件(*.*)|*.*|", 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(_T("上传成功!"));
}
}
}
void CMFCApplication1Dlg::OnDownload() //下载文件
{
// TODO: 在此添加控件通知处理程序代码
CString selfile;
FileName.GetText(FileName.GetCurSel(), selfile);//获得想要下载资源名
if (!selfile.IsEmpty())
{
//弹出另存为对话框
CFileDialog file(FALSE, NULL, selfile, OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT, L"所有文件(*.*)|*.*|", this);
if (file.DoModal() == IDOK)
{
CString strname;
strname = file.GetFileName();
CString strdir;
pFtpConnection->GetCurrentDirectory(strdir);
pFtpConnection->GetFile(selfile, strname);//下载文件到的本地位置
pInternetSession->Close();
this->ConnectFtp();
pFtpConnection->SetCurrentDirectory(strdir);
this->UpdateDir();
AfxMessageBox(_T("下载成功!"));
}
}
}
void CMFCApplication1Dlg::OnDelete() //删除文件
{
// TODO: 在此添加控件通知处理程序代码
CString selfile;
FileName.GetText(FileName.GetCurSel(), selfile);//获取用户要删除的资源名
if (!selfile.IsEmpty())
{
if (AfxMessageBox(L"确定要删除这个文件?", 4 + 48) == 6)
{
pFtpConnection->Remove(selfile);
}
CString strdir;
pFtpConnection->GetCurrentDirectory(strdir);
pInternetSession->Close();
this->ConnectFtp();
pFtpConnection->SetCurrentDirectory(strdir);
this->UpdateDir();
}
}
void CMFCApplication1Dlg::OnNoName() //匿名登陆
{
// TODO: 在此添加控件通知处理程序代码
int icheck = FtpNonameLog.GetCheck();
if (icheck == 1)
{
//匿名登陆
Ftpuser.EnableWindow(FALSE);
FtpPassword.EnableWindow(FALSE);
Ftpuser.SetWindowText(L"anonymous");
FtpPassword.SetWindowText(L"");
UpdateData();
CString str;
FtpIP.GetWindowText(str);
if (!(str == "")) //如果IP不为空,可以连接
{
FtpLogin.EnableWindow(TRUE);
}
}
else //不按要求输入不能连接
{
Ftpuser.EnableWindow(TRUE);
FtpPassword.EnableWindow(TRUE);
Ftpuser.SetWindowText(L"");
FtpPassword.SetWindowText(L"");
FtpLogin.EnableWindow(FALSE);
}
}
void CMFCApplication1Dlg::ConnectFtp() //FTP连接
{
BYTE nFild[4];
UpdateData();
CString sip,strusr,strpwd;
Ftpuser.GetWindowText(strusr);
FtpPassword.GetWindowText(strpwd);
FtpIP.GetWindowText(sip);
if (sip.IsEmpty())
{
AfxMessageBox(_T("IP地址为空!"));
return;
}
pInternetSession = new CInternetSession(L"MR", INTERNET_OPEN_TYPE_PRECONFIG);
try
{
pFtpConnection = pInternetSession->GetFtpConnection(sip, strusr, strpwd, atoi("21"));
bconnect=true;
}
catch (CInternetException* pEx)
{
TCHAR szErr[1024];
pEx->GetErrorMessage(szErr, 1024);
AfxMessageBox(szErr);
pEx->Delete();
}
}
void CMFCApplication1Dlg::UpdateDir() //更新列表的文件目录
{
FileName.ResetContent();
//创建一个CFtpFileFind实例
CFtpFileFind ftpfind(pFtpConnection);
CString strdirpath;
pFtpConnection->GetCurrentDirectory(strdirpath);
//找到第一个文件或者文件夹
BOOL bfind = ftpfind.FindFile(strdirpath);
while (bfind)
{
bfind = ftpfind.FindNextFile();
CString strpath;
if (ftpfind.IsDots())
continue;
if (!ftpfind.IsDirectory()) //判断是目录还是文件
{
strpath = ftpfind.GetFileName(); //文件则读取文件名
FileName.AddString(strpath);
}
else
{
strpath = ftpfind.GetFilePath();
FileName.AddString(strpath);
}
}
}