sunyke 发表于 2015-5-28 10:43:26

VC++ 实现FTP下载的通用类

// DownloadFtp.cpp: implementation of the CDownloadFtp class.
//
//////////////////////////////////////////////////////////////////////


#include "stdafx.h"
#include "downtest.h"
#include "DownloadFtp.h"


#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif


void DownloadNotify ( int nIndex, UINT nNotityType, LPVOID lpNotifyData, LPVOID pDownloadMTR );


//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////


CDownloadFtp::CDownloadFtp()
{
}


CDownloadFtp::~CDownloadFtp()
{
}


//
// 创建一个 FTP 的数据通道连接
//
BOOL CDownloadFtp::CreateFTPDataConnect(CSocketClient &SocketClient)
{
ASSERT ( SocketClient.m_hSocket == INVALID_SOCKET );
// 设被动模式
if ( !m_SocketClient.SendString ( "PASV\r\n" ) ) return FALSE;
CString csResponseStr;
if ( !m_SocketClient.GetResponse ( 227, &csResponseStr ) )
return FALSE;
CString csPasvIP;
USHORT uPasvPort = 0;
if ( !m_SocketClient.GetIPAndPortByPasvString ( csResponseStr, csPasvIP, uPasvPort ) )
return FALSE;


// 创建数据通道连接
if ( !SocketClient.Connect ( csPasvIP, uPasvPort ) )
return FALSE;


return TRUE;
}


//
// 连接需要下载的服务器
//
BOOL CDownloadFtp::Connect ()
{
if ( !CDownloadPub::Connect () )
return FALSE;


SLEEP_RETURN_Down ( 0 );
int nResponseCode = m_SocketClient.GetResponse ();
if ( nResponseCode != 220 )
{
Log ( L_WARNING, "(%d) Connect to [%s:%u] failed", m_nIndex, m_csServer, m_nPort );
}


// 登录
SLEEP_RETURN_Down ( 0 );
if ( m_csUsername.IsEmpty () )
m_csUsername = "anonymous";
if ( !m_SocketClient.SendString ( "USER %s\r\n", m_csUsername ) ) return FALSE;
nResponseCode = m_SocketClient.GetResponse ();


// 需要密码
SLEEP_RETURN_Down ( 0 );
if ( nResponseCode == 331 )
{
if ( !m_SocketClient.SendString ( "PASS %s\r\n", m_csPassword ) ) return FALSE;
nResponseCode = m_SocketClient.GetResponse ();
}


// 登录失败
if ( nResponseCode != 230 )
{
Log ( L_ERROR, "(%d) Login failed", m_nIndex );
return FALSE;
}
Log ( L_NORMAL, "(%d) Welcome message:--------------------------\r\n%s\r\n", m_nIndex,
m_SocketClient.GetResponseHistoryString () );
m_SocketClient.ClearResponseHistoryString ();


return TRUE;
}


BOOL CDownloadFtp::DownloadOnce()
{
// 不需要下载了
int nWillDownloadSize = Get_WillDownloadSize();    // 本次应该下载的字节数
int nDownloadedSize = Get_DownloadedSize ();    // 已下载字节数
if ( nWillDownloadSize > 0 && nDownloadedSize >= nWillDownloadSize )
return DownloadEnd(TRUE);


if ( !CDownloadPub::DownloadOnce () )
return DownloadEnd(FALSE);

// 设置接收数据类型为 二进制模式
if ( !m_SocketClient.SendString ( "TYPE I\r\n" ) )
return DownloadEnd(FALSE);
if ( !m_SocketClient.GetResponse ( 200 ) )
return DownloadEnd(FALSE);
SLEEP_RETURN_Down ( 0 );


// 创建数据通道连接
CSocketClient SocketClient;
SocketClient.SetEventOfEndModule ( m_hEvtEndModule );
SocketClient.m_nIndex = m_nIndex;
if ( !CreateFTPDataConnect ( SocketClient ) )
return DownloadEnd(FALSE);
SLEEP_RETURN_Down ( 0 );

// 设定下载文件的起始位置
int nWillDownloadStartPos = Get_WillDownloadStartPos (); // 开始位置
if ( !m_SocketClient.SendString ( "REST %d\r\n", nWillDownloadStartPos+nDownloadedSize ) )
return DownloadEnd(FALSE);
if ( !m_SocketClient.GetResponse ( 350 ) )
return DownloadEnd(FALSE);


// 提交下载文件的请求
if ( !m_SocketClient.SendString ( "RETR %s\r\n", m_csObject ) )
return DownloadEnd(FALSE);
if ( !m_SocketClient.GetResponse ( 150 ) )
return DownloadEnd(FALSE);
SLEEP_RETURN_Down ( 0 );


// 从数据通道读取数据,并保存到文件中
BOOL bRet = RecvDataAndSaveToFile(SocketClient);
SocketClient.Disconnect ();
return DownloadEnd ( bRet );


}


//
// 获取远程站点信息,如:是否支持断点续传、要下载的文件大小和创建时间等
//
BOOL CDownloadFtp::GetRemoteSiteInfo_Pro()
{
BOOL bRet = FALSE;
CSocketClient SocketClient;
CString csReadData;
char szRecvBuf = {0};
int nReadSize = 0;
CString csOneLine;
int i;


SocketClient.m_nIndex = m_nIndex;
if ( !HANDLE_IS_VALID(m_hEvtEndModule) )
goto Finished;
SocketClient.SetEventOfEndModule ( m_hEvtEndModule );


if ( !CDownloadPub::GetRemoteSiteInfo_Pro() )
goto Finished;


if ( !m_SocketClient.Is_Connected () && !Connect () )
goto Finished;
SLEEP_RETURN ( 0 );


// 设置接收数据类型为 ASCII
if ( !m_SocketClient.SendString ( "TYPE A\r\n" ) ) goto Finished;
if ( !m_SocketClient.GetResponse ( 200 ) )
goto Finished;
SLEEP_RETURN ( 0 );

// 判断是否支持断点续传
if ( !m_SocketClient.SendString ( "REST 1\r\n" ) ) goto Finished;
m_bSupportResume = ( m_SocketClient.GetResponse () == 350 );
TRACE ( "是否支持断点续传:%s\n", m_bSupportResume ? "支持":"不支持" );
if ( m_bSupportResume )
{
if ( !m_SocketClient.SendString ( "REST 0\r\n" ) ) goto Finished;
VERIFY ( m_SocketClient.GetResponse ( 350 ) );
}
SLEEP_RETURN ( 0 );


// 创建数据通道连接
if ( !CreateFTPDataConnect ( SocketClient ) )
goto Finished;
// 发送列举文件命令
if ( !m_SocketClient.SendString ( "LIST %s\r\n", m_csObject ) )
goto Finished;
if ( !m_SocketClient.GetResponse ( 150 ) )
goto Finished;
SLEEP_RETURN ( 0 );

// 从数据通道读取文件列表信息,直到命令通道返回 "226" 回应码,注意:这里接收数据用非阻塞型的。
for ( i=0; ;i++ )
{
memset ( szRecvBuf, 0, sizeof(szRecvBuf) );
nReadSize = SocketClient.Receive ( szRecvBuf, sizeof(szRecvBuf), FALSE );
if ( nReadSize < 0 )
{
   Log ( L_WARNING, "(%d) Receive file list info failed", m_nIndex );
   break;
}
csReadData += szRecvBuf;
int nResponseCode = m_SocketClient.GetResponse ( (CString*)NULL, FALSE );
if ( nResponseCode == -1 ) goto Finished;
else if ( nResponseCode == 0 )
{
   SLEEP_RETURN ( 100 );
}
else if ( nResponseCode == 226 )
{
   break;
}
else goto Finished;
SLEEP_RETURN ( 0 );
}


for ( i=0; !csReadData.IsEmpty() ; i++ )
{
csOneLine = GetOneLine ( csReadData );
if ( !csOneLine.IsEmpty() )
{
   ParseFileInfoStr ( csOneLine );
}
}
bRet = TRUE;


Finished:
return bRet;
}


//
// 从 "-rw-rw-rw-   1 user   group    37979686 Mar9 13:39 FTP-Callgle 1.4.0_20060309.cab" 字符串中分析
// 出文件信息
//
void CDownloadFtp::ParseFileInfoStr(CString &csFileInfoStr)
{
csFileInfoStr.TrimLeft (); csFileInfoStr.TrimRight ();
if ( csFileInfoStr.IsEmpty() ) return;
BOOL bLastCharIsSpace = ( csFileInfoStr==' ' );
int nSpaceCount = 0;
CString csFileTime1, csFileTime2, csFileTime3;
CString csNodeStr;
CString csFileName;
for ( int i=0; i 0 && nWillDownloadSize-nDownloadedSize > m_nFileTotalSize )
       Set_WillDownloadSize ( m_nFileTotalSize-nDownloadedSize );
   }
    }
    // 文件时间第一节
    else if ( nSpaceCount == 5 )
    {
   csFileTime1 = csNodeStr;
    }
    // 文件时间第二节
    else if ( nSpaceCount == 6 )
    {
   csFileTime2 = csNodeStr;
    }
    // 文件时间第三节
    else if ( nSpaceCount == 7 )
    {
   csFileTime3 = csNodeStr;
    }
    else if ( nSpaceCount > 7 )
    {
   csFileName = csFileInfoStr.Mid ( i );
   csFileName.TrimLeft(); csFileName.TrimRight();
   break;
    }
   }
   bLastCharIsSpace = TRUE;
}
else
{
   bLastCharIsSpace = FALSE;
}
}


GetFileTimeInfo ( csFileTime1, csFileTime2, csFileTime3 );
}


void CDownloadFtp::GetFileTimeInfo(CString csFileTime1, CString csFileTime2, CString csFileTime3)
{
if ( csFileTime3.IsEmpty() ) return;
CString csYear, csMonth, csDay, csHour, csMinute, csSecond;
int nMonth = GetMouthByShortStr ( csFileTime1 );
ASSERT ( nMonth >= 1 && nMonth
页: [1]
查看完整版本: VC++ 实现FTP下载的通用类