Ftp.h
#ifndef FTP_H
#define FTP_H
#include "IFtp.h"
#include <InetSDK.h>
#include <WinInet.h>
#pragma comment(lib, "WinInet.lib")
#include <list>
#include <string>
using namespace std;
#include "TFWorker.h"
//====FTP类定义
class Ftp : public IFtp
{
public:
//====连接服务器
//----ip (In): 服务器IP
//----port (In): 服务端口,默认FTP端口21
//----返回值: 操作成功返回TRUE,否则返回FALSE
virtual TBOOL Connect(const TCH* ip, TU16 port=21, const TCH* username=NULL, const TCH* password=NULL);
//====连接服务器
//----ip (In): 服务器IP
//----port (In): 服务端口,默认FTP端口21
//----返回值: 操作成功返回TRUE,否则返回FALSE
virtual TBOOL Connect(const TCH* ip, TU16 port, const TCH* username, const TCH* password, TU32 overTime);
//====断开连接
virtual void DisConnect(void);
virtual TBOOL GetFilesAndDirs(const TCH* destPath, list<string>& paths);
//====查询指定FTP路径下包含的所有文件名称
//----destPath (In): FTP路径
virtual TBOOL GetFiles(const TCH* destPath, list<string>& paths);
//====查询指定文件的长度
//----path (In): 指定文件路径
virtual TU64 GetFileLen(const TCH* path);
//====上传文件
//----srcPath (In): 文件源路径,包含文件全名
//----destPath (In): 文件目的路径,包含文件全名,若目的路径不存在,则自动创建
//----返回值: 操作成功返回TRUE,否则返回FALSE
virtual TBOOL Upload(const TCH* srcPath, const TCH* destPath) const;
//====下载文件
//----srcPath (In): 文件源路径,包含文件全名
//----destPath (In): 文件目的路径,包含文件全名
//----返回值: 操作成功返回TRUE,否则返回FALSE
virtual TBOOL Download(const TCH* srcPath, const TCH* destPath) const;
//====删除文件
//----path (In): 文件路径,包含文件全名
virtual TBOOL Delete(const TCH* path) const;
//====检查文件
//----path (In): 文件路径,包含文件全名
//----返回值: 存在返回TRUE,否则返回FALSE
virtual TBOOL Exist(const TCH* path) const;
private:
class Con
{
public:
Con(TU32 waitTime, Ftp* pFtp):m_WaitTime(waitTime), m_pFtp(pFtp),m_hConnect(NULL){};
HINTERNET operator()(const TCH* ip, TU16 port, const TCH* username, const TCH* password);
private:
//====扫描入口函数
//----param (In): 实时扫描内核指针
static DWORD WINAPI ConProc(LPVOID param);
private:
TU32 m_WaitTime;
Ftp* m_pFtp;
string m_Ip;
TU16 m_Port;
string m_Username;
string m_Password;
HINTERNET m_hConnect;
};
private:
TBOOL GetFilesImp(const TCH* destPath, list<string>& paths);
void CreateDir(const TCH* path) const;
private:
HINTERNET m_hNet;
HINTERNET m_hConnect;
};
#endif
Ftp.cpp
#include "Ftp.h"
HINTERNET Ftp::Con::operator()(const TCH* ip, TU16 port, const TCH* username, const TCH* password)
{
m_Ip = ip;
m_Port = port;
m_Username = username;
m_Password = password;
HANDLE hThread = ::CreateThread(NULL, 0, ConProc, this, 0, NULL);
::WaitForSingleObject(hThread, m_WaitTime);//m_WaitTime毫秒
return m_hConnect;
};
//====扫描入口函数
//----param (In): 实时扫描内核指针
DWORD WINAPI Ftp::Con::ConProc(LPVOID param)
{
Con* pCon = (Con*)param;
pCon->m_hConnect = ::InternetConnect(pCon->m_pFtp->m_hNet,
pCon->m_Ip.c_str(),
pCon->m_Port,
pCon->m_Username.c_str(),
pCon->m_Password.c_str(),
INTERNET_SERVICE_FTP, INTERNET_FLAG_PASSIVE, 0);
return 0;
}
//====连接服务器
//----ip (In): 服务器IP
//----port (In): 服务端口,默认FTP端口21
//----返回值: 操作成功返回TRUE,否则返回FALSE
TBOOL Ftp::Connect(const TCH* ip, TU16 port, const TCH* username, const TCH* password)
{
m_hNet = ::InternetOpen("", INTERNET_OPEN_TYPE_PRECONFIG, NULL, NULL, 0);
if (NULL == m_hNet)
{
return FALSE;
}
m_hConnect = ::InternetConnect(m_hNet, ip, port, username, password, INTERNET_SERVICE_FTP, INTERNET_FLAG_PASSIVE, 0);
if (NULL == m_hConnect)
{
::InternetCloseHandle(m_hNet);
return FALSE;
}
return TRUE;
}
//====连接服务器
//----ip (In): 服务器IP
//----port (In): 服务端口,默认FTP端口21
//----返回值: 操作成功返回TRUE,否则返回FALSE
TBOOL Ftp::Connect(const TCH* ip, TU16 port, const TCH* username, const TCH* password, TU32 overTime)
{
m_hNet = ::InternetOpen("", INTERNET_OPEN_TYPE_PRECONFIG, NULL, NULL, 0);
if (NULL == m_hNet)
{
return FALSE;
}
Con connect(overTime, this);
m_hConnect = connect(ip, port, username, password);
if (NULL == m_hConnect)
{
::InternetCloseHandle(m_hNet);
return FALSE;
}
return TRUE;
}
//====断开连接
void Ftp::DisConnect(void)
{
if (NULL != m_hConnect)
{
::InternetCloseHandle(m_hConnect);
m_hConnect = NULL;
}
if (NULL != m_hNet)
{
::InternetCloseHandle(m_hNet);
m_hNet = NULL;
}
}
//====查询指定FTP路径下包含的所有文件名称
//----destPath (In): FTP路径
TBOOL Ftp::GetFiles(const TCH* destPath, list<string>& paths)
{
return GetFilesImp(destPath, paths);
}
//====查询指定文件的长度
//----path (In): 指定文件路径
TU64 Ftp::GetFileLen(const TCH* path)
{
WIN32_FIND_DATA re;
::memset(&re, 0, sizeof(re));
if (NULL == m_hConnect)
{
return 0;
}
HINTERNET hFile = ::FtpFindFirstFile(m_hConnect, path, &re, 0, NULL);
if (NULL != hFile)
{
TU64 len(0);
len = re.nFileSizeHigh;
len <<= 32;
len |= re.nFileSizeLow;
::InternetCloseHandle(hFile);
return len;
}
return 0;
}
//====上传文件
//----srcPath (In): 文件源路径,包含文件全名
//----destPath (In): 文件目的路径,包含文件全名,若目的路径不存在,则自动创建
//----返回值: 操作成功返回TRUE,否则返回FALSE
TBOOL Ftp::Upload(const TCH* srcPath, const TCH* destPath) const
{
string ftpPath(destPath);
TI32 pos = ftpPath.rfind('\\');
if (ftpPath.npos != pos)
{
ftpPath.erase(pos+1);
::FtpCreateDirectory(m_hConnect, ftpPath.c_str());
}
if (!::FtpPutFile(m_hConnect, srcPath, destPath, FTP_TRANSFER_TYPE_BINARY, 0))
{
//int ret = ::GetLastError();
return FALSE;
}
return TRUE;
}
//====下载文件
//----srcPath (In): 文件源路径,包含文件全名
//----destPath (In): 文件目的路径,包含文件全名
//----返回值: 操作成功返回TRUE,否则返回FALSE
TBOOL Ftp::Download(const TCH* srcPath, const TCH* destPath) const
{
string localPath(destPath);
TI32 pos = localPath.rfind('\\');
if (localPath.npos != pos)
{
localPath.erase(pos+1);
CreateDir(localPath.c_str());
}
//INTERNET_FLAG_RELOAD // INTERNET_FLAG_RESYNCHRONIZE
if (!::FtpGetFile(m_hConnect, srcPath, destPath, FALSE, 0, INTERNET_FLAG_RESYNCHRONIZE, 0))
{
return FALSE;
}
return TRUE;
}
//====删除文件
//----path (In): 文件路径,包含文件全名
TBOOL Ftp::Delete(const TCH* path) const
{
return ::FtpDeleteFile(m_hConnect, path);
}
//====检查文件
//----path (In): 文件路径,包含文件全名
//----返回值: 存在返回TRUE,否则返回FALSE
TBOOL Ftp::Exist(const TCH* path) const
{
HINTERNET hFile = ::FtpFindFirstFile(m_hConnect, path, NULL, 0, NULL);
if (NULL != hFile)
{
::InternetCloseHandle(hFile);
return TRUE;
}
return FALSE;
}
TBOOL Ftp::GetFilesImp(const TCH* destPath, list<string>& paths)
{
list<string> ds;
string path(destPath);
path += "\\";
WIN32_FIND_DATA re;
::memset(&re, 0, sizeof(re));
if (NULL == m_hConnect)
{
return FALSE;
}
HINTERNET hFile = ::FtpFindFirstFile(m_hConnect, (path+"*.*").c_str(), &re, 0, NULL);
if (NULL == hFile)
{
return TRUE;
}
else
{
if (FILE_ATTRIBUTE_DIRECTORY & re.dwFileAttributes)
{
string p = (path=="\\" ? "" : path) + re.cFileName;
ds.push_back(p);
}
else
{
paths.push_back(path=="\\" ? re.cFileName : path+re.cFileName);
}
}
while (::InternetFindNextFile(hFile, &re))
{
if (FILE_ATTRIBUTE_DIRECTORY & re.dwFileAttributes)
{
string p = (path=="\\" ? "" : path) + re.cFileName;
ds.push_back(p);
}
else
{
paths.push_back(path=="\\" ? re.cFileName : path+re.cFileName);
}
}
if (NULL != hFile)
{
::InternetCloseHandle(hFile);
}
for (list<string>::iterator i=ds.begin(); i!=ds.end(); ++i)
{
GetFilesImp(i->c_str(), paths);
}
return TRUE;
}
TBOOL Ftp::GetFilesAndDirs(const TCH* destPath, list<string>& paths)
{
list<string> ds;
string path(destPath);
path += "\\";
WIN32_FIND_DATA re;
::memset(&re, 0, sizeof(re));
if (NULL == m_hConnect)
{
return FALSE;
}
HINTERNET hFile = ::FtpFindFirstFile(m_hConnect, (path+"*.*").c_str(), &re, 0, NULL);
if (NULL == hFile)
{
return TRUE;
}
else
{
if (FILE_ATTRIBUTE_DIRECTORY & re.dwFileAttributes)
{
string p = (path=="\\" ? "" : path) + re.cFileName;
paths.push_back(p+"*");
}
else
{
paths.push_back(path=="\\" ? re.cFileName : path+re.cFileName);
}
}
while (::InternetFindNextFile(hFile, &re))
{
if (FILE_ATTRIBUTE_DIRECTORY & re.dwFileAttributes)
{
string p = (path=="\\" ? "" : path) + re.cFileName;
paths.push_back(p+"*");
}
else
{
paths.push_back(path=="\\" ? re.cFileName : path+re.cFileName);
}
}
if (NULL != hFile)
{
::InternetCloseHandle(hFile);
}
return TRUE;
}
void Ftp::CreateDir(const TCH* path) const
{
string p(path);
TI32 pos = p.find('\\', 0);
while (p.npos != pos)
{
string d = p.substr(0, pos+1);
::CreateDirectory(d.c_str(), NULL);
pos = p.find('\\', pos+1);
}
}