FTP开发中下载文件的两种方式

1. 通过 GetFile 方式下载
2. 通过 CInternetFile::Read 方式下载

两种方式的区别:
第一种方式,操作级别较高。直接调用就好,这种方式封装了一切操作。
第二种方式,自己可以控制。其基本原理,就是在网络上打开一个文件,就像本地打开文件一样。读取,然后写入到本地文件。

以下代码,两种方式都有,第二种方式注释掉了。打开即可使用。

在FTP下载中,碰到的奇异问题:
下载大于100M的文件。
今天,下载到最后一块,出现timeout。
在本地建立ftp良好。
在其他测试环境良好。
下一步,在另外一个计算机上建立ftp再测试下。确定是ftp服务器问题,还是客户端问题。
另外出问题的时候,用filezilla客户端下载,良好。

找到一个 FTP Client 类。使用后,再议。
http://www.codeproject.com/KB/IP/ftpclientclass.aspx

服务器端使用filezilla 。

5.15问题已解决,和信令无关。

bool DownloadFile(char *filename)
{
 CInternetSession* pInetSession; //会话对象
 CFtpConnection* pFtpConnection; //连接对象
 
 pInetSession=new CInternetSession(AfxGetAppName(),1,PRE_CONFIG_INTERNET_ACCESS);
 
 try
 {
  //新建连接对象
  pFtpConnection=pInetSession->GetFtpConnection("127.0.0.1", "sa", "111");
 }
 catch(CInternetException *pEx)
 {
  //获取错误
  TCHAR szError[1024];
  if(pEx->GetErrorMessage(szError,1024))
   AfxMessageBox(szError);
  else 
   AfxMessageBox("There was an exception");
  pEx->Delete();
  
  return FALSE;
 }
 
 TCHAR tchBuffer[MAX_PATH];
 GetCurrentDirectory(MAX_PATH, tchBuffer);
 strcat(tchBuffer, filename + 1);
 
 
 if(pFtpConnection->GetFile(filename, "d:\\2.exe", false))
 {
  //AfxMessageBox("下载成功");
 }
 else
  DWORD dResult = GetLastError();

/* 
 CInternetFile *pInternetFile = pFtpConnection->OpenFile(filename, GENERIC_READ, FTP_TRANSFER_TYPE_BINARY, 1);
 char szBuffer[4096];

 CFile file("d:\\1.exe", CFile::modeWrite | CFile::modeCreate | CFile::typeBinary);
 UINT uRead;

 int count = 0;
 
 while (true)
 {
  try
  {
   uRead = pInternetFile->Read(szBuffer, 4096);
   ++count;
   TRACE2("%d %d\n", count, uRead);

  }
  catch (CInternetException *pEx)
  {
   //获取错误
   TCHAR szError[1024];
   if(pEx->GetErrorMessage(szError,1024))
    AfxMessageBox(szError);
   else 
    AfxMessageBox("There was an exception");
   
   pEx->Delete();
  }
  
  file.Write(szBuffer, uRead);

  if ( uRead < 4096 )
   break;
  
 }

 file.Close();
 pInternetFile->Close();
 delete pInternetFile; */

 if ( NULL != pFtpConnection )
  pFtpConnection->Close();
 delete pFtpConnection;
 delete pInetSession;

 return true;

}

第二种方式下载的函数
CString GetFTPFile(const char *ftpserver, const char *remotefile,
                      const char *localfile )
{
    TCHAR   szCause[255];
    CString sCause;
    sCause.Format(""); 
 
    try
    {
  TCHAR ftpAgent[256] = _T("FTPSource");
        //create the internet session

        CInternetSession mysession(ftpAgent,
   1,
   INTERNET_OPEN_TYPE_PRECONFIG,
   NULL,
   NULL,
     INTERNET_FLAG_DONT_CACHE);

        //get the ftp connection from internet session
        CFtpConnection* pFtpConn = mysession.GetFtpConnection(ftpserver,"sa","111");
       
        if(pFtpConn)
        {
   CFtpFileFind finder(pFtpConn);
   finder.FindFile(remotefile);
   finder.FindNextFile();
   
   FILE* fp = fopen(localfile,"wb");
   if(fp == NULL)
   {
    return "file open error";
   }
   __int64  _fileSize = finder.GetLength64();
   
   CInternetFile* _pFtpFile = pFtpConn->OpenFile(remotefile,
    GENERIC_READ,
    FTP_TRANSFER_TYPE_BINARY|INTERNET_FLAG_DONT_CACHE,
    1);
   
   _pFtpFile->SetReadBufferSize(READSIZE);
   int i = 1;
            while(_fileSize > 0 )
   {
    char temp[READSIZE]= "\0";
    if(_fileSize > READSIZE)
    {
     
     _pFtpFile->Read((LPVOID)temp, READSIZE );     
     _fileSize = _fileSize - READSIZE;
     
     fwrite(temp,READSIZE,1,fp);
    }
    else
    {
     _pFtpFile->Read((LPVOID)temp, _fileSize );  
     fwrite(temp,sizeof(char),_fileSize,fp);
     _fileSize = 0;
    }
   }
   _pFtpFile->Close();
   fclose(fp);
        }
        else
        {
   //could not connect
   sCause.Format("Could not connect to ftp server!! \nPlease "
    "ensure you have correct address and access rights!");
        }     
    }
 
    catch(CException* error)
    {
        error->GetErrorMessage(szCause,254,NULL);
        sCause.Format("%s",szCause);
    }

    return (sCause);
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值