.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服务器发送文件的更多相关文章

  1. 【FTP】C# System.Net.FtpClient库连接ftp服务器(下载文件)

    如果自己单枪匹马写一个连接ftp服务器代码那是相当恐怖的(socket通信),有一个评价较高的dll库可以供我们使用. 那就是System.Net.FtpClient,链接地址:https://net ...

  2. (4)FTP服务器下载文件

    上一篇中,我们提到了怎么从FTP服务器下载文件.现在来具体讲述一下. 首先是路径配置.. 所以此处我们需要一个app.config来设置路径. <?xml version="1.0&q ...

  3. Unix lrzsz命令 上传本地文件到服务器 / 发送文件到客户端

    第三方教程:https://www.jb51.net/article/73690.htm 安装命令: $ yum install lrzsz 本地上传文件到服务器,如果是xshell,直接拖拽文件进入 ...

  4. 【ABAP系列】SAP ABAP 从FTP服务器读取文件到本地

    公众号:SAP Technical 本文作者:matinal 原文出处:http://www.cnblogs.com/SAPmatinal/ 原文链接:[ABAP系列]SAP ABAP 从FTP服务器 ...

  5. Ubuntu 16.04 安装ftp服务器传输文件

    最近在搞深度学习,老师比较宝贝他的服务器,要求我以后负责管理服务器.往后所有要使用服务器的人都必须向我申请账号,然后只允许客户端访问,使用文件传输软件传输文件.像我这样一个linux菜逼,这种要求不是 ...

  6. linux上创建ftp服务器下载文件///使用AWS服务器作为代理,下载sbt相关的包

    最近觉得自己下载有些jar的速度太慢了,就在aws上下好了,然后转到我电脑上来,在aws上开了ftp服务器.结果就倒腾了一上午,作个记录,以便后面查看. 1.安装vsftpd yum -y insta ...

  7. swift之向ftp服务器传文件

    在 mac 上如何使用 xcode, swift 语言开发一个向 ftp 服务器上传文件的工具? 使用的是第三方库 Rebekka,下载地址为:https://github.com/Constanti ...

  8. c#之向ftp服务器传文件

    .Net提供了FtpWebRequest类,代码如下: using System; using System.Collections.Generic; using System.IO; using S ...

  9. linux命令行模式下对FTP服务器进行文件上传下载

    参考源:点击这里查看   1. 连接ftp服务器 格式:ftp [hostname| ip-address]a)在linux命令行下输入: ftp 192.168.1.1 b)服务器询问你用户名和密码 ...

随机推荐

  1. 传智播客C/C++学院年薪24-50万招聘C/C++讲师

    C/C++技术讲师 6名 (北京,年薪:24-50万) 传智播客C/C++课程培训体系如下: 1.C语言,世界五百强C语言面试训练 2.C++语言,世界五百强C++语言面试训练 3.数据结构与算法,世 ...

  2. paip.提升用户体验---论文本编辑器的色彩方案

    paip.提升用户体验---论文本编辑器的色彩方案 作者Attilax ,  EMAIL:1466519819@qq.com  来源:attilax的专栏 地址:http://blog.csdn.ne ...

  3. SQL Server 2008 批量插入数据时报错

    前几天在SQL Server 2008同步产品数据时,总是提示二进制文本被截断的错误,但是经过检查发现数据都符合格式要求. 百思不得其解,单独插入一条条数据则可以插入,但是批量导入则报错. 批量导入代 ...

  4. C++_const

    //const在C不可以初始化数组 //const在C++可以用来初始化数组 #include <iostream> using namespace std; void main() { ...

  5. 快速定位MS Sql Server 数据库死锁进程

    最近在做一个大型项目,由于数据设计采用离散型数据库设计,以方便需求变更及用户自定义流程要素,因为要素用户自定义,数据完整性靠代码约束变得不太现实,只能依靠表间关系来约束,结果因此导致数据的操作经常产生 ...

  6. Couldn't load libPassword from loader:NDK开发中C文件编译成cpu对应的so类库时,找不到类库报错的原因之一

    LogCat输出: 03-03 12:42:32.665: E/AndroidRuntime(32432): FATAL EXCEPTION: main03-03 12:42:32.665: E/An ...

  7. ORA-03113: 通信通道的文件结尾 进程 ID: 764 会话 ID: 125 序列号: 5

    昨天因为导入很久数据,最后一看是因为数据文件不够,后来就关机了.现在,开启数据库,总是报“ORA-03113: 通信通道的文件结尾” SQL> conn /as sysdba; 已连接到空闲例程 ...

  8. Xcode工程使用CocoaPods管理第三方库新建工程时出现异常

    Xcode工程使用CocoaPods管理第三方库新建工程时出现异常 Xcode工程使用CocoaPods管理第三方库新建工程时出现错误工程使用CocoaPods管理第三方库,在新的目录update版本 ...

  9. javaTemplates-学习笔记二

    配置PlayFramework环境 下载jar包[Play with Activator],这一步有点晕是JAVA程序员CMD执行的步骤;运行了jar包下载了很多配置文件什么的,有的资源没有VPN链接 ...

  10. 应该知道的Linux技巧

    作者:陈皓(花名:钻风) 这篇文章来源于Quroa的一个问答<What are some time-saving tips that every Linux user should know?& ...