使用wininet向FTP服务器发送文件
来源:互联网 发布:淘宝卖家冻结资金 编辑:程序博客网 时间:2024/05/16 23:01
.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 directorybool 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服务器发送文件
- 使用WinInet向 ASP.Net Web服务器 多线程上传文件
- 使用WinInet向 ASP.Net Web服务器 多线程上传文件
- 使用wininet上传FTP文件
- 使用Wininet发送短信
- 使用Wininet发送短信
- 使用Wininet发送短信
- 使用Wininet发送短信
- 使用wininet进行FTP文件上传和下载
- iOS中使用POST向服务器发送图片文件
- 使用wininet枚举FTP目录
- Java中使用FTPClient向FTP服务器上传文件、下载文件
- 利用TJSONArray向REST服务器发送文件
- ActiveX Control 向Https服务器发送文件
- 利用TJSONArray向REST服务器发送文件 .
- C# WinForm向服务器发送文件
- HttpClient 向tomcat服务器发送文件
- 利用WinInet库和STL获取计算机硬盘中的文件并上传到指定FTP服务器
- 使用PL/SQL导出数据库表中的数据为excel格式
- 搭建hypervisor类型为VMWare的cloudstack环境
- iNotes 9.0: 全文搜索功能改进
- 传感器
- Linux文件查找命令find,xargs详述
- 使用wininet向FTP服务器发送文件
- Linux send与recv函数详解
- 一图胜千言,图解Hadoop分布式文件系统HDFS
- 音频压缩工具——Speex的使用(Android&iOS语音录音技术预演)
- ios 使用gcd 显示倒计时
- 关于回文算法
- android布局属性详解
- 一些做的过的零散的题目(poj)
- Hibernate直接生成MySQL数据库