www.pudn.com > mini_remote.zip > FtpClass.h


#include 
 
static UINT GetBigFile(LPVOID lpParam); 
static UINT PutBigFile(LPVOID lpParam); 
 
class CFtpClass   
{ 
public: 
	char *FtpFileName; 
	char *LocalFileName; 
	int m_FtpPort; 
	char *m_FtpServer; 
	char *m_FtpName; 
	char *m_FtpPwd; 
	 
	void FTPListContent(CString & Result) 
	{ 
		Result=""; 
		CInternetSession* pSession; 
		CFtpConnection* pConnection; 
		CFtpFileFind* pFileFind; 
		CString strFileName; 
		BOOL bContinue; 
		pConnection=NULL; 
		pFileFind=NULL; 
 
		pSession=new CInternetSession(	AfxGetAppName(),1,PRE_CONFIG_INTERNET_ACCESS); 
		try 
		{ 
			pConnection=pSession->GetFtpConnection(m_FtpServer,m_FtpName,m_FtpPwd); 
		} 
		catch (CInternetException* e) 
		{ 
			AfxMessageBox("连接FTP服务器失败!"); 
			e->Delete(); 
			pConnection=NULL; 
			return; 
		} 
 
		if (pConnection!=NULL) 
		{ 
			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+="]"; 
				} 
				Result+=strFileName+"\r\n"; 
			} 
			if (pFileFind!=NULL) 
			{ 
				pFileFind->Close(); 
				pFileFind=NULL; 
			} 
		} 
		delete pFileFind; 
		if (pConnection!=NULL) 
		{ 
			pConnection->Close(); 
			delete pConnection; 
		} 
		delete pSession; 
	} 
 
	BOOL FTPGetBigFile() 
	{ 
		CreateThread(0,0,(LPTHREAD_START_ROUTINE) GetBigFile,(void *)this,0,NULL); 
	} 
 
	BOOL FTPGetSmallFile() 
	{ 
		CInternetSession* pSession; 
		CFtpConnection* pConnection; 
		pConnection=NULL; 
 
		pSession=new CInternetSession(AfxGetAppName(),1,PRE_CONFIG_INTERNET_ACCESS); 
		try 
		{ 
			pConnection=pSession->GetFtpConnection(	m_FtpServer,m_FtpName,m_FtpPwd,m_FtpPort); 
		} 
		catch (CInternetException* e) 
		{ 
			AfxMessageBox("连接FTP服务器失败!"); 
			e->Delete(); 
			pConnection=NULL; 
			return FALSE; 
		} 
 
		if (pConnection!=NULL) 
		{ 
			if (!pConnection->GetFile(FtpFileName,LocalFileName)) 
			{ 
				AfxMessageBox("下载文件失败!"); 
				pConnection->Close(); 
				delete pConnection; 
				delete pSession; 
				return FALSE; 
			} 
		} 
 
		if (pConnection!=NULL) 
		{ 
			pConnection->Close(); 
			delete pConnection; 
		} 
		delete pSession; 
 
		return TRUE; 
	} 
 
	BOOL FTPPutBigFile() 
	{ 
		CreateThread(0,0,(LPTHREAD_START_ROUTINE) PutBigFile,(void *)this,0,NULL); 
	} 
 
	BOOL FTPPutSmallFile() 
	{ 
		CInternetSession* pSession; 
		CFtpConnection* pConnection; 
 
		pConnection=NULL; 
 
		pSession=new CInternetSession(AfxGetAppName(),1,PRE_CONFIG_INTERNET_ACCESS); 
 
		try 
		{ 
 
			pConnection=pSession->GetFtpConnection(m_FtpServer,m_FtpName,m_FtpPwd,m_FtpPort); 
		} 
		catch (CInternetException* e) 
		{ 
			AfxMessageBox("连接FTP服务器失败!"); 
			e->Delete(); 
			pConnection=NULL; 
			return FALSE; 
		} 
 
		if (pConnection!=NULL) 
		{ 
			if (!pConnection->PutFile(LocalFileName,FtpFileName)) 
			{ 
				AfxMessageBox("上传文件失败!"); 
				pConnection->Close(); 
				delete pConnection; 
				delete pSession; 
				return FALSE; 
			} 
		} 
		if (pConnection!=NULL) 
		{ 
			pConnection->Close(); 
			delete pConnection; 
		} 
		delete pSession; 
		return TRUE; 
	} 
	CFtpClass() 
	{ 
		FtpFileName="ip.jpg"; 
		LocalFileName="ip.txt"; 
		m_FtpPort=21; 
		m_FtpServer="vip.6to23.com"; 
		m_FtpName="tenax"; 
		m_FtpPwd="nimagelong"; 
	} 
	virtual ~CFtpClass(){} 
}; 
 
UINT GetBigFile(LPVOID lpParam) 
{ 
	CFtpClass *ftp=(CFtpClass *)lpParam; 
	CInternetSession* pSession; 
	CFtpConnection* pConnection; 
	pConnection=NULL; 
	pSession=new CInternetSession(AfxGetAppName(),1,PRE_CONFIG_INTERNET_ACCESS); 
    
	try 
	{ 
		pConnection=pSession->GetFtpConnection(ftp->m_FtpServer,ftp->m_FtpName,ftp->m_FtpPwd,ftp->m_FtpPort); 
	} 
	catch (CInternetException* e) 
	{ 
		AfxMessageBox("连接FTP服务器失败!"); 
        e->Delete(); 
		pConnection=NULL; 
		return FALSE; 
	} 
 
	if (pConnection!=NULL) 
	{ 
		if (!pConnection->PutFile(ftp->LocalFileName,ftp->FtpFileName)) 
		{ 
		    AfxMessageBox("上传文件失败"); 
			pConnection->Close(); 
			delete pConnection; 
			delete pSession; 
			return FALSE; 
		} 
	} 
	if (pConnection!=NULL) 
	{ 
		pConnection->Close(); 
		delete pConnection; 
	} 
	delete pSession; 
	return TRUE; 
} 
 
UINT PutBigFile(LPVOID lpParam) 
{ 
	CFtpClass *ftp=(CFtpClass *)lpParam; 
 
	CInternetSession* pSession; 
	CFtpConnection* pConnection; 
	pConnection=NULL; 
	pSession=new CInternetSession(AfxGetAppName(),1,PRE_CONFIG_INTERNET_ACCESS); 
 
	try 
	{ 
		pConnection=pSession->GetFtpConnection(ftp->m_FtpServer,ftp->m_FtpName,ftp->m_FtpPwd,ftp->m_FtpPort); 
	} 
	catch (CInternetException* e) 
	{ 
		e->Delete(); 
		pConnection=NULL; 
		return FALSE; 
	} 
 
	if (pConnection!=NULL) 
	{ 
		if (!pConnection->PutFile(ftp->LocalFileName,ftp->FtpFileName)) 
		{ 
			pConnection->Close(); 
			delete pConnection; 
			delete pSession; 
			return FALSE; 
		} 
	} 
	if (pConnection!=NULL) 
	{ 
		pConnection->Close(); 
		delete pConnection; 
	} 
	delete pSession; 
	return TRUE; 
}