#include <windows.h>
#include <vector>
#include<io.h>
#include <wininet.h>
#include <iostream>
using namespace std;
//获取文件和文件大小
void GetFiles( string path, vector<string>& files );
/**
* 当上传程序运行在Windows环境,上传文件到Linux环境
* 为防止目的路径和文件名中文乱码
* 需要将目的路径和文件名从GB2312转码为UTF-8
* 反之,从Linux上传文件刀Windows,需要从UTF-8转码为GB2312
*/
//GB2312到UTF-8的转换
char* G2U(const char* gb2312);
//UTF-8到GB2312的转换
char* U2G(const char* utf8);
int main()
{
//ftp地址
string ftpSvrIp="10.192.XXX.XX";
//ftp端口
int port=21;
//用户名
string userName="user";
//用户密码
string password="12345";
//上传文件源路径
string sourceFilePath="C:/Desktop/测试";
//上传文件目的路径
string desFilePath="./test/测试/海康/";
vector<string> files;
//判断上传的是文件还是文件夹标识
int size=0;
if(sourceFilePath.find(".")==string::npos)
{
//获取文件夹下所有文件名
GetFiles(sourceFilePath, files );
char str[30];
size = files.size();
}
HINTERNET hint;
HINTERNET hftp;
do{
hint = InternetOpen(0, INTERNET_OPEN_TYPE_PRECONFIG, 0, 0, 0);
if (hint==NULL)
{
break;
}
//创建ftp连接
hftp = InternetConnect(hint, ftpSvrIp.c_str(),port,userName.c_str(),password.c_str(), INTERNET_SERVICE_FTP, 0, 0);
if (hftp==NULL)
{
break;
}
//如果目的路径不存在,一级一级创建
//目的路径格式为:"./dir/dir1/.../"
int pos=1;
string tempPath;
while(pos>0)
{
// 从第二个“/”开始依次找到目的路径中的“/”位置
pos=desFilePath.find_first_of('/',pos+1);
if(pos==-1)
break;
tempPath=desFilePath.substr(0,pos+1);
if(_access(tempPath.c_str(),0)==-1)
{
FtpCreateDirectory(hftp,tempPath.c_str());
}
}
//如果FtpSetCurrentDirectory一直报错,看一下desFilePath路径是否创建成功,用资源管理器看能否直接访问ftp
if (FtpSetCurrentDirectory(hftp,desFilePath.c_str()))
{
//上传文件源为一个文件
if(0==size)
{
//获取上传路径中的文件名
int pos=sourceFilePath.find_last_of('/');
string desfilename=sourceFilePath.substr(pos+1);
int i=FtpPutFile(hftp,sourceFilePath.c_str(),desfilename.c_str(), FTP_TRANSFER_TYPE_BINARY, 0);
}
//上传文件源为文件夹
else
{
for (int i = 0;i < size;i++)
{
std::string tempFilePath=files[i].c_str();
//获取上传路径中的文件名
int pos=tempFilePath.find_last_of('/');
string desfilename=tempFilePath.substr(pos+1);
FtpPutFile(hftp,tempFilePath.c_str(),desfilename.c_str(), FTP_TRANSFER_TYPE_BINARY, 0);
}
}
}
}while(0);
InternetCloseHandle(hftp);
InternetCloseHandle(hint);
cout<<"上传文件完成"<<endl;
system("pause");
return 0;
}
void GetFiles( string path, vector<string>& files )
{
//文件句柄
long hFile = 0;
//文件信息
struct _finddata_t fileinfo;
std::string p;
if((hFile = _findfirst(p.assign(path).append("/*").c_str(),&fileinfo))!= -1)
{
do
{
//如果是目录,迭代之
//如果不是,加入列表
if((fileinfo.attrib & _A_SUBDIR))
{
if(strcmp(fileinfo.name,".") != 0 && strcmp(fileinfo.name,"..")!= 0)
GetFiles( p.assign(path).append("/").append(fileinfo.name), files );
}
else
{
files.push_back(p.assign(path).append("/").append(fileinfo.name));
}
}while(_findnext(hFile, &fileinfo)==0);
_findclose(hFile);
}
}
//UTF-8到GB2312的转换
char* U2G(const char* utf8)
{
int len = MultiByteToWideChar(CP_UTF8, 0, utf8, -1, NULL, 0);
wchar_t* wstr = new wchar_t[len+1];
memset(wstr, 0, len+1);
MultiByteToWideChar(CP_UTF8, 0, utf8, -1, wstr, len);
len = WideCharToMultiByte(CP_ACP, 0, wstr, -1, NULL, 0, NULL, NULL);
char* str = new char[len+1];
memset(str, 0, len+1);
WideCharToMultiByte(CP_ACP, 0, wstr, -1, str, len, NULL, NULL);
if(wstr) delete[] wstr;
return str;
}
//GB2312到UTF-8的转换
char* G2U(const char* gb2312)
{
int len = MultiByteToWideChar(CP_ACP, 0, gb2312, -1, NULL, 0);
wchar_t* wstr = new wchar_t[len+1];
memset(wstr, 0, len+1);
MultiByteToWideChar(CP_ACP, 0, gb2312, -1, wstr, len);
len = WideCharToMultiByte(CP_UTF8, 0, wstr, -1, NULL, 0, NULL, NULL);
char* str = new char[len+1];
memset(str, 0, len+1);
WideCharToMultiByte(CP_UTF8, 0, wstr, -1, str, len, NULL, NULL);
if(wstr) delete[] wstr;
return str;
}
没有合适的资源?快使用搜索试试~ 我知道了~
资源推荐
资源详情
资源评论
收起资源包目录
TestFtpUpload.zip (10个子文件)
TestFtpUpload
Release
Debug
TestFtpUpload.ilk 609KB
TestFtpUpload.sln 905B
TestFtpUpload
Release
BuildLog.htm 7KB
TestFtpUpload.vcproj 4KB
Debug
BuildLog.htm 7KB
main.cpp 4KB
TestFtpUpload.vcproj.HIK.luyijun.user 1KB
WinInet.Lib 61KB
TestFtpUpload.suo 22KB
TestFtpUpload.ncb 15.53MB
共 10 条
- 1
资源评论
黔家仔
- 粉丝: 111
- 资源: 10
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
最新资源
- 原生微信小程序源码 - -仿豆瓣图书
- 原生微信小程序源码 - -滴滴公交-查公交
- 人工智能实验四 感知器算法的设计实现
- java小项目多线程多线程 复制文件 冒泡排序 群聊
- 四数之和(java代码).docx
- 701837906919458TapScanner v3.0.10 (Pro).apk
- 青岛大学人工智能实验二 利用α-β搜索的博弈树算法编写一字棋游戏
- ### 1、项目介绍 本项目Scrapy进行数据爬取,并使用Django框架+PyEcharts实现可视化大屏 效果如下:
- # 微信小程序-健康菜谱 基于微信小程序的一个查找检索菜谱的应用 ### 效果 !动态图(./res/gif/demo
- zabbix-get命令包资源
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功