.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. 自绘Tab控件

    自绘tab按钮效果图如下: 使用例子: MyTabControl *tabControl = NULL; tabControl = new MyTabControl();tabControl-> ...

  2. Android中ViewStub组件使用

    1. 概述: ViewStub组件和<include>标签的作用类似,主要是为了提高布局的重用性,及布局的模块化.它们之间最大的差别是,ViewStub中的布局不会随着它所在布局的渲染而渲 ...

  3. Day1_PHP快速入门

    本人知识背景:行业软件C/C++开发两年经验,了解PHP, 所以学习日志偏向记录PHP相对于C的特性 测试环境:EasyPHP13.1 Day 1 学习时间:3小时 1. HTML触发PHP HTML ...

  4. 前端web应用组件化(一) 徐飞

    https://github.com/xufei/blog/issues/6 Web应用的组件化(一) 基本思路 1. 为什么要做组件化? 无论前端也好,后端也好,都是整个软件体系的一部分.软件产品也 ...

  5. spring NotWritablePropertyException异常

    Caused by: org.springframework.beans.NotWritablePropertyException: Invalid property 'userDao' of bea ...

  6. 将图片文件以byte的形式从导数据库中

    byte[] FileByteArray = new byte[FileLength];  //图象文件临时储存Byte数组                 //Stream StreamObject ...

  7. android之PackageManager简介

    PackageManager相关 本类API是对所有基于加载信息的数据结构的封装,包括以下功能: 安装,卸载应用查询permission相关信息 查询Application相关信息(applicati ...

  8. spring mvc 简单搭建

    文中用的框架版本:spring 3,hibernate 3,没有的,自己上网下. web.xml配置: </load-on-startup>     </servlet>    ...

  9. C++ try catch 捕获空指针异常,数组越界异常

    #include <exception> #include <iostream> using namespace std; /************************* ...

  10. 基础知识——Cocos2d-x学习历程(三)

    1.场景与流程控制 我们把一些内容相对不变的游戏元素集合称作场景(scene),把游戏在场景之间切换的过程叫做流程控制(flow control). 在Cocos2d-x中,场景的实现是Scene. ...