C++ FTP相关,简单实现

来源:互联网 发布:keyshot软件下载 编辑:程序博客网 时间:2024/05/07 17:40

ps:做个简单的上传FTP处理要用,做个记录。FTP使用vsftpd实现。

part1:

 使用afxinet.h提供的来完成,这个是属于MFC的。刚好我需要完成程序还不能使用MFC大哭

头文件:当然是#include <afxinet.h>

简单使用MSDN例程,

CInternetSession sess(_T("My FTP Session"));CFtpConnection* pConnect = NULL;try{   // Request a connection to ftp.microsoft.com. Default   // parameters mean that we'll try with username = ANONYMOUS   // and password set to the machine name @ domain name   pConnect = sess.GetFtpConnection(_T("192.168.1.1","","",21));//这里输入参数是服务器地址+登陆名+密码+端口   pConnect->SetCurrentDirectory(L"test");//传入的文件   CString strLocfile,strRemotefile;  //传入FTP文件名 和 源文件信息   int m = pConnect->PutFile(strLocfile,strRemotefile,FTP_TRANSFER_TYPE_BINARY,1);   if (!m)   {        _tprintf_s(_T("UpLoad Failed"));    }            pConnect->Close();}catch (CInternetException* pEx){   TCHAR sz[1024];   pEx->GetErrorMessage(sz, 1024);   _tprintf_s(_T("ERROR!  %s\n"), sz);   pEx->Delete();}// if the connection is open, close itif (pConnect != NULL) {   pConnect->Close();   delete pConnect;}
这样就基本实现了。


part2:

原有工程不能用MFC  只有换了"wininet.h"。这个是基于API实现的,前者也是封装这个实现的。

头文件:#include "wininet.h"

CString strLocfile,strRemotefile;HINTERNET hOpen = InternetOpen(L"My FTP Session", INTERNET_OPEN_TYPE_DIRECT, NULL, NULL, 0);HINTERNET hConnection  = InternetConnect(hOpen,_T("192.168.1.1"),INTERNET_DEFAULT_FTP_PORT,_T(""),_T(""),INTERNET_SERVICE_FTP,INTERNET_FLAG_PASSIVE,0);if (hConnection == 0)  {_tprintf_s(_T("Connection Failed\n"));  }BOOL BRET;BRET = FtpSetCurrentDirectory(hConnection,_T("test"));if (BRET == FALSE )  {_tprintf_s(_T("Floder Find Failed\n"));  }int m = FtpPutFile(hConnection,strLocfile,strRemotefile, FTP_TRANSFER_TYPE_ASCII, 0);if (!m)  {_tprintf_s(_T("UpLoad Failed"));   }

OK 简单实现。

FTP :https://msdn.microsoft.com/zh-cn/data/aa384180%28v=vs.90%29

WinINet API:http://blog.csdn.net/bobo1394/article/details/447043


0 0
原创粉丝点击