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

来源:互联网 发布:大学数据新闻作品 编辑:程序博客网 时间:2024/04/30 01:21

// 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[NET_BUFFER_SIZE] = {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 Mar  9 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[0]==' ' );
 int nSpaceCount = 0;
 CString csFileTime1, csFileTime2, csFileTime3;
 CString csNodeStr;
 CString csFileName;
 for ( int i=0; i<csFileInfoStr.GetLength(); i++ )
 {
  if ( csFileInfoStr[i]==' ' )
  {
   if ( !bLastCharIsSpace )
   {
    csNodeStr = csFileInfoStr.Mid ( i );
    csNodeStr.TrimLeft (); csNodeStr.TrimRight ();
    nSpaceCount ++;
    int nFindPos = csNodeStr.Find ( " ", 0 );
    if ( nFindPos < 0 ) nFindPos = csNodeStr.GetLength() - 1;
    csNodeStr = csNodeStr.Left ( nFindPos );
    // 文件大小
    if ( nSpaceCount == 4 )
    {
     if ( m_nIndex == -1 ) // 主线程才需要获取文件大小的信息
     {
      m_nFileTotalSize = (int)atoi(csNodeStr);
      DownloadNotify ( m_nIndex, NOTIFY_TYPE_GOT_REMOTE_FILESIZE, (LPVOID)m_nFileTotalSize, m_pDownloadMTR );
      int nWillDownloadStartPos = Get_WillDownloadStartPos (); // 开始位置
      int nWillDownloadSize = Get_WillDownloadSize();    // 本次应该下载的字节数
      int nDownloadedSize = Get_DownloadedSize ();    // 已下载字节数
      if ( m_nFileTotalSize > 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 <= 12 );
 csMonth.Format ( "%02d", nMonth );

 COleDateTime tOleNow = COleDateTime::GetCurrentTime ();
 csDay.Format ( "%02d", atoi(csFileTime2) );
 csSecond = "00";
 // 带年的日期,如:Aug 21 2006
 if ( csFileTime3.Find ( ":", 0 ) < 0 )
 {
  csYear = csFileTime3;
  csHour = "00";
  csMinute = "00";
 }
 // 不带年的日期,如:Feb 21 01:11
 else
 {
  csYear.Format ( "%04d", tOleNow.GetYear() );
  int nPos = csFileTime3.Find ( ":", 0 );
  if ( nPos < 0 ) nPos = csFileTime3.GetLength() - 1;
  csHour = csFileTime3.Left ( nPos );
  csMinute = csFileTime3.Mid ( nPos+1 );
 }

 CString csFileTimeInfo;
 csFileTimeInfo.Format ( "%s-%s-%s %s:%s:%s", csYear, csMonth, csDay, csHour, csMinute, csSecond );
 ConvertStrToCTime ( csFileTimeInfo.GetBuffer(0), m_TimeLastModified );
}

0 0