#include <ncftp.h>
#pragma comment(lib,"libncftp.lib")
#pragma comment(lib,"sio.lib")
#pragma comment(lib,"Strn.lib")

void upload()
{
 InitWinsock();
 
 FTPLibraryInfo m_li;
 FTPInitLibrary(&m_li);

 FTPConnectionInfo m_ci;
 FTPInitConnectionInfo(&m_li, &m_ci, kDefaultFTPBufSize);

 //进度指示
 m_ci.useProgressMeter = 1;
 //进度指示回调函数
 m_ci.progress = ProcFTPProgressMeter;

 //登录
 strncpy(m_ci.host, "localhost", sizeof(m_ci.host) - 1);
 strncpy(m_ci.user, "username", sizeof(m_ci.user) - 1);
 strncpy(m_ci.pass, "password", sizeof(m_ci.pass) - 1);
 int ret = FTPOpenHost(&m_ci) ;
 assert(ret == 0);

 //上传文件
 int result = FTPPutFiles3(
  &m_ci,
  "C:\\test.txt",
  "/",
  kRecursiveNo,
  kGlobYes,
  kTypeBinary,
  kAppendNo,
  NULL,
  ".tmp",
  kResumeNo,
  kDeleteNo,
  ProcFTPConfirmResumeUpload, //文件已存在的处理方式回调函数
  0);
 assert(ret == 0);

 //重命名文件
 int result = FTPRename(
   &m_ci,
   "/test.txt",
   "/1-1-test.txt");
 assert(ret == 0);

 //退出登录 
 FTPAbortDataTransfer(&m_ci);
 FTPCloseHost(&m_ci);

 DisposeWinsock();
}

int ProcFTPConfirmResumeUpload(
 const FTPCIPtr cip,
 const char *localpath,
 longest_int localsize,
 time_t localmtime,
 const char **remotepath,
 longest_int remotesize,
 time_t remotetime,
 longest_int *startPoint)
{
 
 return kConfirmResumeProcSaidOverwrite;
 
 /*
 //可以根据自己的需要返回如下值
 kConfirmResumeProcSaidSkip
 kConfirmResumeProcSaidResume
 kConfirmResumeProcSaidOverwrite
 kConfirmResumeProcSaidAppend
 kConfirmResumeProcSaidBestGuess
 */
}

void ProcFTPProgressMeter(const FTPCIPtr f, int status)
{
 if ( status == kPrInitMsg )
 {
 }
 else if (status == kPrEndMsg )
 { 
 }
 else if (status==kPrUpdateMsg )
 {
  //percentCompleted=f->percentCompleted;
  //kBytesPerSec=f->kBytesPerSec;
  //bytesTransferred=f->bytesTransferred;
 } 
}

 这段代码基于libncftp-3.2.5。

参考网站:
http://www.ncftp.com/libncftp/
http://www.ibm.com/developerworks/cn/aix/library/au-ncftp_flex/


本文链接地址: libncftp的基本用法
http://qingfengju.com/index.asp?id=333

分类:Win32/C++ 查看次数:4434 发布时间:2013-4-20 10:42:23