Win32 API函数:
参考链接:https://blog.csdn.net/cddchina/article/details/38727545
- InternetOpen 初始化 Win32 internet
- InternetConnect 打开一个FTP, HTTP, or Gopher 应用会话
- FtpCreateDirectory 在服务器上建立一个新的目录
- FtpRemoveDirectory 删除服务器上的一个目录
- FtpOpenFile 打开服务器上的一个文件进行读写
- FtpGetFile 接收指定的文件并且在本地建立它
- FtpPutFile 发送指定文件到服务器
- FtpDeleteFile 删除服务器上一个指定的文件
- FtpSetCurrentDirectory 设置服务器上当前的工作目录
- FtpGetCurrentDirectory 返回服务器当前的工作目录
- FtpCommand 发送命令到服务器
- FtpFindFirstFile 返回文件信息。放在 WIN32_FIND_DATA 结构中
- InternetFindNextFile 调用 FtpFindFirstFile()后在目录中连续查找
- FtpRenameFile 修改服务器上指定的文件的名字
参考MSDN查询各个参数信息:https://docs.microsoft.com/en-us/windows/win32/api/wininet/nf-wininet-ftpgetfilea
封装类头文件:
#include <iostream>
#include <memory>
#include <windows.h>
#include <process.h>
#include <wininet.h>
#include <string>
#pragma comment(lib,"wininet.lib")//链接wininet.lib库
using namespace std;
class WinFTP
{
public:
WinFTP();
~WinFTP();
bool OpenFtp(string user, string pwd, string serverip, int serverport = INTERNET_DEFAULT_FTP_PORT);
bool GetCurDir(string &path);//获取当前路径
bool ChangeDir(string path);//切换当前路径
bool GetFile(string srcpath, string despath);
void CloseFtp();
private:
bool OpenInternet();
private:
HINTERNET hINTSession;//网络连接句柄
HINTERNET hFTPSession;
};
CPP文件:
WinFTP::WinFTP() :hINTSession(nullptr), hFTPSession(nullptr)
{
}
WinFTP::~WinFTP()
{
CloseFtp();
}
//这个函数初始化一个应用程序对WinInet函数的使用
bool WinFTP::OpenInternet()
{
hINTSession = InternetOpenA("FTPClient.exe", INTERNET_OPEN_TYPE_PRECONFIG, NULL, NULL, 0);
if (hINTSession == nullptr)
{
cout << "无法打开 hINTSession!" << endl;
return false;
}
return true;
}
//这个函数为指定的站点打开一个FTP或HTTP会话
bool WinFTP::OpenFtp(string user, string pwd, string serverip, int serverport)
{
if (!OpenInternet())
return false;
hFTPSession = InternetConnectA(hINTSession, serverip.c_str(), serverport, user.c_str(), pwd.c_str(), INTERNET_SERVICE_FTP, 0, 0);
if (hFTPSession == nullptr)
{
cout << "连接FTP失败!" << endl;
InternetCloseHandle(hINTSession);
return false;
}
return true;
}
//获取当前路径
bool WinFTP::GetCurDir(string &path)
{
unsigned long dwCurDir = 256;
path.resize(dwCurDir);
if (!FtpGetCurrentDirectoryA(hFTPSession, const_cast<char*>(path.c_str()), &dwCurDir)) //这个函数检索指定FTP会话的当前目录)
{
cout << "获取路径失败!" << endl;
return false;
}
return true;
}
//切换当前路径
bool WinFTP::ChangeDir(string path)
{
if (!FtpSetCurrentDirectoryA(hFTPSession, path.c_str()))
{
cout << "路径切换失败" << endl;
return false;
}
return true;
}
//下载文件。这里的参数指定可以参考MSDN,关于文件下载更新或者变化等情况
bool WinFTP::GetFile(string srcpath, string despath)
{
if (!FtpGetFileA(hFTPSession, srcpath.c_str(), despath.c_str(), FALSE, FILE_ATTRIBUTE_NORMAL, FTP_TRANSFER_TYPE_BINARY, 0))
{
cout << "下载失败" << endl;
return false;
}
return true;
}
//关闭ftp
void WinFTP::CloseFtp()
{
if (hINTSession == nullptr || hFTPSession == nullptr)
return;
InternetCloseHandle(hINTSession);
InternetCloseHandle(hFTPSession);
cout << "FTP关闭" << endl;
return;
}
测试:
int main()
{
WinFTP Ftp;
if (!Ftp.OpenFtp("test", "test", "192.168.99.72"))
{
cout << "连接失败" << endl;
return 0;
}
//string curdir;
//if (Ftp.GetCurDir(curdir))
//{
// cout << curdir << endl;
//}
//"切换目录"
//if (Ftp.ChangeDir("//fan_11//wind_1//") && Ftp.GetCurDir(curdir))
//{
// cout << curdir << endl;
//}
while (true)
{
string srcpath, despath;
cout << "输入目标:";
cin >> srcpath;
cout << endl << "输入指定:";
cin >> despath;
if (Ftp.GetFile(srcpath, despath))
{
cout << "下载成功" << endl;
}
else
{
cout << "下载失败" << endl;
}
}
return 0;
}
目标下载路径如果包含不存在文件需要自己创建,使用相对简单,做个笔记。