#include <WinInet.h>
#pragma comment(lib, "WinInet.lib")
bool FTPUpload(const string& strFtpHost, int nPort, const string &strUser, const string &strPwd,
const string& strLocalFile, const string& strFtpFile, string &strMsg)
{
BOOL dRes,pRes;
HINTERNET hInternet;
HINTERNET hConnect;
const char *pFtpHost = strFtpHost.c_str();
if (nPort==0)
{
nPort = INTERNET_DEFAULT_FTP_PORT;
}
const char *pFtpUser = strUser.c_str();
const char *pFtpPwd = strPwd.c_str();
const char *pLocalFile = strLocalFile.c_str();
const char *pFtpFile = strFtpFile.c_str();
hInternet = InternetOpen("DataClient", INTERNET_OPEN_TYPE_DIRECT,NULL, NULL, INTERNET_FLAG_NO_CACHE_WRITE);
if ( NULL == hInternet )
{
//printf("InternetOpen Error:%d ", GetLastError() );
strMsg = "InternetOpen Error";
return false;
}
hConnect = InternetConnect(hInternet, pFtpHost, nPort, pFtpUser, pFtpPwd, INTERNET_SERVICE_FTP,
INTERNET_FLAG_EXISTING_CONNECT || INTERNET_FLAG_PASSIVE,0 );
if ( NULL == hInternet )
{
//printf( "InternetConnect Error:%d ", GetLastError() );
strMsg = "InternetConnect Error";
InternetCloseHandle(hInternet);
return false;
}
//dRes = FtpGetFile(hConnect, "./usera/123.txt", "D:\\ftp123.txt", FALSE, FILE_ATTRIBUTE_ARCHIVE, FTP_TRANSFER_TYPE_UNKNOWN, 0);
//if ( dRes == 0 )
//{
//
printf( "FtpGetFile Error: ", GetLastError() );
//}
pRes = FtpPutFile(hConnect,pLocalFile,pFtpFile,FTP_TRANSFER_TYPE_BINARY,0);
InternetCloseHandle(hConnect);
InternetCloseHandle(hInternet);
if(pRes==0)
{
//printf("上传文件失败! ");
strMsg = "FtpPutFile Error";
return false;
}
strMsg="上次成功";
return true;
}