//MyFtp.h
# include <string>
class MyFtp
{
public:
bool UploadFile(std::string FileName);//上传文件
MyFtp(std::string FtpIp, int FtpPort, std::string m_FtpUserName, std::string m_FtpPassword);
private:
std::string m_FtpIp;
std::string m_FtpUserName;
std::string m_FtpPassword;
int m_FtpPort;
};
上面的文件为实现FTP功能的头文件
//MyFtp.cpp
/*
2018年4月23日10:50:04
代码目的:
通过Windows系统库实现本地文件的FTP上传,并且把上传的文件根据系统时间重新命名
因为本代码是广播功能的一部分,因此上传的文件均为wav格式的音频。因此直接把后缀名写成wav。
同时注意:代码中可以复用功能的还有系统时间的格式化成char*数组。
*/
#include <windows.h>
#include "MyFtp.h"
#include "stdio.h"
#include <time.h>
#include "wininet.h "
MyFtp::MyFtp(std::string FtpIp, int FtpPort, std::string m_FtpUserName, std::string m_FtpPassword)
{
this->m_FtpIp = FtpIp;
this->m_FtpPort = FtpPort;
this->m_FtpUserName = m_FtpUserName;
this->m_FtpPassword = m_FtpPassword;
}
//上传std::string 的文件名或路径。
bool MyFtp::UploadFile(std::string FileName)
{
HINTERNET hInternet;
HINTERNET hConnect;
hInternet = InternetOpen("A3G3 Sample", INTERNET_OPEN_TYPE_DIRECT,
NULL, NULL, INTERNET_FLAG_NO_CACHE_WRITE);
if (NULL == hInternet)
{
printf("InternetOpen Error:%d\n", GetLastError());
}
hConnect = InternetConnect(hInternet, (this->m_FtpIp).c_str(), this->m_FtpPort,(this->m_FtpUserName).c_str(), (this->m_FtpPassword).c_str(),
INTERNET_SERVICE_FTP, INTERNET_FLAG_EXISTING_CONNECT||INTERNET_FLAG_PASSIVE, 0);
if (NULL == hInternet)
{
printf("InternetConnect Error", GetLastError());
InternetCloseHandle(hInternet);
}
BOOL pRes;
char SaveName[30];
// 基于当前系统的当前日期/时间,组织成特定名称的文件名,避免上传的文件名FTP服务器文件名同名。
time_t now = time(0);
tm *t = localtime(&now);
sprintf_s(SaveName, "%d-%d-%d %d-%d-%d.wav", t->tm_year+1900, t->tm_mon+1, t->tm_mday, t->tm_hour, t->tm_min, t->tm_sec);
std::string fileName(SaveName);
//FTP服务器的目录层次,要上传的目录路径为FTP根目录/BoradCastFile/下。
std::string DestDir = "/BroadCastFile/"+fileName;
pRes = FtpPutFile(hConnect, FileName.c_str(), DestDir.c_str(), FTP_TRANSFER_TYPE_ASCII, 0);
bool Flag;
if (pRes)
{
printf("上传文件成功\n");
Flag = true;
}
else
{
printf("上传文件失败\n");
Flag = false;
}
InternetCloseHandle(hConnect);
InternetCloseHandle(hInternet);
return Flag;
}
//main.cpp
/*
2018年4月23日10:55:09
代码目的:测试FTP功能可用性。
同时演示了标准库std::string与char*的转换。char*的打印。
同时也要注意的是,windows系统路径分隔符。如//D:\\Users\\cetc03.hik\\a.txt可以
而在Python中,标识同样的路径,则要使用
D:\\\\Users\\\\cetc03.hik\\\\a.txt
*/
# include "MyFtp.h"
# include <string>
# include <io.h>
# include <iostream>
using namespace std;
int main(void)
{
char* ch = "12345";
std::string str = ch;
std::cout<<str<<std::endl;
str += ch;
std::cout<<str<<std::endl;
str += "aaa";
std::cout<<str<<std::endl;
char * c ="我爱你我爱你你爱我吗";
printf("c= %s", c);
MyFtp myFtp = MyFtp("10.20.80.39", 21, "testFTP", "hik12345+");
//注意:在windows系统中,访问指定目录的分隔符如下例子所示:
//D:\\Users\\cetc03.hik\\a.txt可以
//D:/Users/cetc03.hik//a.txt在windows系统也可以。
if (_access("D:\\Users\\cetc03.hik\\a.txt", 0) !=0 )
{
std::cout<<"文件不存在: "<<"D:\\Users\\cetc03.hik\\a.txt"<<std::endl;
}else
{
std::cout<<"文件存在: "<<"D:\\Users\\cetc03.hik\\a.txt"<<std::endl;
}
myFtp.UploadFile("D:\\Users\\cetc03.hik\\a.txt");
system("pause");
return 0;
}
通过代码,我们可以实现把本地文件上传到FTP服务器的指定路径,并且同时指定上传到FTP的文件的文件名FtpPutFile表示的是上传,在该wininet.h头文件下也可以查找检索,删除,下载等FTP功能。