⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 mainfrm.cpp

📁 ftp客户端
💻 CPP
📖 第 1 页 / 共 5 页
字号:
		m_ctLocFile.SetItemData(0,DIRECTORYICON);
		m_bHaveDotFlag=FALSE;
	}
	m_ctLocFile.Invalidate();
	m_ctLocFile.SetRedraw();
	return 0L;	
}

LRESULT CLocFileView::OnReDisplayLocFile(WPARAM wParam,LPARAM lParam)
{
	CString str=(LPCTSTR)lParam;
	OnSetLocDirectoryDisplay(0,lParam);
	CMyAppThread* pThread=new CMyAppThread(str);
	if(!BeginFindLocFile(pThread))
		AfxMessageBox("建立网络线程失败");
	return 0L;
}

LRESULT CLocFileView::OnInsertLocFile(WPARAM wParam,LPARAM lParam)
{
	FILE_FTP_INFO* pInfo=(FILE_FTP_INFO*)wParam;
	CString str=pInfo->szFileName;
	if(str=="."||str=="..")
	{
		m_bHaveDotFlag=TRUE;
		return 0L;
	}
	OnInsertFile(&m_ctLocFile,pInfo,lParam);
	return 0L;
}
/////////////////////////////////////////////////////////////////////////////
// CMyAppThread

IMPLEMENT_DYNCREATE(CMyAppThread, CWinThread)

CMyAppThread::CMyAppThread()
{
	
}


CMyAppThread::~CMyAppThread()
{
	if(hFtpConn)
		InternetCloseHandle(hFtpConn);	
}



BOOL CMyAppThread::InitInstance()
{
	// TODO:  perform and per-thread initialization here
	return TRUE;
}

int CMyAppThread::ExitInstance()
{
	// TODO:  perform any per-thread cleanup here
	return CWinThread::ExitInstance();
}

BEGIN_MESSAGE_MAP(CMyAppThread, CWinThread)
	//{{AFX_MSG_MAP(CMyAppThread)
		// NOTE - the ClassWizard will add and remove mapping macros here.
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CMyAppThread message handlers

UINT CMyAppThread::StartRenameFileThread(LPVOID lParam)
{
	CFtpParaClass::FILE_CHANGE_INFO* pInfo=(CFtpParaClass::FILE_CHANGE_INFO*)lParam;
	CMyAppThread* pThis=(CMyAppThread*)pInfo->pThread;
	return pThis->RenameFileThread(lParam);


}
UINT CMyAppThread::StartDeleteFileThread(LPVOID lParam)
{
	CFtpParaClass::SEND_RECEIVE_INFO* pInfo=(CFtpParaClass::SEND_RECEIVE_INFO*)lParam;
	CMyAppThread* pThis=(CMyAppThread*)pInfo->pThread;
	return pThis->DeleteFileThread(lParam);
}

UINT CMyAppThread::StartReceiveFileThread(LPVOID lParam)
{
	CFtpParaClass::SEND_RECEIVE_INFO* pInfo=(CFtpParaClass::SEND_RECEIVE_INFO*)lParam;
	CMyAppThread* pThis=(CMyAppThread*)pInfo->pThread;
	return pThis->ReceiveFileThread(lParam);
}

UINT CMyAppThread::StartSendFileThread(LPVOID lParam)
{
	CFtpParaClass::SEND_RECEIVE_INFO* pInfo=(CFtpParaClass::SEND_RECEIVE_INFO*)lParam;
	CMyAppThread* pThis=(CMyAppThread*)pInfo->pThread;
	return pThis->SendFileThread(lParam);
}

UINT CMyAppThread::StartPreSendFileThread(LPVOID lParam)
{
	CMyAppThread* pThis=(CMyAppThread*)lParam;
	return pThis->PreSendFileThread(lParam);
}

UINT CMyAppThread::StartPreReceiveFileThread(LPVOID lParam)
{
	CMyAppThread* pThis=(CMyAppThread*)lParam;
	return pThis->PreRecieveFileThread(lParam);
}

UINT CMyAppThread::StartPreDeleteFileThread(LPVOID lParam)
{
	CMyAppThread* pThis=(CMyAppThread*)lParam;
	return pThis->PreDeleteFileThread(lParam);
}
	
UINT CMyAppThread::StartFindLocFileThread(LPVOID lParam)
{
	CMyAppThread* pThis=(CMyAppThread*)lParam;
	return pThis->FindLocFileThread(lParam);
}

UINT CMyAppThread::StartFindServFileThread(LPVOID lParam)
{
	CMyAppThread* pThis=(CMyAppThread*)lParam;
	return pThis->FindServFileThread(lParam);
}

UINT CMyAppThread::FindLocFileThread(LPVOID lParam)
{
	CMyAppThread* pThread=(CMyAppThread*)lParam;
	CFtpParaClass::FILE_FTP_INFO* pInfo=new (CFtpParaClass::FILE_FTP_INFO);
	CFileFind fileFind;
	CString str;
	UINT i=0;
	CTime time;
	BOOL bContinue;
	::SetCurrentDirectory(m_szLocDirectory);
	::GetCurrentDirectory(MAX_PATH,(LPTSTR)(LPCTSTR)m_szLocDirectory);
	strcpy(pInfo->szFilePath,m_szLocDirectory);
	::PostMessage(pLocView->m_hWnd,WM_SETDIRECTORYDISPLAY,0,(LPARAM)(LPCSTR)m_szLocDirectory);
	bContinue=fileFind.FindFile("*.*");
	if(!bContinue)
		goto end;
	while(bContinue)
	{
		bContinue=fileFind.FindNextFile();
		if(fileFind.IsHidden())continue;
		str=fileFind.GetFileName();
		strcpy(pInfo->szFileName,str);
		fileFind.GetLastWriteTime(time);
		str=time.Format("%x");
		strcpy(pInfo->szFileDate,str);
		if(fileFind.IsDirectory())
		{
			strcpy(pInfo->szFileSize,"<DIR>");
			pInfo->nType=DIRECTORYICON;
			
		}
		else
		{
			int i=fileFind.GetLength();
			if(i>1024)
			{
				str.Format("%d",i/1024);
				str+="KB";
			}
			else
				str.Format("%d",i);
			strcpy(pInfo->szFileSize,str);
			pInfo->nType=FILEICON;
		}
		::SendMessage(pLocView->m_hWnd,WM_SETFILE,(WPARAM)pInfo,(LPARAM)LOCFILE);
	}
end:
	::PostMessage(pLocView->m_hWnd,WM_SETREDRAWFLAG,0,0);
	fileFind.Close();
	delete pInfo;
	delete pThread;
	return 0L;
}

UINT CMyAppThread::FindServFileThread(LPVOID lParam)
{
	CString str,szFile,szFtpInfo;
	if(!hFtpConn)
	{
		szFtpInfo=m_szFtpName+"服务器未接上";
		pFtpInfoView->SendMessage(WM_RECORDFTPINFO,0,(LPARAM)(LPCTSTR)szFtpInfo);
		return 0L;
	}
	CMyAppThread* pThread=(CMyAppThread*)lParam;
	CFtpParaClass::FILE_FTP_INFO* pInfo=new(CFtpParaClass::FILE_FTP_INFO);
	FILETIME fileTime;
	WIN32_FIND_DATA findData;
	HINTERNET hFind;
	szFtpInfo="正在连接"+m_szFtpName+"服务器";
	pFtpInfoView->SendMessage(WM_RECORDFTPINFO,0,(LPARAM)(LPCTSTR)szFtpInfo);
	szFtpInfo=m_szFtpName+"服务器已连上,用户"+m_szFtpUser+"登录成功";
	pFtpInfoView->SendMessage(WM_RECORDFTPINFO,0,(LPARAM)(LPCTSTR)szFtpInfo);
	DWORD dwLength=MAX_PATH;
	if(m_szFtpDirectory!=" ")
		FtpSetCurrentDirectory(hFtpConn,m_szFtpDirectory);
	FtpGetCurrentDirectory(hFtpConn,(LPTSTR)(LPCTSTR)m_szFtpDirectory,&dwLength);
	::SendMessage(pServView->m_hWnd,WM_SETDIRECTORYDISPLAY,0,(LPARAM)(LPCTSTR)m_szFtpDirectory);
	szFtpInfo="正在查找文件";
	pFtpInfoView->SendMessage(WM_RECORDFTPINFO,0,(LPARAM)(LPCTSTR)szFtpInfo);
	if(!(hFind=FtpFindFirstFile(hFtpConn,_T("*"),&findData,0,0)))
	{
		if (GetLastError()  == ERROR_NO_MORE_FILES) 
		{
			AfxMessageBox("目录为空,没有多余的内容");
			goto end;
		}
		else
		{
			szFtpInfo="文件查找出错,退回,请查明原因";
			pFtpInfoView->SendMessage(WM_RECORDFTPINFO,0,(LPARAM)(LPCTSTR)szFtpInfo);
			goto end;
		}
	}
	do{
		szFile=findData.cFileName;
		strcpy(pInfo->szFileName,szFile);
		fileTime=findData.ftLastWriteTime;
		CTime time=CTime(fileTime);
		CString str=time.Format("%x");
		strcpy(pInfo->szFileDate,str);
		if(findData.dwFileAttributes==FILE_ATTRIBUTE_DIRECTORY)
		{
			strcpy(pInfo->szFileSize,"<DIR>");
			pInfo->nType=DIRECTORYICON;
		}
		else
		{
			DWORD i=findData.nFileSizeLow;
			if(i>1024)
			{
				str.Format("%ld",i/1024);
				str+="KB";
			}
			else
				str.Format("%ld",i);
			strcpy(pInfo->szFileSize,str);
			pInfo->nType=FILEICON;
		}
		::SendMessage(pServView->m_hWnd,WM_SETFILE,(WPARAM)pInfo,(LPARAM)SERVFILE);
		
	}while(InternetFindNextFile(hFind,&findData));
	
	::SendMessage(pServView->m_hWnd,WM_SETREDRAWFLAG,0,(LPARAM)lParam);
	InternetCloseHandle(hFind);
end:
	szFtpInfo="查找文件结束";
	::Sleep(0);
	pFtpInfoView->SendMessage(WM_RECORDFTPINFO,0,(LPARAM)(LPCTSTR)szFtpInfo);
	delete pInfo;
	delete pThread;
	return 0L;	
}

UINT CMyAppThread::SendFileThread(LPVOID lParam)
{
	IsFree=TRUE;
	int i=0;
	CFtpParaClass:: FILE_FTP_INFO* pInf=new (CFtpParaClass::FILE_FTP_INFO);
	CFtpParaClass::SEND_RECEIVE_INFO* pInf1=(CFtpParaClass::SEND_RECEIVE_INFO*)lParam;
	CMyAppThread* pThread=(CMyAppThread*)pInf1->pThread;
	CFtpParaClass::FILE_COUNT_INFO* pInfo=pInf1->pCount;
    CString str,strFtp,szFtpInfo,DirName;
	if(!hFtpConn)
		goto end;
	FtpSetCurrentDirectory(hFtpConn,m_szFtpDirectory);
	::SetCurrentDirectory(m_szLocDirectory);
	for(;i<m_nCount;i++)
	{
		str=m_szLocDirectory;
		strFtp=m_szFtpDirectory;
		DirName=pInfo[i].fileName;
		if(DirName=="..")continue;
		if(m_szLocDirectory.Right(1)=="\\")
			str+=DirName;
		else
			str+="\\"+DirName;
		if(m_szFtpDirectory.Right(1)=='/')
			strFtp+=DirName;
		else
			strFtp+="/"+DirName;
		if(pInfo[i].ufileFlag==FILEICON)
		{
			szFtpInfo="发送";
			szFtpInfo+=DirName;
			pFtpInfoView->SendMessage(WM_RECORDFTPINFO,0,(LPARAM)(LPCTSTR)szFtpInfo);
			if(FtpPutFile(hFtpConn,str,strFtp,INTERNET_FLAG_NO_CACHE_WRITE,0))
			{
				if(IsFree)
				{
					IsFree=FALSE;
					strcpy(pInf->szFileName,DirName);
					strcpy(pInf->szFileDate,"");
					strcpy(pInf->szFileSize,"");
					pInf->nType=FILEICON;
					::PostMessage(pServView->m_hWnd,WM_SETFILE,(WPARAM)pInf,SERVFILE);
				}
				continue;
			}
			else
			{
				szFtpInfo="发送"+DirName+"出错";
				pFtpInfoView->SendMessage(WM_RECORDFTPINFO,0,(LPARAM)(LPCTSTR)szFtpInfo);
				break;
			}
		}
		else
			pLocView->SendMessage(WM_PRESENDFILE,(WPARAM)(LPCTSTR)str,(LPARAM)(LPCTSTR)strFtp);
	}
end:
	nThreadCount--;
	::Sleep(10);
	while(1)
	{
		if(nThreadCount==0)
		{
			szFtpInfo="文件发送结束,退回";
			szFtpInfo+=m_szLocDirectory;
			pFtpInfoView->SendMessage(WM_RECORDFTPINFO,0,(LPARAM)(LPCTSTR)szFtpInfo);
			::SendMessage(pServView->m_hWnd,WM_REDISPLAYFILE,0,(LPARAM)(LPCTSTR)m_szFtpDirectory);
			break;
		}
		::Sleep(100);
	}
	delete[] pInfo;
	delete pInf;
	delete pThread;
	delete pInf1;
	return 0L;
}


UINT CMyAppThread::PreSendFileThread(LPVOID lParam)
{
	CMyAppThread* pThread=(CMyAppThread*)lParam;
	CFtpParaClass:: FILE_FTP_INFO* pInfo=new CFtpParaClass::FILE_FTP_INFO;
	CFileFind fileFind;
	CString szFtpInfo,str,strFtp;
	szFtpInfo="创建"+m_szFtpDirectory+"目录";
	pFtpInfoView->SendMessage(WM_RECORDFTPINFO,0,(LPARAM)(LPCTSTR)szFtpInfo);
	FtpCreateDirectory(hFtpConn,m_szFtpDirectory);
	FtpSetCurrentDirectory(hFtpConn,m_szFtpDirectory);
	::SetCurrentDirectory(m_szLocDirectory);
	szFtpInfo="进入"+m_szFtpDirectory+"目录";
	pServView->SendMessage(WM_SETDIR,0,(LPARAM)(LPCTSTR)m_szFtpDirectory);
	if(IsFree)
		pFtpInfoView->PostMessage(WM_RECORDFTPINFO,0,(LPARAM)(LPCTSTR)szFtpInfo);
	UINT bContinue=fileFind.FindFile("*");
	CString tFile;
	if(bContinue)
	{
		do
		{
			str=m_szLocDirectory;
			strFtp=m_szFtpDirectory;
			bContinue=fileFind.FindNextFile();
			tFile=fileFind.GetFileName();
			if(tFile=="."||tFile==".."||fileFind.IsHidden())continue;
			if(m_szLocDirectory.Right(1)=='\\')
					str+=tFile;
				else
					str+="\\"+tFile;
				if(m_szFtpDirectory.Right(1)=='/')
					strFtp+=tFile;
				else
					strFtp+="/"+tFile;
			if(fileFind.IsDirectory())
				pLocView->SendMessage(WM_PRESENDFILE,(WPARAM)(LPCTSTR)str,(LPARAM)(LPCTSTR)strFtp);
			else
			{
				szFtpInfo="发送"+tFile;
				pFtpInfoView->SendMessage(WM_RECORDFTPINFO,0,(LPARAM)(LPCTSTR)szFtpInfo);
				FtpPutFile(hFtpConn,str,strFtp,INTERNET_FLAG_NO_CACHE_WRITE,0)?TRUE:FALSE;
				if(IsFree)
				{
					IsFree=FALSE;
					strcpy(pInfo->szFileName,tFile);
					strcpy(pInfo->szFileDate,"");
					strcpy(pInfo->szFileSize,"");
					pInfo->nType=FILEICON;
					::PostMessage(pServView->m_hWnd,WM_SETFILE,(WPARAM)pInfo,SERVFILE);
				}
			}
		}while(bContinue);
		fileFind.Close();
	}
		delete pInfo;
		delete pThread;
		nThreadCount--;
		return 0L;
}

UINT CMyAppThread::ReceiveFileThread(LPVOID lParam)
{
	IsFree=TRUE;
	int i=0;
	CFtpParaClass:: FILE_FTP_INFO* pInf=new (CFtpParaClass::FILE_FTP_INFO);
	CFtpParaClass::SEND_RECEIVE_INFO* pInf1=(CFtpParaClass::SEND_RECEIVE_INFO*)lParam;
	CMyAppThread* pThread=(CMyAppThread*)pInf1->pThread;
	CFtpParaClass::FILE_COUNT_INFO* pInfo=pInf1->pCount;
    CString str,strFtp,szFtpInfo,DirName;
	if(!hFtpConn)
		goto end;
	FtpSetCurrentDirectory(hFtpConn,m_szFtpDirectory);
	::SetCurrentDirectory(m_szLocDirectory);
	for(;i<m_nCount;i++)
	{
		str=m_szLocDirectory;
		strFtp=m_szFtpDirectory;
		DirName=pInfo[i].fileName;
		if(DirName=="..")continue;
		if(m_szLocDirectory.Right(1)=="\\")
			str+=DirName;
		else
			str+="\\"+DirName;
		if(m_szFtpDirectory.Right(1)=='/')
			strFtp+=DirName;
		else
			strFtp+="/"+DirName;
		if(pInfo[i].ufileFlag==FILEICON)
		{
			szFtpInfo="接收"+DirName;
			pFtpInfoView->SendMessage(WM_RECORDFTPINFO,0,(LPARAM)(LPCTSTR)szFtpInfo);
			if(FtpGetFile(hFtpConn,strFtp,str,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,SERVFILE);
				}
				continue;
			}
			else
			{
				szFtpInfo="接收"+DirName+"出错";
				pFtpInfoView->SendMessage(WM_RECORDFTPINFO,0,(LPARAM)(LPCTSTR)szFtpInfo);
				break;
			}
		}
		else
			pServView->SendMessage(WM_PRERECEIVEFILE,(WPARAM)(LPCTSTR)str,(LPARAM)(LPCTSTR)strFtp);
	}
	
end:
	nThreadCount--;
	::Sleep(10);
	while(1)
	{
		if(nThreadCount==0)
		{
			szFtpInfo="文件接收结束,退回"+m_szLocDirectory;
			pFtpInfoView->SendMessage(WM_RECORDFTPINFO,0,(LPARAM)(LPCTSTR)szFtpInfo);
			::SendMessage(pLocView->m_hWnd,WM_REDISPLAYFILE,0,(LPARAM)(LPCTSTR)m_szLocDirectory);
			break;
		}
		::Sleep(100);
	}
	delete[] pInfo;
	delete pInf;
	delete pThread;
	delete pInf1;

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -