稳定盈利的期货交易方法-量化趋势交易

alantop -专业量化投资者

爱好:量化投资,逆向工程,渗透
随笔 - 595, 文章 - 0, 评论 - 921, 引用 - 0
数据加载中……

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);
}

posted on 2008-05-14 01:51 AlanTop 阅读(2836) 评论(2)  编辑 收藏 引用 所属分类: C++VC++

评论

# re: FTP开发中下载文件的两种方式[未登录]  回复  更多评论   

第一个信令端口超时了 一定间隔内发点命令过去就好
最后下载结束的时候 会检测信令端口是否超时的
2008-05-14 08:56 | cppexplore

# re: FTP开发中下载文件的两种方式  回复  更多评论   

能支持断点续传么?
2008-05-15 16:43 | 买书网

只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理