.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. 使用TWebBrowser组件保存网页为html和mht文件 收藏

    一.保存为HTML文件 uses ActiveX;...procedure WB_SaveAs_HTML(WB : TWebBrowser; const FileName : string) ;var ...

  2. [Drools]JAVA规则引擎 -- Drools

    Drools是一个基于Java的规则引擎,开源的,可以将复杂多变的规则从硬编码中解放出来,以规则脚本的形式存放在文件中,使得规则的变更不需要修正代码重启机器就可以立即在线上环境生效. 本文所使用的de ...

  3. C++实现Http Post请求

    参考资料: http://apps.hi.baidu.com/share/detail/39003388 http://blog.csdn.net/yc0188/article/details/474 ...

  4. 杂记之activity之间的跳转

    代码结构图 manifest.xml <?xml version="1.0" encoding="utf-8"?> <manifest xml ...

  5. ToolStripMenuItem控件实现DatagridView行的上下移

    /*--------------行上移------------------*/ 1 private void 上移ToolStripMenuItem_Click(object sender, Even ...

  6. JavaScript之JS实现动画效果

    在前面的随笔中介绍了如何用DOM技术修改文档的央样式信息,用JavaScript添加样式信息可以节约我们的时间和精力,但总的来说,CSS仍是完成这类任务的最佳工具.但是有一个应用领域是目前的CSS无能 ...

  7. WebPart设置杂项

    CS写法:                    } 后台写法:  public D_ZoneLimitView WebPart { get; set; }

  8. Samba在CentOS下的图形化界面的安装

    第一步:构建yum仓库(在此用的是北交大的yum仓库) 打开目录/etc/yum.repos.d下的CentOS-Base.repo文件,此处是我自己建的yum仓库,修改里面的链接地址为北交大的镜像的 ...

  9. iOS RTMP 视频直播开发笔记(1) – 采集摄像头图像

    1. 采集硬件(摄像头)视频图像 这里简单说下 iOS 的摄像头采集. 首先初始化AVCaptureSession,说到Session,有没有人想到AVAudioSession呢? // 初始化 AV ...

  10. java堆溢出的小栗子

    package com.xiaoysec.test; import java.util.ArrayList; import java.util.List; /** *VM Args:-verbose: ...