.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