一个好用的封装好的FTPClient

来源: 

https://www.codeproject.com/Articles/8667/FTP-Client-Class


将上面文章中的FTPClient文件夹中添加到自己工程时还需要添加:
#pragma comment (lib, "Ws2_32.lib")

否则会有连接错误

 

下面仅贴出例子:

void TestFTP()
{
   nsFTP::CFTPClient ftpClient;
   nsFTP::CLogonInfo logonInfo(_T("localhost"), 21, _T("anonymous"),
                                           _T("<a href="mailto:anonymous@user.com">anonymous@user.com"));

   // 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("<a href="mailto:anonymous@user.com">anonymous@user.com"));

   nsFTP::CFTPClient ftpClientTarget;
   nsFTP::CLogonInfo logonInfoTarget(_T("targetftpserver"), 21, _T("anonymous"),
                                                       _T("<a href="mailto:anonymous@user.com">anonymous@user.com"));

   // 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("<a href="mailto:anonymous@user.com">anonymous@user.com</a>"));

   // 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();
}
posted on 2018-06-27 17:13  南枝  阅读(352)  评论(0编辑  收藏  举报