c#如何通过ftp上传文件_ftp上传文件

一、FTP服务器

常用的FTP服务器有:Server-U,Filezilla Server,IIS。

Server-U的特点是功能强大,但是需要收费。

FileZilla Server是一种小巧、快速、可信赖的支持FTP以及SFTP的服务器端。它是开源的,并且具有很丰富的操作接口。

IIS是微软自带的FTP服务器,但是配置和操作非常的复杂。

25c819556d33b371e37686fb3749338d.png

Server-U

f3533425f42ed38b5b4bc68edaad3d06.png

Filezilla Server

二、FTP客户端

常见FTP客户端工具:filezilla、LeapFTP、CuteFTP

928a86e3b16c40cd19a9e74f346bc029.png

filezilla

6b2e25a7ed0e0fd35857f38f6b6ae454.png

LeapFTP

910a8b5c4f1f2fd3b2308005089865a7.png

CuteFTP

三、C++ FTP客户端操作框架

C++ FTP客户端框架:ftplibpp、ftplib、windows系统Wininet函数、libcurl、ftp.exe命令上传与下载文件。

ftplibpp, 提供ftp客户端功能的平台独立 C++ 库,支持Linux、Mac、window系统,支持 fxp, ssl/tl加密。https://github.com/mkulke/ftplibpp

ftplib, 提供ftp客户端功能的平台独立 C库,支持Linux (X86), Mac OS-X and OpenVMS (AXP)系统。http://nbpfaus.net/~pfau/ftplib/

windows系统Wininet函数,https://docs.microsoft.com/zh-cn/windows/win32/wininet/ftp-sessions

注意:windows中ftp.exe命令上传与下载文件方式比其他方式更加有效,其他方式不太稳定。

1、ftplibpp

函数说明:https://www.helplib.com/GitHub/article_110777

vs2015工程如何使用ftplib?

1)添加ftplib.h ftplib.cpp文件到工程中。

2)预处理器定义中添加NOSSL NOLFS _CRT_SECURE_NO_WARNINGS

3)ftplib.h头文件中增加

#include #ifndef _WIN32#include #include #else#include #endif

2、ftplib

函数说明:http://nbpfaus.net/~pfau/ftplib/ftplib.html

3、windows系统Wininet函数

步骤:

1) InternetOpen初始化一个Internet句柄。此句柄用于建立一个FTP session。

2)InternetConnect创建一个FTP session。INTERNET_DEFAULT_FTP_PORT for the nServerPort parameter and INTERNET_SERVICE_FTP for the dwService parameter.

3)执行必要的操作。比如FtpPutFile、FtpGetFile、FtpDeleteFile、FtpRenameFile、FtpCreateDirectory、FtpRemoveDirectory、FtpGetCurrentDirectory、FtpSetCurrentDirectory等。

4)InternetCloseHandle关闭由InternetConnect创建的FTP session。

5)InternetCloseHandle关闭由InternetOpen创建的FTP session。

FtpCreateDirectory、FtpDeleteFile及之后的几个函数都需要InternetConnect返回的句柄。

常见函数介绍:

HINTERNET InternetOpen(

LPCTSTR lpszAgent,// 指定调用 WinINet 函数的应用程序或入口。该入口用作HTTP协议中用户代理项。其实是自定义的名称。如”MyFtp”、“mwj”等。

DWORD dwAccessType,//一般为INTERNET_OPEN_TYPE_PRECONFIG:返回注册表中的代理或直接的配置。

LPCTSTR lpszProxyName,//一般为NULL。若参数dwAccessType不是INTERNET_OPEN_TYPE_PROXY,此参数应被设为NULL。

LPCTSTR lpszProxyBypass,//一般为NULL。若参数dwAccessType不是INTERNET_OPEN_TYPE_PROXY,此参数应被设为NULL。

DWORD dwFlags);// INTERNET_FLAG_ASYNC:仅能用于作用在该函数返回的句柄的子句柄上的异步请求。INTERNET_FLAG_OFFLINE 与 INTERNET_FLAG_FROM_CACHE 相同:不做网络请求。所有的实体都由缓存返回。若请求条目不在缓存中,将返回一个错误。对于遍历FTP服务器上的文件夹时,此参数必须为0。

HINTERNET WINAPI InternetConnect(

HINTERNET hInternet, //InternetOpen返回的句柄

LPCTSTR lpszServerName, //要连接的Internet server的名字或IP

INTERNET_PORT nServerPort, //对FTP用INTERNET_DEFAULT_FTP_PORT

LPCTSTR lpszUserName, //对FTP可用“anonymous”。设为NULL,对FTP将自动设为anonymous

LPCTSTR lpszPassword, //若为NULL,对FTP则自动使用anonymous的默认密码

DWORD dwService, //对FTP用INTERNET_SERVICE_FTP

DWORD dwFlags, //一般为0

DWORD dwContext);//一般为0

此函数不仅可连接FTP还可连接HTTP。返回NULL表明连接失败。

FtpFindFirstFile和InternetFindNextFile遍历ftp文件

WIN32_FIND_DATA fd;HINTERNET hFind = FtpFindFirstFile(hFtpSession, "/*.*", &fd, INTERNET_FLAG_RELOAD, 0);if(hFind != INVALID_HANDLE_VALUE){    BOOL bFind = TRUE;    while(bFind)    {        bFind = InternetFindNextFile(hFind, &fd);        OutputDebugString(fd.cFileName);        OutputDebugString("");    }}InternetCloseHandle(hFind);实例:#include void main(){     BOOL dRes,pRes;     HINTERNET hInternet;     HINTERNET hConnect;     hInternet = InternetOpen("Test Sample", INTERNET_OPEN_TYPE_DIRECT,          NULL, NULL, INTERNET_FLAG_NO_CACHE_WRITE);     if ( NULL == hInternet )     {          printf("InternetOpen Error:%d", GetLastError() );     }    hConnect  = InternetConnect(hInternet, "127.0.0.1"/*FTP服务器地址*/, INTERNET_DEFAULT_FTP_PORT/*FTP端口号,此为默认值---21*/,          "admin"/*用户名*/,  "123456"/*密码*/, INTERNET_SERVICE_FTP,          INTERNET_FLAG_EXISTING_CONNECT || INTERNET_FLAG_PASSIVE,0 );     if ( NULL == hInternet )     {         printf( "InternetConnect Error:%d", GetLastError() );         InternetCloseHandle(hInternet);     }dRes = FtpGetFile(hConnect, "./download/test.txt", "D:est.txt", FALSE,               FILE_ATTRIBUTE_ARCHIVE, FTP_TRANSFER_TYPE_UNKNOWN, 0);     if ( dRes == 0 )     {        printf( "FtpGetFile Error:", GetLastError() );     }else{         printf( "下载文件成功!" );     }     pRes = FtpPutFile(hConnect,"D:est.txt","test.txt",FTP_TRANSFER_TYPE_ASCII,0);    if(pRes==0) {  printf("上传文件失败!"); }else{  printf("上传文件成功!"); }    InternetCloseHandle(hConnect);    InternetCloseHandle(hInternet); if(dRes&&pRes) return true;    else return false;

4、libcurl实现ftp客户端(上传、下载、进度、断点续传)

https://blog.csdn.net/wu110112/article/details/72898630

https://blog.csdn.net/u012234115/article/details/83869486

5、ftp.exe命令上传文件

bool FtpUploadFile(std::string strUuid,std::string strIp,int nPort,std::stringstrLoginUsername,std::string strLoginPassword,std::string strMainPath,std::stringstrSubPath,std::string strLocalFilePath,std::string strRomuteFileName,bool bIsBinary){std::string strCommandFile = strMainPath;strCommandFile += "//";strCommandFile += strUuid;strCommandFile += "-command.tmp";FILE * pCommandFile = fopen(strCommand.c_str(),"w+");std::string strFileName = "";char *pSrcFilePath = (char *)strLocalFilePath.c_str();char *pFindPos = strrchr(pSrcFilePath,'/');if(pFindPos == NULL){pFindPos = strrchr(pSrcFilePath,'');}if(pFindPos != NULL){strFileName = strLocalFilePath.substr((pFindPos-pSrcFilePath)+1,strLocalFilePath.size()-((pFindPos-pSrcFilePath)+1));}if(pCommandFile != NULL){fprintf(pCommandFile,"open %s %d",strIp.c_str(),nPort);fprintf(pCommandFile,"USER %s",strLoginUsername.c_str());fprintf(pCommandFile,"%s",strLoginPassword.c_str());//create directoryfprintf(pCommandFile,"mkdir %s",strMainPath.c_str());fprintf(pCommandFile,"cd %s",strMainPath.c_str());fprintf(pCommandFile,"mkdir %s",strSubPath.c_str());fprintf(pCommandFile,"cd %s",strSubPath.c_str());if(bIsBinary){fprintf(pCommandFile,"binary");}else{fprintf(pCommandFile,"ascii");}fprintf(pCommandFile,"prompt off");fprintf(pCommandFile,"delete %s",strRomuteFileName.c_str());fprintf(pCommandFile,"put %s",strLocalFilePath.c_str());//renameif(strRomuteFileName.size() > 0 && strFileName != strRomuteFileName){fprintf(pCommandFile,"rename %s %s",strFileName.c_str(),strRomuteFileName.c_str());}fprintf(pCommandFile,"quit");fclose(pCommandFile);std::string strParameter = "-n -s:" + strCommandFile;SHELLEXECUTEINFO shExecInfo = {0};shExecInfo.cbSize = sizeof(SHELLEXECUTEINFO);shExecInfo.fMask = SEE_MASK_NOCLOSEPROCESS;shExecInfo.hwnd = NULL;shExecInfo.lpVerb = NULL;shExecInfo.lpFile = "ftp.exe";shExecInfo.lpParameters = strParameter;shExecInfo.lpDirectory = NULL;shExecInfo.nShow = SW_HIDE;shExecInfo.hInstApp = NULL;ShellExecuteEx(&shExecInfo);WaitForSingleObject(shExecInfo.hProcess,INFINITE);DeleteFile(strCommandFile.c_str());return true;}else{return false;}}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值