使用wininet向FTP服务器发送文件
.h
#pragma once
#include <windows.h>
#include <tchar.h>
#include <string>
#include <vector>
using namespace std; class WininetFtpClient
{
public:
WininetFtpClient(void);
~WininetFtpClient(void); public:
bool ConncetServer(const wstring & strServer, const wstring & strAccount, const wstring & strPswd);
bool PostFile(const wstring & srcPath, const wstring & ftpPath);
bool FindSpecificDirectoy(const wstring & filePath); private:
bool DownLoad(const char *Url, const char * filePathName);
LPVOID m_hSession;
LPVOID m_hConnect;
};
#include "WininetFtpClient.h"
#include <wininet.h>
#include <io.h>
#pragma comment(lib,"Wininet.lib") int SplitString(const wstring& strSrc, const wstring& strSplit, vector<wstring>& vecResult)
{
int pos = strSrc.find(strSplit, 0);
if (pos == -1)
{
return 0;
} int startPos = 0;
int splitN = pos;
wstring lineText(_T("")); while (pos > -1)
{
lineText = strSrc.substr(startPos, splitN);
startPos = pos + strSplit.length();
pos = strSrc.find(strSplit, startPos);
splitN = pos - startPos;
vecResult.push_back(lineText);
} splitN = strSrc.length() - startPos;
if (splitN > 0)
{
lineText = strSrc.substr(startPos, splitN);
vecResult.push_back(lineText);
} return vecResult.size();
} WininetFtpClient::WininetFtpClient(void)
{ } WininetFtpClient::~WininetFtpClient(void)
{
if (m_hSession)
{
InternetCloseHandle(m_hSession);
} if (m_hConnect)
{
InternetCloseHandle(m_hConnect);
}
} bool WininetFtpClient::ConncetServer(const wstring & strServer, const wstring & strAccount, const wstring & strPswd)
{
m_hSession = InternetOpen(TEXT("UpdDemo"), INTERNET_OPEN_TYPE_PRECONFIG, NULL, NULL, 0);
if (!m_hSession)
{
DWORD error = ::GetLastError();
char stroutput[MAX_PATH] = "";
sprintf(stroutput, "m_hSession is NULL error code %d", error);
::OutputDebugStringA(stroutput); return false;
} m_hConnect = ::InternetConnect(m_hSession, strServer.c_str(), INTERNET_DEFAULT_FTP_PORT,
strAccount.c_str(), strPswd.c_str(), INTERNET_SERVICE_FTP, NULL, NULL);
if (!m_hConnect)
{
DWORD error = ::GetLastError();
char stroutput[MAX_PATH] = "";
sprintf(stroutput, "m_hConnect is NULL error code %d", error);
::OutputDebugStringA(stroutput); return false;
}
return true;
} //fun:send file to ftp sever
//parmeter:srcPath, source file path; ftpPath, ftp server path as root directory
bool WininetFtpClient::PostFile(const wstring & srcPath, const wstring & ftpPath)
{
::OutputDebugString(srcPath.c_str());
if (!m_hSession || !m_hConnect)
{
return false;
} //#define TEST_FTP_API
#ifdef TEST_FTP_API
BOOL bSuccess = FtpSetCurrentDirectory(m_hConnect, L"cdn.xxx.cn/upload/xxx/xxx/test"); //设置当前目录失败,返回异步重叠错误,后来无视这个问题,直接传送文件 //非阻塞直接用
FindSpecificDirectoy(m_strDestDir);
#endif vector <wstring> pathVect;
SplitString(srcPath, L"\\", pathVect);
if (pathVect.empty())
{
::OutputDebugString(L"pathVect is empty");
return false; //为空,路径有问题
}
vector<wstring>::iterator iter = pathVect.end() - 1; //取最后一个元素是exe名称
wstring strExeName(*iter);
strExeName = ftpPath + L"/" + strExeName;
DWORD dwContext = 0;
if (!FtpPutFile(m_hConnect, srcPath.c_str(), strExeName.c_str(), FTP_TRANSFER_TYPE_BINARY, dwContext))
{
DWORD error = GetLastError();
return false;
}
return true;
} bool WininetFtpClient::FindSpecificDirectoy(const wstring & filePath)
{
vector <wstring> pathVect;
SplitString(filePath, L"/", pathVect); WIN32_FIND_DATA findData;
HINTERNET hDirectory = m_hConnect;
vector <wstring>::iterator iter = pathVect.begin();
for(int i = 0; iter != pathVect.end(); iter++, i++)
{ hDirectory = FtpFindFirstFile(hDirectory, iter->c_str(), &findData, 0, 0);
while(InternetFindNextFile(hDirectory, &findData))
{ }
if (!hDirectory)
{
DWORD error = ::GetLastError();
return false;
}
}
return true;
}
main #include "WininetFtpClient.h" int WINAPI _tWinMain(HINSTANCE hInstance, HINSTANCE /*hPrevInstance*/, LPTSTR lpstrCmdLine, int nCmdShow)
{ WininetFtpClient winclient;
if (!winclient.ConncetServer(L"188.169.45.190", L"account", L"password"))
{
return 0;
}
winclient.PostFile(_T("D:\\test.txt"), L"ftpDirectory/"); return 1;
}
代码下载资源:
http://download.csdn.net/detail/sundan308/6487155
使用wininet向FTP服务器发送文件的更多相关文章
- 【FTP】C# System.Net.FtpClient库连接ftp服务器(下载文件)
如果自己单枪匹马写一个连接ftp服务器代码那是相当恐怖的(socket通信),有一个评价较高的dll库可以供我们使用. 那就是System.Net.FtpClient,链接地址:https://net ...
- (4)FTP服务器下载文件
上一篇中,我们提到了怎么从FTP服务器下载文件.现在来具体讲述一下. 首先是路径配置.. 所以此处我们需要一个app.config来设置路径. <?xml version="1.0&q ...
- Unix lrzsz命令 上传本地文件到服务器 / 发送文件到客户端
第三方教程:https://www.jb51.net/article/73690.htm 安装命令: $ yum install lrzsz 本地上传文件到服务器,如果是xshell,直接拖拽文件进入 ...
- 【ABAP系列】SAP ABAP 从FTP服务器读取文件到本地
公众号:SAP Technical 本文作者:matinal 原文出处:http://www.cnblogs.com/SAPmatinal/ 原文链接:[ABAP系列]SAP ABAP 从FTP服务器 ...
- Ubuntu 16.04 安装ftp服务器传输文件
最近在搞深度学习,老师比较宝贝他的服务器,要求我以后负责管理服务器.往后所有要使用服务器的人都必须向我申请账号,然后只允许客户端访问,使用文件传输软件传输文件.像我这样一个linux菜逼,这种要求不是 ...
- linux上创建ftp服务器下载文件///使用AWS服务器作为代理,下载sbt相关的包
最近觉得自己下载有些jar的速度太慢了,就在aws上下好了,然后转到我电脑上来,在aws上开了ftp服务器.结果就倒腾了一上午,作个记录,以便后面查看. 1.安装vsftpd yum -y insta ...
- swift之向ftp服务器传文件
在 mac 上如何使用 xcode, swift 语言开发一个向 ftp 服务器上传文件的工具? 使用的是第三方库 Rebekka,下载地址为:https://github.com/Constanti ...
- c#之向ftp服务器传文件
.Net提供了FtpWebRequest类,代码如下: using System; using System.Collections.Generic; using System.IO; using S ...
- linux命令行模式下对FTP服务器进行文件上传下载
参考源:点击这里查看 1. 连接ftp服务器 格式:ftp [hostname| ip-address]a)在linux命令行下输入: ftp 192.168.1.1 b)服务器询问你用户名和密码 ...
随机推荐
- Nginx模块开发入门(转)
前言 Nginx是当前最流行的HTTP Server之一,根据W3Techs的统计,目前世界排名(根据Alexa)前100万的网站中,Nginx的占有率为6.8%.与Apache相比,Nginx在高并 ...
- 【译】浅谈微软OneNote的自动化测试工具
当我们向人们介绍OneNote的自动化时,有一个问题被相当频繁地提到,担忧我们的自动化框架中UI层面测试偏少. 我不喜欢基于UI的自动化.我知道在市场上有许多的自动化系统都是基于UI的自动化(点击按钮 ...
- poj 3273 Monthly Expense (二分搜索,最小化最大值)
题目:http://poj.org/problem?id=3273 思路:通过定义一个函数bool can(int mid):=划分后最大段和小于等于mid(即划分后所有段和都小于等于mid) 这样我 ...
- COCOS2d-x简易安装步骤
准备工作:1. 下载 cocos2d-x 下载地址:http://cdn.cocos2d-x.org/cocos2d-x-2.2.zip2. 下载 python 2.7.3 下载地址:h ...
- SQL Server 2008 批量插入数据时报错
前几天在SQL Server 2008同步产品数据时,总是提示二进制文本被截断的错误,但是经过检查发现数据都符合格式要求. 百思不得其解,单独插入一条条数据则可以插入,但是批量导入则报错. 批量导入代 ...
- 04737_C++程序设计_第8章_多态性和虚函数
例8.1 分析下面程序的输出结果. 例8.2 分别使用指针和引用的display函数. #include <iostream> using namespace std; const dou ...
- cocos2d-x创建场景
今天开始学习Cocos2d-x,使用的版本是2.1.4,这个版本比较老,对应的参考资料也比较齐全. 在mac/xcode环境下,代码是写在Classes文件夹下的,和iOS应用类似,程序从AppDel ...
- 自己定义View常处理的回调函数
onFinishInflate() 当View中全部的子控件均被映射成xml后触发 onMeasure(int, int) 确定全部子元素的大小 onLayout(boolean, int, int, ...
- 通过pull解析器操作安卓的xml
通过pull解析器操作安卓的xml 例子定义了一个javabean用于存放上面解析出来的xml内容, 这个javabean为Person,代码请见本页下面备注: =================== ...
- SQL2008缩小日志脚本
以下为SQL2008 缩小日志文件的脚本,在SQL Server Management Studio中打开数据库,将脚本里的数据库名称替换成需要缩小日志的库名称,然后 运行以下脚本. USE WSS_ ...