c++ 实现FTP的上传和下载

好久没整理过博客了,整理下最近杂七杂八的东西

前段时间用到FTP的上传下载,网上有很多封装好的库,有的功能很多,有的很精简。大家找适合自己项目的就好了

这次我用的都是基本功能,时间也紧加上自己也懒得具体写,就找了个用。中间遇到一个很好的库POCO,但是太大了我没必要用这么大的,但是这个库很好,值得看看,说明文档非常详细

至于怎么建FTP服务器,很简单,随便搜一下都可以找到,就不说了。说一下我用的这个mini库,在后面把源文件贴上

用法也忒简单了,把源文件加到工程中就可以 了

void TestFTP()
{
   nsFTP::CFTPClient ftpClient;
   nsFTP::CLogonInfo logonInfo(_T("localhost"), 21, _T("anonymous"),
                                           _T("[email protected]"));

   // connect to server
   ftpClient.Login(logonInfo);

   // get directory listing
   nsFTP::TFTPFileStatusShPtrVec list;
   ftpClient.List(_T("/"), list);

   // iterate listing
   for( nsFTP::TFTPFileStatusShPtrVec::iterator it=list.begin();
                                            it!=list.end(); ++it )
       TRACE(_T("\n%s"), (*it)->Name().c_str());

   // do file operations
   ftpClient.DownloadFile(_T("/pub/test.txt"), _T("c:\\temp\\test.txt"));

   ftpClient.UploadFile(_T("c:\\temp\\test.txt"), _T("/upload/test.txt"));

   ftpClient.Rename(_T("/upload/test.txt"), _T("/upload/NewName.txt"));

   ftpClient.Delete(_T("/upload/NewName.txt"));

   // disconnect
   ftpClient.Logout();
}

void TestFXP()
{
   nsFTP::CFTPClient ftpClientSource;
   nsFTP::CLogonInfo logonInfoSource(_T("sourceftpserver"), 21, _T("anonymous"),
                                                       _T("[email protected]"));

   nsFTP::CFTPClient ftpClientTarget;
   nsFTP::CLogonInfo logonInfoTarget(_T("targetftpserver"), 21, _T("anonymous"),
                                                       _T("[email protected]"));

   // connect to server
   ftpClientSource.Login(logonInfoSource);
   ftpClientTarget.Login(logonInfoTarget);


   // do file operations
   nsFTP::CFTPClient::TransferFile(ftpClientSource, _T("/file.txt"),
                                   ftpClientTarget, _T("/newFile.txt"));


   // disconnect
   ftpClientTarget.Logout();
   ftpClientSource.Logout();
}


void TestDownloadAsciiFileIntoTextBuffer()
{
   nsFTP::CFTPClient ftpClientSource;
   nsFTP::CLogonInfo logonInfoSource(_T("sourceftpserver"), 21, _T("anonymous"),
                                                       _T("[email protected]"));

   // connect to server
   ftpClientSource.Login(logonInfoSource);

   nsFTP::COutputStream outputStream(_T("\r\n"), _T("Example"));

   // do file operations
   ftpClientSource.DownloadFile(_T("/file.txt"), outputStream,
                                nsFTP::CRepresentation(nsFTP::CType::ASCII()));

   tstring output = outputStream.GetBuffer();

   // disconnect
   ftpClientSource.Logout();
}

DEMO下载

http://download.csdn.net/detail/c914620529/9855471




你可能感兴趣的:(c++)