VC FTP多线程上传下载

来源:互联网 发布:手机淘宝支付过程 编辑:程序博客网 时间:2024/06/07 20:19
#include "FtpDlg.h"typedef struct{CListBox* pList;CString strFtpSite;CString strName;CString strPwd;}FTP_INFO;UINT ListContentMT(LPVOID pParam){if (pParam==NULL)AfxEndThread(NULL);//获得参数FTP_INFO* pInfo;CListBox* pList;CString strFtpSite;CString strName;CString strPwd;pInfo=(FTP_INFO*)pParam;pList=pInfo->pList;strFtpSite=pInfo->strFtpSite;strName=pInfo->strName;strPwd=pInfo->strPwd;CInternetSession* pSession;CFtpConnection* pConnection;CFtpFileFind* pFileFind;CString strFileName;BOOL bContinue;pConnection=NULL;pFileFind=NULL;//创建Internet会话pSession=new CInternetSession(AfxGetAppName(),1,PRE_CONFIG_INTERNET_ACCESS);try{//试图建立FTP连接pConnection=pSession->GetFtpConnection(strFtpSite,strName,strPwd);}catch(CInternetException* e){//错误处理e->Delete();pConnection=NULL;}if (pConnection!=NULL){//创建CFtpFileFind对象//传递CFtpConnection对象指针pFileFind=new CFtpFileFind(pConnection);//查找任意文件bContinue=pFileFind->FindFile("*");if (!bContinue){//查找完毕pFileFind->Close();pFileFind=NULL;}while (bContinue){//查找下一个文件bContinue=pFileFind->FindNextFile();//获得找到的文件的文件名strFileName=pFileFind->GetFileName();if(pFileFind->IsDirectory()){//找到的是否为目录//标记目录名strFileName="["+strFileName;strFileName+="]";}//显示查找的内容pList->AddString(strFileName);}if(pFileFind!=NULL){//查询结束pFileFind->Close();pFileFind=NULL;}}//删除对象delete pFileFind;if (pConnection!=NULL){pConnection->Close();delete pConnection;}delete pSession;return 0;}//下载文件调用的函数BOOL GetFileMT(CString strFtpSite,CString strName,CString strPwd,CString strSourceName,CString strDestName){CInternetSession* pSession;CFtpConnection* pConnection;pConnection=NULL;//创建Internet会话pSession=new CInternetSession(AfxGetAppName(),1,PRE_CONFIG_INTERNET_ACCESS);try{//试图建立FTP连接pConnection=pSession->GetFtpConnection(strFtpSite,strName,strPwd);}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 DownloadFileMT(LPVOID pParam){if (pParam==NULL)AfxEndThread(NULL);FTP_INFO* pInfo;CListBox* pList;CString strFtpSite;CString strName;CString strPwd;pInfo=(FTP_INFO*)pParam;pList=pInfo->pList;strFtpSite=pInfo->strFtpSite;strName=pInfo->strName;strPwd=pInfo->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 (GetFileMT(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 PutFileMT(CString strFtpSite,CString strName,CString strPwd,CString strSourceName,CString strDestName){CInternetSession* pSession;CFtpConnection* pConnection;pConnection=NULL;//创建Internet会话pSession=new CInternetSession(AfxGetAppName(),1,PRE_CONFIG_INTERNET_ACCESS);try{//试图建立FTP连接pConnection=pSession->GetFtpConnection(strFtpSite,strName,strPwd);}catch(CInternetException* e){//错误处理e->Delete();pConnection=NULL;return FALSE;}if (pConnection!=NULL){//上传文件if(!pConnection->PutFile(strSourceName,strDestName)){//上传文件错误pConnection->Close();delete pConnection;delete pSession;return FALSE;}}//清除对象if (pConnection!=NULL){pConnection->Close();delete pConnection;}delete pSession;return TRUE;}UINT UploadFileMT(LPVOID pParam){if (pParam==NULL)AfxEndThread(NULL);FTP_INFO* pInfo;CString strFtpSite;CString strName;CString strPwd;pInfo=(FTP_INFO*)pParam;strFtpSite=pInfo->strFtpSite;strName=pInfo->strName;strPwd=pInfo->strPwd;CString strSourceName;CString strDestName;CFileDialog dlg(TRUE,"","*.*");if (dlg.DoModal()==IDOK){//获得待上传的本地机文件路径和文件名strSourceName=dlg.GetPathName();strDestName=dlg.GetFileName();//调用函数上传文件if (PutFileMT(strFtpSite,strName,strPwd,strSourceName,strDestName))AfxMessageBox("上传成功!",MB_OK|MB_ICONINFORMATION);else AfxMessageBox("上传失败!",MB_OK|MB_ICONSTOP);}else{//文件选择有错误AfxMessageBox("请选择文件!",MB_OK|MB_ICONSTOP);}return 0;}

原创粉丝点击