📄 ftp.cpp
字号:
CFtpParaClass:: FILE_FTP_INFO* pInf=new (CFtpParaClass::FILE_FTP_INFO);
CFtpParaClass::FILE_COUNT_INFO* pInfo=(CFtpParaClass::FILE_COUNT_INFO*)lParam;
BOOL Result;
CString str,strFtp,OldLoc,OldFtp;
HINTERNET hInetSession=InternetOpen(szAppName,INTERNET_OPEN_TYPE_PRECONFIG,NULL,NULL,0);
HINTERNET hFtpConn=InternetConnect(hInetSession,szFtpName,nPort,
szFtpUser,szFtpPassword,INTERNET_SERVICE_FTP,INTERNET_FLAG_PASSIVE,0);
if(!hFtpConn)
{
AfxMessageBox("建立联接出错,返回");
InternetCloseHandle(hInetSession);
return 0L;
}
szFtpInfo="正在进入";
szFtpInfo+=szFtpDirectory;
pFtpInfoView->PostMessage(WM_RECORDFTPINFO,0,(LPARAM)(LPCTSTR)szFtpInfo);
FtpSetCurrentDirectory(hFtpConn,szFtpDirectory);
::SetCurrentDirectory(szLocDirectory);
OldLoc=szLocDirectory;
OldFtp=szFtpDirectory;
for(int i=0;i<nCount;i++)
{
str=OldLoc;
strFtp=OldFtp;
CString DirName=pInfo[i].fileName;
if(pInfo[i].ufileFlag==FILEICON)
{
szFtpInfo="正在接收";
szFtpInfo+=DirName;
pFtpInfoView->PostMessage(WM_RECORDFTPINFO,0,(LPARAM)(LPCTSTR)szFtpInfo);
if(FtpGetFile(hFtpConn,DirName,DirName,FALSE,FILE_ATTRIBUTE_NORMAL,FTP_TRANSFER_TYPE_BINARY |
INTERNET_FLAG_NO_CACHE_WRITE,0))
{
if(IsFree)
{
IsFree=FALSE;
strcpy(pInf->szFileName,DirName);
strcpy(pInf->szFileDate,"");
strcpy(pInf->szFileSize,"");
pInf->nType=FILEICON;
::PostMessage(pLocView->m_hWnd,WM_SETFILE,(WPARAM)pInf,(LPARAM)SERVFILE);
}
szFtpInfo="接收";
szFtpInfo+=DirName;
szFtpInfo+="完毕,下一个";
pFtpInfoView->PostMessage(WM_RECORDFTPINFO,0,(LPARAM)(LPCTSTR)szFtpInfo);
continue;
}
else
{
szFtpInfo="接收";
szFtpInfo+=DirName;
szFtpInfo+="出错,退出查找原因";
pFtpInfoView->PostMessage(WM_RECORDFTPINFO,0,(LPARAM)(LPCTSTR)szFtpInfo);
break;
}
}
else
{
if(str.Right(1)=="\\")
str+=DirName;
else
str+="\\"+DirName;
if(strFtp.Right(1)=='/')
strFtp+=DirName;
else
strFtp+="/"+DirName;
if(Result=PreReceiveFile(hInetSession,str,strFtp,(LPVOID)pInf))
continue;
else
{
AfxMessageBox("发送有错,请查明原因");
break;
}
}
}
szFtpInfo="文件接收结束,退回";
szFtpInfo+=OldLoc;
pFtpInfoView->PostMessage(WM_RECORDFTPINFO,0,(LPARAM)(LPCTSTR)szFtpInfo);
::Sleep(20);
if(IsFree)
{
::SendMessage(pLocView->m_hWnd,WM_REDISPLAYFILE,(WPARAM)(LPCTSTR)OldLoc,0);
}
delete[] pInfo;
delete pInf;
InternetCloseHandle(hFtpConn);
InternetCloseHandle(hInetSession);
return 0L;
}
UINT CMyAppThread::DeleteFileThread(LPVOID lParam)
{
IsFree=TRUE;
CString szFtpInfo;
CFtpParaClass::FILE_COUNT_INFO* pInfo=(CFtpParaClass::FILE_COUNT_INFO*)lParam;
BOOL Result;
CString strFtp,OldFtp;
HINTERNET hInetSession=InternetOpen(szAppName,INTERNET_OPEN_TYPE_PRECONFIG,NULL,NULL,0);
szFtpInfo="正在连接";
szFtpInfo+=szFtpName;
pFtpInfoView->PostMessage(WM_RECORDFTPINFO,0,(LPARAM)(LPCTSTR)szFtpInfo);
HINTERNET hFtpConn=InternetConnect(hInetSession,szFtpName,nPort,
szFtpUser,szFtpPassword,INTERNET_SERVICE_FTP,INTERNET_FLAG_PASSIVE,0);
if(!hFtpConn)
{
szFtpInfo="未连接上";
szFtpInfo+=szFtpName;
szFtpInfo+="服务器";
pFtpInfoView->PostMessage(WM_RECORDFTPINFO,0,(LPARAM)(LPCTSTR)szFtpInfo);
::Sleep(10);
InternetCloseHandle(hInetSession);
return 0L;
}
szFtpInfo="成功连上";
szFtpInfo+=szFtpName;
szFtpInfo+="服务器,现在进入";
szFtpInfo+=szFtpDirectory;
pFtpInfoView->PostMessage(WM_RECORDFTPINFO,0,(LPARAM)(LPCTSTR)szFtpInfo);
FtpSetCurrentDirectory(hFtpConn,szFtpDirectory);
OldFtp=szFtpDirectory;
for(int i=0;i<nCount;i++)
{
strFtp=OldFtp;
CString DirName=pInfo[i].fileName;
if(pInfo[i].ufileFlag==FILEICON)
{
szFtpInfo="正在删除";
szFtpInfo+=DirName;
pFtpInfoView->PostMessage(WM_RECORDFTPINFO,0,(LPARAM)(LPCTSTR)szFtpInfo);
if(FtpDeleteFile(hFtpConn,DirName))
{
continue;
}
else
{
szFtpInfo="删除";
szFtpInfo+=DirName;
szFtpInfo+="出错,返回";
pFtpInfoView->PostMessage(WM_RECORDFTPINFO,0,(LPARAM)(LPCTSTR)szFtpInfo);
break;
}
}
else
{
if(strFtp.Right(1)=='/')
strFtp+=DirName;
else
strFtp+="/"+DirName;
if(Result=PreDeleteFile(hInetSession,strFtp,OldFtp))
continue;
else
{
AfxMessageBox("删除出错,请查明原因");
break;
}
}
}
szFtpInfo="文件删除结束";
pFtpInfoView->PostMessage(WM_RECORDFTPINFO,0,(LPARAM)(LPCTSTR)szFtpInfo);
::Sleep(10);
if(IsFree)
::SendMessage(pServView->m_hWnd,WM_REDISPLAYFILE,(WPARAM)(LPCTSTR)OldFtp,0);
delete[] pInfo;
InternetCloseHandle(hFtpConn);
InternetCloseHandle(hInetSession);
return 0L;
}
UINT CMyAppThread::RenameFileThread(LPVOID lParam)
{
CString szFtpInfo;
CFtpParaClass::FILE_CHANGE_INFO* pInfo=(CFtpParaClass::FILE_CHANGE_INFO*)lParam;
HINTERNET hInetSession=InternetOpen(szAppName,INTERNET_OPEN_TYPE_PRECONFIG,NULL,NULL,0);
szFtpInfo="正在连接";
szFtpInfo+=szFtpName;
szFtpInfo+="服务器";
pFtpInfoView->PostMessage(WM_RECORDFTPINFO,0,(LPARAM)(LPCTSTR)szFtpInfo);
HINTERNET hFtpConn=InternetConnect(hInetSession,szFtpName,nPort,
szFtpUser,szFtpPassword,INTERNET_SERVICE_FTP,INTERNET_FLAG_PASSIVE,0);
if(!hFtpConn)
{
szFtpInfo="未连上";
szFtpInfo+=szFtpName;
szFtpInfo+="服务器,请查明原因,目前只能返回";
pFtpInfoView->PostMessage(WM_RECORDFTPINFO,0,(LPARAM)(LPCTSTR)szFtpInfo);
InternetCloseHandle(hInetSession);
::Sleep(10);
return 0L;
}
szFtpInfo=szFtpName;
szFtpInfo+="服务器连接成功,现进行改名操作";
pFtpInfoView->PostMessage(WM_RECORDFTPINFO,0,(LPARAM)(LPCTSTR)szFtpInfo);
FtpSetCurrentDirectory(hFtpConn,szFtpDirectory);
szFtpInfo="正在把";
szFtpInfo+=pInfo->OldName;
szFtpInfo+="改为";
szFtpInfo+=pInfo->NewName;
pFtpInfoView->PostMessage(WM_RECORDFTPINFO,0,(LPARAM)(LPCTSTR)szFtpInfo);
FtpRenameFile(hFtpConn,pInfo->OldName,pInfo->NewName);
szFtpInfo="改名成功";
pFtpInfoView->PostMessage(WM_RECORDFTPINFO,0,(LPARAM)(LPCTSTR)szFtpInfo);
::Sleep(10);
delete pInfo;
InternetCloseHandle(hFtpConn);
InternetCloseHandle(hInetSession);
return 0L;
}
BOOL CMyAppThread::PreDeleteFile(HINTERNET hInetSession, CString& strFtp,CString& OldDir)
{
CString szFtpInfo;
BOOL result;
DWORD dwSize=MAX_PATH;
CString tFile;
WIN32_FIND_DATA findData;
HINTERNET hFind;
HINTERNET hFtpConn=InternetConnect(hInetSession,szFtpName,nPort,szFtpUser,szFtpPassword,INTERNET_SERVICE_FTP,INTERNET_FLAG_PASSIVE,0);
if(!hFtpConn)
{
AfxMessageBox("建立联接出错,返回");
InternetCloseHandle(hInetSession);
return FALSE;
}
szFtpInfo="正在进入";
szFtpInfo+=strFtp;
szFtpInfo+="目录";
pFtpInfoView->PostMessage(WM_RECORDFTPINFO,0,(LPARAM)(LPCTSTR)szFtpInfo);
FtpSetCurrentDirectory(hFtpConn,strFtp);
szFtpInfo="进入";
szFtpInfo+=strFtp;
szFtpInfo+="目录成功";
pFtpInfoView->PostMessage(WM_RECORDFTPINFO,0,(LPARAM)(LPCTSTR)szFtpInfo);
if(!(hFind=FtpFindFirstFile(hFtpConn,_T("*"),&findData,0,0)))
{
if (GetLastError()!= ERROR_NO_MORE_FILES)
result=FALSE;
else
result=TRUE;
goto end;
}
do
{
tFile=findData.cFileName;
if(tFile=="."||tFile=="..")continue;
if(findData.dwFileAttributes==FILE_ATTRIBUTE_DIRECTORY)
{
if(!(result=PreDeleteFile(hInetSession,strFtp+"/"+tFile,strFtp)))
{
AfxMessageBox("删除出错,返回");
break;
}
}
else
{
szFtpInfo="正在删除";
szFtpInfo+=tFile;
pFtpInfoView->PostMessage(WM_RECORDFTPINFO,0,(LPARAM)(LPCTSTR)szFtpInfo);
result=(FtpDeleteFile(hFtpConn,tFile))?TRUE:FALSE;
if(!result)
{
szFtpInfo="删除"+tFile+"出错";
pFtpInfoView->PostMessage(WM_RECORDFTPINFO,0,(LPARAM)(LPCTSTR)szFtpInfo);
break;
}
}
}while(InternetFindNextFile(hFind,&findData));
FtpSetCurrentDirectory(hFtpConn,OldDir);
szFtpInfo="正在删除"+strFtp+"目录";
pFtpInfoView->PostMessage(WM_RECORDFTPINFO,0,(LPARAM)(LPCTSTR)szFtpInfo);
FtpRemoveDirectory(hFtpConn,strFtp);
InternetCloseHandle(hFind);
InternetCloseHandle(hFtpConn);
return result;
end:
InternetCloseHandle(hFtpConn);
return result;
}
BOOL CMyAppThread::PreReceiveFile(HINTERNET hInetSession, CString& str,CString& strFtp,LPVOID pInf)
{
CFtpParaClass:: FILE_FTP_INFO* pInfo=(CFtpParaClass:: FILE_FTP_INFO*)pInf;
BOOL result;
UINT uSize;
DWORD dwSize=MAX_PATH;
HANDLE hHeap;
LPSTR lpBuffer;
CString lpBufferFtp,tFile;
WIN32_FIND_DATA findData;
HINTERNET hFind;
HINTERNET hFtpConn=InternetConnect(hInetSession,szFtpName,nPort,szFtpUser,szFtpPassword,INTERNET_SERVICE_FTP,INTERNET_FLAG_PASSIVE,0);
if(!hFtpConn)
{
AfxMessageBox("建立联接出错,返回");
InternetCloseHandle(hInetSession);
return FALSE;
}
uSize=(GetCurrentDirectory(0,NULL)*sizeof(TCHAR));
hHeap=GetProcessHeap();
lpBuffer=(LPSTR)HeapAlloc(hHeap,HEAP_ZERO_MEMORY,uSize);
GetCurrentDirectory(uSize,lpBuffer);
FtpSetCurrentDirectory(hFtpConn,strFtp);
::CreateDirectory(str,NULL);
::SetCurrentDirectory(str);
::SendMessage(pLocView->m_hWnd,WM_SETDIR,0,(LPARAM)(LPCTSTR)str);
if(!(hFind=FtpFindFirstFile(hFtpConn,_T("*"),&findData,0,0)))
{
if (GetLastError()!= ERROR_NO_MORE_FILES)
result=FALSE;
else
result=TRUE;
goto end;
}
do
{
tFile=findData.cFileName;
if(tFile=="."||tFile==".."||findData.dwFileAttributes==FILE_ATTRIBUTE_HIDDEN)continue;
if(findData.dwFileAttributes==FILE_ATTRIBUTE_DIRECTORY)
{
if(!(result=PreReceiveFile(hInetSession,str+"\\"+tFile,strFtp+"/"+tFile,pInf)))
{
AfxMessageBox("文件发送出错,返回");
break;
}
}
else
{
result=(FtpGetFile(hFtpConn,tFile,tFile,FALSE,FILE_ATTRIBUTE_NORMAL,FTP_TRANSFER_TYPE_BINARY|
INTERNET_FLAG_NO_CACHE_WRITE,0))?TRUE:FALSE;
if(!result)
{
AfxMessageBox("文件发送出错,返回");
break;
}
if(IsFree)
{
IsFree=FALSE;
strcpy(pInfo->szFileName,tFile);
strcpy(pInfo->szFileDate,"");
strcpy(pInfo->szFileSize,"");
pInfo->nType=FILEICON;
::PostMessage(pLocView->m_hWnd,WM_SETFILE,(WPARAM)pInfo,SERVFILE);
}
}
}while(InternetFindNextFile(hFind,&findData));
SetCurrentDirectory(lpBuffer);
::SendMessage(pLocView->m_hWnd,WM_SETDIR,0,(LPARAM)lpBuffer);
InternetCloseHandle(hFind);
HeapFree(hHeap,HEAP_NO_SERIALIZE,lpBuffer);
InternetCloseHandle(hFtpConn);
return result;
end:
::SetCurrentDirectory(lpBuffer);
::SendMessage(pLocView->m_hWnd,WM_SETDIR,0,(LPARAM)lpBuffer);
HeapFree(hHeap,HEAP_NO_SERIALIZE,lpBuffer);
InternetCloseHandle(hFtpConn);
return result;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -