.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. 紫薇~还记得大明湖畔的HTML5智力拼图吗?

    曲线谜团是非常有趣的HTML5智力游戏,据说超过多少分会有惊喜,游戏简单易操作,偶尔抛弃那种杀死脑细胞的大型游戏,玩玩这种简单经典的益智小游戏,放松放松,也是不错的选择嘛-将游戏 通过 统一开发环境( ...

  2. org/apache/commons/pool/impl/GenericObjectPool异常的解决办法

    org/apache/commons/pool/impl/GenericObjectPool异常的解决办法 webwork+spring+hibernate框架的集成, 一启动Tomcat服务器就出了 ...

  3. kettle中调用java类

    kettle中调用java类 有时须要在kettle调用java类,如:验证.查询或自己定义加密等.有时甚至连主要的数据訪问都不那么简单,如获取一个存储文件或使用一个数据库连接,某些数据源可能封装在应 ...

  4. HTML之学习笔记(八)表格

    Html的表格使用table标签.table标签含有tr(table row)子标签,tr又含有th(table head)和td(table data)子标签这样的嵌套结构 代码演示 <tab ...

  5. JavaScript之面向对象的概念,对象属性和对象属性的特性简介

    一.大家都知道,面向对象语言有一个标志,那就是他们都有类的概念,通过类我们可以创建任意多个具有相同属性和方法的对象.但ECMAScript(指定JavaScript标准的机构,也就是说JavaScri ...

  6. Activity中异步操作showDialog异常解决方法:判断Ay是否结束

    Android – Displaying Dialogs From Background Threads 判断一下Activity是否在finishing就好了,否则万一Activity销毁了,这个D ...

  7. pycharm中添加扩展工具pylint

    今天调试了好几个小时,想吧pylint集成到pycharm中去,从网上找了个宝贝帖 子,但是不好用,原因是作者写的脚本是检查工程和模块的,而我的是单独检查一个文件,当然前者肯定会在项目后期用的.所以就 ...

  8. 页面加载时,页面中DIV随之滑动出来;去掉页面滚动条

    <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8&quo ...

  9. java 正则表达式抽取

    package com.achun.test; import java.util.regex.Matcher;import java.util.regex.Pattern; public class ...

  10. IOS 特定于设备的开发:获取和使用设备姿势(通过手机方向控制3d物体显示)

    利用设备的机载陀螺仪可以实现,当你旋转手机屏幕时,里面的画面不会随着视图更新而移动,以平衡物理运动. 下面例子利用少量简单的几何变换执行该操作.他建立一个运动管理器,订阅设备运动更新,然后基于运动管理 ...