今天,我们就看看,我们在实际应用中如何封装我们的FTP吧.我在这里就只贴出代码了.
我们的FTP类是CTaskFtp:
头文件:
// CTaskFtp.h: interface for the CCTaskFtp class.
//
//
#if !defined(AFX_CTaskFtp_H__DFFC12FF_CE6C_479B_90F5_88814610EE17__INCLUDED_)
#define AFX_CTaskFtp_H__DFFC12FF_CE6C_479B_90F5_88814610EE17__INCLUDED_
#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000
#include <afxinet.h>
class CTaskFtp : public CObject
{
public:
CTaskFtp();
virtual ~CTaskFtp();
BOOL OpenFtpConnection(LPCTSTR strServer ,LPCTSTR strUserName = NULL,LPCTSTR strPassword = NULL);
void CloseFtpConnection();
BOOL Download(CString strLocalFile,CString strRemoteFile );
BOOL Upload(CString strLocalFile,CString strRemoteFile );
BOOL FindFile(CString strRemoteFile);
BOOL FindNextFile();
CString GetFileName();
CString GetFilePath();
CString GetFileTitle();
CString GetFileURL();
BOOL IsArchived();
BOOL IsCompressed();
BOOL IsDirectory();
BOOL IsDots();
BOOL IsHidden();
BOOL IsNormal();
BOOL IsReadOnly();
BOOL IsSystem();
BOOL IsTemporary();
BOOL RemoveFile(CString strRemoteFile);
BOOL RemoveAllFiles(CString strRemoteDirectory);
private:
CInternetSession * m_pSession; //Internet Session
CFtpConnection * m_pConnection; //Ftp Connection
CFtpFileFind * m_pFileFind; //filefinder
CString m_strServerName; //服务器地址
CString m_strUserName; //用户名
CString m_strPassword; //用户密码
BOOL m_bNeedRight; //判断是否需要用户权限(默认不要)
};
#endif // !defined(AFX_CTaskFtp_H__DFFC12FF_CE6C_479B_90F5_88814610EE17__INCLUDED_)
实现文件:
#include "TaskFtp.h"
#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif
//
// Construction/Destruction
//
CTaskFtp::CTaskFtp()
{
m_pSession = NULL;
m_pConnection = NULL;
m_pFileFind = NULL;
m_strServerName = "";
m_strUserName = "";
m_strPassword = "";
m_bNeedRight = FALSE;
m_pSession = new CInternetSession();
}
CTaskFtp::~CTaskFtp()
{
if ( m_pSession )
{
CloseFtpConnection();
m_pSession->Close();
delete m_pSession;
}
m_pSession = NULL;
}
BOOL CTaskFtp::OpenFtpConnection(LPCTSTR strServer ,LPCTSTR strUserName /* = NULL */,LPCTSTR strPassword /* = NULL */)
{
if( m_pSession == NULL )
{
LOG_REPORT(g_LogFile,"/t%s , %s , %s/t/n",
"错误","没有建立会话",(CTime::GetCurrentTime()).Format("%Y-%m-%d %H:%M:%S"));
return FALSE;
}
//关闭FTP连接
CloseFtpConnection();
if( strUserName == NULL )
m_bNeedRight = FALSE;
else
m_bNeedRight = TRUE;
m_strServerName = strServer;
m_strUserName = strUserName;
m_strPassword = strPassword;
try
{
if( m_bNeedRight )
m_pConnection = m_pSession->GetFtpConnection(m_strServerName,m_strUserName,m_strPassword);
else
m_pConnection = m_pSession->GetFtpConnection(m_strServerName);
}
catch (CInternetException *pEx)
{
TCHAR szErr[1024];
pEx->GetErrorMessage(szErr, 1024);
LOG_REPORT(g_LogFile,"/t%s , %s , %s/t/n",
"错误",szErr,(CTime::GetCurrentTime()).Format("%Y-%m-%d %H:%M:%S"));
pEx->Delete();
return FALSE;
}
return TRUE;
}
void CTaskFtp::CloseFtpConnection()
{
if ( m_pConnection )
{
if ( m_pFileFind )
{
m_pFileFind->Close();
delete m_pFileFind;
}
m_pConnection->Close();
delete m_pConnection;
}
m_pConnection = NULL;
m_pFileFind = NULL;
m_strServerName = "";
m_strUserName = "";
m_strPassword = "";
}
BOOL CTaskFtp::Download(CString strLocalFile,CString strRemoteFile )
{
if( !m_pConnection )
{
LOG_REPORT(g_LogFile,"/t%s , %s , %s/t/n",
"错误","没有建立FTP连接",(CTime::GetCurrentTime()).Format("%Y-%m-%d %H:%M:%S"));
return FALSE;
}
try
{
m_pConnection->GetFile(strRemoteFile,strLocalFile);
}
catch (CInternetException* pEx)
{
TCHAR szErr[1024];
pEx->GetErrorMessage(szErr, 1024);
LOG_REPORT(g_LogFile,"/t%s , %s , %s/t/n",
"错误",szErr,(CTime::GetCurrentTime()).Format("%Y-%m-%d %H:%M:%S"));
pEx->Delete();
return FALSE;
}
return TRUE;
}
BOOL CTaskFtp::Upload( CString strLocalFile,CString strRemoteFile )
{
if( !m_pConnection )
{
LOG_REPORT(g_LogFile,"/t%s , %s , %s/t/n",
"错误","没有建立FTP连接",(CTime::GetCurrentTime()).Format("%Y-%m-%d %H:%M:%S"));
return FALSE;
}
try
{
return m_pConnection->PutFile(strLocalFile,strRemoteFile);
}
catch (CInternetException* pEx)
{
TCHAR szErr[1024];
pEx->GetErrorMessage(szErr, 1024);
LOG_REPORT(g_LogFile,"/t%s , %s , %s/t/n",
"错误",szErr,(CTime::GetCurrentTime()).Format("%Y-%m-%d %H:%M:%S"));
pEx->Delete();
return FALSE;
}
return TRUE;
}
BOOL CTaskFtp::FindFile(CString strRemoteFile)
{
if( !m_pConnection )
{
LOG_REPORT(g_LogFile,"/t%s , %s , %s/t/n",
"错误","没有建立FTP连接",(CTime::GetCurrentTime()).Format("%Y-%m-%d %H:%M:%S"));
return FALSE;
}
try
{
if ( m_pFileFind )
{
m_pFileFind->Close();
delete m_pFileFind;
m_pFileFind = NULL;
}
m_pFileFind = new CFtpFileFind(m_pConnection);
return m_pFileFind->FindFile(strRemoteFile);
}
catch (CInternetException* pEx)
{
TCHAR szErr[1024];
pEx->GetErrorMessage(szErr, 1024);
LOG_REPORT(g_LogFile,"/t%s , %s , %s/t/n",
"错误",szErr,(CTime::GetCurrentTime()).Format("%Y-%m-%d %H:%M:%S"));
pEx->Delete();
return FALSE;
}
}
BOOL CTaskFtp::FindNextFile()
{
VERIFY( m_pFileFind );
try
{
return m_pFileFind->FindNextFile();
}
catch (CInternetException* pEx)
{
TCHAR szErr[1024];
pEx->GetErrorMessage(szErr, 1024);
LOG_REPORT(g_LogFile,"/t%s , %s , %s/t/n",
"错误",szErr,(CTime::GetCurrentTime()).Format("%Y-%m-%d %H:%M:%S"));
pEx->Delete();
return FALSE;
}
}
CString CTaskFtp::GetFileName()
{
VERIFY( m_pFileFind );
try
{
return m_pFileFind->GetFileName();
}
catch (CInternetException* pEx)
{
TCHAR szErr[1024];
pEx->GetErrorMessage(szErr, 1024);
LOG_REPORT(g_LogFile,"/t%s , %s , %s/t/n",
"错误",szErr,(CTime::GetCurrentTime()).Format("%Y-%m-%d %H:%M:%S"));
pEx->Delete();
return "";
}
}
CString CTaskFtp::GetFilePath()
{
VERIFY( m_pFileFind );
try
{
return m_pFileFind->GetFilePath();
}
catch (CInternetException* pEx)
{
TCHAR szErr[1024];
pEx->GetErrorMessage(szErr, 1024);
LOG_REPORT(g_LogFile,"/t%s , %s , %s/t/n",
"错误",szErr,(CTime::GetCurrentTime()).Format("%Y-%m-%d %H:%M:%S"));
pEx->Delete();
return "";
}
}
CString CTaskFtp::GetFileTitle()
{
VERIFY( m_pFileFind );
try
{
return m_pFileFind->GetFileTitle();
}
catch (CInternetException* pEx)
{
TCHAR szErr[1024];
pEx->GetErrorMessage(szErr, 1024);
LOG_REPORT(g_LogFile,"/t%s , %s , %s/t/n",
"错误",szErr,(CTime::GetCurrentTime()).Format("%Y-%m-%d %H:%M:%S"));
pEx->Delete();
return "";
}
}
CString CTaskFtp::GetFileURL()
{
VERIFY( m_pFileFind );
try
{
return m_pFileFind->GetFileURL();
}
catch (CInternetException* pEx)
{
TCHAR szErr[1024];
pEx->GetErrorMessage(szErr, 1024);
LOG_REPORT(g_LogFile,"/t%s , %s , %s/t/n",
"错误",szErr,(CTime::GetCurrentTime()).Format("%Y-%m-%d %H:%M:%S"));
pEx->Delete();
return "";
}
}
BOOL CTaskFtp::IsArchived()
{
VERIFY( m_pFileFind );
try
{
return m_pFileFind->IsArchived();
}
catch (CInternetException* pEx)
{
TCHAR szErr[1024];
pEx->GetErrorMessage(szErr, 1024);
LOG_REPORT(g_LogFile,"/t%s , %s , %s/t/n",
"错误",szErr,(CTime::GetCurrentTime()).Format("%Y-%m-%d %H:%M:%S"));
pEx->Delete();
return FALSE;
}
}
BOOL CTaskFtp::IsCompressed()
{
VERIFY( m_pFileFind );
try
{
return m_pFileFind->IsCompressed();
}
catch (CInternetException* pEx)
{
TCHAR szErr[1024];
pEx->GetErrorMessage(szErr, 1024);
LOG_REPORT(g_LogFile,"/t%s , %s , %s/t/n",
"错误",szErr,(CTime::GetCurrentTime()).Format("%Y-%m-%d %H:%M:%S"));
pEx->Delete();
return FALSE;
}
}
BOOL CTaskFtp::IsDirectory()
{
VERIFY( m_pFileFind );
try
{
return m_pFileFind->IsDirectory();
}
catch (CInternetException* pEx)
{
TCHAR szErr[1024];
pEx->GetErrorMessage(szErr, 1024);
LOG_REPORT(g_LogFile,"/t%s , %s , %s/t/n",
"错误",szErr,(CTime::GetCurrentTime()).Format("%Y-%m-%d %H:%M:%S"));
pEx->Delete();
return FALSE;
}
}
BOOL CTaskFtp::IsDots()
{
VERIFY( m_pFileFind );
try
{
return m_pFileFind->IsDots();
}
catch (CInternetException* pEx)
{
TCHAR szErr[1024];
pEx->GetErrorMessage(szErr, 1024);
LOG_REPORT(g_LogFile,"/t%s , %s , %s/t/n",
"错误",szErr,(CTime::GetCurrentTime()).Format("%Y-%m-%d %H:%M:%S"));
pEx->Delete();
return FALSE;
}
}
BOOL CTaskFtp::IsHidden()
{
VERIFY( m_pFileFind );
try
{
return m_pFileFind->IsHidden();
}
catch (CInternetException* pEx)
{
TCHAR szErr[1024];
pEx->GetErrorMessage(szErr, 1024);
LOG_REPORT(g_LogFile,"/t%s , %s , %s/t/n",
"错误",szErr,(CTime::GetCurrentTime()).Format("%Y-%m-%d %H:%M:%S"));
pEx->Delete();
return FALSE;
}
}
BOOL CTaskFtp::IsNormal()
{
VERIFY( m_pFileFind );
try
{
return m_pFileFind->IsNormal();
}
catch (CInternetException* pEx)
{
TCHAR szErr[1024];
pEx->GetErrorMessage(szErr, 1024);
LOG_REPORT(g_LogFile,"/t%s , %s , %s/t/n",
"错误",szErr,(CTime::GetCurrentTime()).Format("%Y-%m-%d %H:%M:%S"));
pEx->Delete();
return FALSE;
}
}
BOOL CTaskFtp::IsReadOnly()
{
VERIFY( m_pFileFind );
try
{
return m_pFileFind->IsReadOnly();
}
catch (CInternetException* pEx)
{
TCHAR szErr[1024];
pEx->GetErrorMessage(szErr, 1024);
LOG_REPORT(g_LogFile,"/t%s , %s , %s/t/n",
"错误",szErr,(CTime::GetCurrentTime()).Format("%Y-%m-%d %H:%M:%S"));
pEx->Delete();
return FALSE;
}
}
BOOL CTaskFtp::IsSystem()
{
VERIFY( m_pFileFind );
try
{
return m_pFileFind->IsSystem();
}
catch (CInternetException* pEx)
{
TCHAR szErr[1024];
pEx->GetErrorMessage(szErr, 1024);
LOG_REPORT(g_LogFile,"/t%s , %s , %s/t/n",
"错误",szErr,(CTime::GetCurrentTime()).Format("%Y-%m-%d %H:%M:%S"));
pEx->Delete();
return FALSE;
}
}
BOOL CTaskFtp::IsTemporary()
{
VERIFY( m_pFileFind );
try
{
return m_pFileFind->IsTemporary();
}
catch (CInternetException* pEx)
{
TCHAR szErr[1024];
pEx->GetErrorMessage(szErr, 1024);
LOG_REPORT(g_LogFile,"/t%s , %s , %s/t/n",
"错误",szErr,(CTime::GetCurrentTime()).Format("%Y-%m-%d %H:%M:%S"));
pEx->Delete();
return FALSE;
}
}
BOOL CTaskFtp::RemoveFile(CString strRemoteFile)
{
if( !m_pConnection )
{
LOG_REPORT(g_LogFile,"/t%s , %s , %s/t/n",
"错误","没有建立FTP连接",(CTime::GetCurrentTime()).Format("%Y-%m-%d %H:%M:%S"));
return FALSE;
}
try
{
return m_pConnection->Remove(strRemoteFile);
}
catch (CInternetException* pEx)
{
TCHAR szErr[1024];
pEx->GetErrorMessage(szErr, 1024);
LOG_REPORT(g_LogFile,"/t%s , %s , %s/t/n",
"错误",szErr,(CTime::GetCurrentTime()).Format("%Y-%m-%d %H:%M:%S"));
pEx->Delete();
return FALSE;
}
}
BOOL CTaskFtp::RemoveAllFiles(CString strRemoteDirectory)
{
if( !m_pConnection )
{
LOG_REPORT(g_LogFile,"/t%s , %s , %s/t/n",
"错误","没有建立FTP连接",(CTime::GetCurrentTime()).Format("%Y-%m-%d %H:%M:%S"));
return FALSE;
}
try
{
BOOL bWork = FindFile(strRemoteDirectory + "//*.*");
while(bWork)
{
bWork = FindNextFile();
if( IsDirectory() || IsDots() || IsHidden() || IsSystem() )
continue;
else
{
RemoveFile(GetFilePath());
}
}
}
catch (CInternetException* pEx)
{
TCHAR szErr[1024];
pEx->GetErrorMessage(szErr, 1024);
LOG_REPORT(g_LogFile,"/t%s , %s , %s/t/n",
"错误",szErr,(CTime::GetCurrentTime()).Format("%Y-%m-%d %H:%M:%S"));
pEx->Delete();
return FALSE;
}
return TRUE;
}
当然了,在实际应用中还是有必要做部分修改的.比如加上异常处理等等.
不过,你也可以直接用的.呵呵