《MFC编程:FTP多线程网络编程实验(2)》要点:
本文介绍了MFC编程:FTP多线程网络编程实验(2),希望对您有用。如果有疑问,可以联系我们。
//定义一个结构,用于传递数据至控制函数中
typedef struct {
CListBox* pList; //列表框句柄
CString strFtpSite; //IP
CString strName; //姓名
CString strPwd; //密码
}FTP_INFO;
BOOL mtDownload(CString strFtpSite,CString strName,CString strPwd,CString strSourceName,CString strDestName);
BOOL mtUpload(CString strFtpSite,CString strName,CString strPwd,CString strSourceName,CString strDestName);
UINT mtQuery(LPVOID pParam)
{
if (pParam==NULL) AfxEndThread(NULL);
//这一段代码是用来获取函数调用的参数的,用法非常典型,函数调用的入口参数
//pParam是一个LPVOID类型的指针,必须将它化为FTP_INFO结构类型的指针
//变量,才能从中取出相应的数据成员
FTP_INFO*PP;
CListBox* pList;
CString strFtpSite;
CString strName;
CString strPwd;
PP=(FTP_INFO*)pParam;
pList=PP->pList;
strFtpSite=PP->strFtpSite;
strName=PP->strName;
strPwd=PP->strPwd;
CInternetSession*pSession;
CFtpConnection*pConnection;
CFtpFileFind*pFileFind;
CString strFileName;
BOOL bContinue;
pConnection=NULL;
pSession = new CInternetSession;
try
{
pConnection=
pSession->GetFtpConnection(strFtpSite,strName,strPwd,21,TRUE);
}catch (CInternetSession*e) {
e->Close();
pConnection=NULL;
}
if(pConnection!=NULL)
{
pFileFind=new CFtpFileFind(pConnection);
bContinue=pFileFind->FindFile("*");
if(!bContinue)
{
pFileFind->Close();
pFileFind=NULL;
}
bContinue = pFileFind->FindNextFile();
while (bContinue)
{
strFileName=pFileFind->GetFileName();
if(pFileFind->IsDirectory()) strFileName="["+strFileName+"]";
pList->AddString(strFileName);
bContinue = pFileFind->FindNextFile();
}
if(pFileFind!=NULL)
{
pFileFind->Close();
pFileFind=NULL;
}
}
delete pFileFind;
if(pConnection!=NULL)
{
pConnection->Close();
delete pConnection;
}
delete pSession;
return 0;
}
UINT mtDownloadFile(LPVOID pParam)
{
if (pParam==NULL) AfxEndThread(NULL);
FTP_INFO*PP;
CListBox* pList;
CString strFtpSite;
CString strName;
CString strPwd;
PP=(FTP_INFO*)pParam;
pList=PP->pList;
strFtpSite=PP->strFtpSite;
strName=PP->strName;
strPwd=PP->strPwd;
int nSel=pList->GetCurSel();
CString strSourceName;
pList->GetText(nSel,strSourceName);
if(strSourceName.GetAt(0)!='[')
{
CString strDestName;
CFileDialog dlg(FALSE,"","*.*");
if(dlg.DoModal()==IDOK)
{
strDestName=dlg.GetPathName();
if(mtDownload(strFtpSite,strName,strPwd,
strSourceName,strDestName))
AfxMessageBox("下载成功!",MB_OK|MB_ICONINFORMATION);
else{
AfxMessageBox("下载失败!",MB_OK|MB_ICONSTOP);
return FALSE;
}
}else{AfxMessageBox("请写入文件名!",MB_OK|MB_ICONSTOP);
return FALSE;
}
}else{AfxMessageBox("不能下载目录!\n请重选!",MB_OK|MB_ICONSTOP);
return FALSE;
}
return 0;
}
//下载文件调用函数
BOOL mtDownload(CString strFtpSite,CString strName,CString strPwd,CString strSourceName,CString strDestName)
{
CInternetSession*pSession;
CFtpConnection*pConnection;
pConnection=NULL;
pSession=new CInternetSession(AfxGetAppName(),1,PRE_CONFIG_INTERNET_ACCESS);
try
{
pConnection=pSession->GetFtpConnection(strFtpSite,strName,strPwd,21,TRUE);
}
catch(CInternetException*e)
{
e->Delete();
pConnection=NULL;
return FALSE;
}
if(pConnection!=NULL)
{
if(!pConnection->GetFile(strSourceName,strDestName))
{
pConnection->Close();
delete pConnection;
delete pSession;
return FALSE;
}
}
if(pConnection!=NULL)
{
pConnection->Close();
delete pConnection;
}
delete pSession;
return TRUE;
}
//用于上传的线程函数
UINT mtUploadFile(LPVOID pParam)
{
if (pParam==NULL) AfxEndThread(NULL);
FTP_INFO*PP;
CListBox* pList;
CString strFtpSite;
CString strName;
CString strPwd;
PP=(FTP_INFO*)pParam;
pList=PP->pList;
strFtpSite=PP->strFtpSite;
strName=PP->strName;
strPwd=PP->strPwd;
CString strSourceName;
CString strDestName;
CFileDialog dlg(TRUE,"","*.*");
if(dlg.DoModal()==IDOK)
{
strSourceName=dlg.GetPathName();
strDestName=dlg.GetFileName();
if(mtUpload(strFtpSite,strName,strPwd,strSourceName,strDestName))
AfxMessageBox("上传成功!",MB_OK|MB_ICONINFORMATION);
else
AfxMessageBox("上传失败!",MB_OK|MB_ICONSTOP);
}else{
AfxMessageBox("请选择文件!",MB_OK|MB_ICONSTOP);
}return 0;
}
//上传文件调用的函数
BOOL mtUpload(CString strFileSite,CString strName,CString strPwd,CString strSourceName,CString strDestName)
{
CInternetSession*pSession;
CFtpConnection*pConnection;
pConnection=NULL;
pSession=new CInternetSession(AfxGetAppName(),1,PRE_CONFIG_INTERNET_ACCESS);
try
{
pConnection=pSession->GetFtpConnection(strFileSite,strName,strPwd,21,TRUE);
}
catch (CInternetException*e)
{
e->Delete();
pConnection=NULL;
AfxMessageBox("上传失败!001",MB_OK|MB_ICONSTOP);
return FALSE;
}
if(pConnection!=NULL)
{
if (!pConnection->PutFile(strSourceName,strDestName))
{
//上传文件错误
pConnection->Close();
delete pConnection;
delete pSession;
AfxMessageBox("上传失败!002",MB_OK|MB_ICONSTOP);
return FALSE;
}
}
//清除对象
if(pConnection!=NULL)
{
pConnection->Close();
delete pConnection;
}
delete pSession;
return TRUE;
}
转载请注明本页网址:
http://www.vephp.com/jiaocheng/94_2.html