#include "stdafx.h"
#include "FtpManage.h"
#include <xlocbuf>
#include <codecvt>
FtpManage::FtpManage():m_pInetSession(NULL),m_pFtpConnection(NULL)
{
InitFtpSetting();
}
FtpManage::~FtpManage()
{
}
void FtpManage::InitFtpSetting()
{
m_strFTPServer = CUtility::getValueByKey(INI_SETTING_FTPSETTING, INI_NODE_FTPSERVER);
m_strFTPPassive = CUtility::getValueByKey(INI_SETTING_FTPSETTING, INI_NODE_FTPPASSIVE);
m_strFTPRoot = CUtility::getValueByKey(INI_SETTING_FTPSETTING, INI_NODE_FTPROOT);
m_strFTPUser = CUtility::getValueByKey(INI_SETTING_FTPSETTING, INI_NODE_FTPUSER);
m_strFTPPassWd = CUtility::getValueByKey(INI_SETTING_FTPSETTING, INI_NODE_FTPPASSWD);
}
std::vector<CString> FtpManage::getSplitStringVector(CString strFtpPath, LPCTSTR param2 /*= _T("/")*/)
{
vector<CString> retVec;
retVec.clear();
int nFind = strFtpPath.Replace(_T("/"), _T("/"));
if (nFind <= 0)
{
retVec.push_back(_T("/"));
return retVec;
}
for (int i=0; i<=nFind; i++)
{
retVec.push_back(CUtility::SubString(strFtpPath, _T("/"), i) + _T("/"));
}
return retVec;
}
int FtpManage::GetFileFromFtp(/*CString strFtpPath, */CString strFtpFileName, CString strDwgSavePath)
{
//if (SetCurrentFtpDir(strFtpPath)<0)
//{
// return -1;
//}
CFtpFileFind findFile(m_pFtpConnection);
if (findFile.FindFile(strFtpFileName, INTERNET_FLAG_DONT_CACHE))
{
if (!m_pFtpConnection->GetFile(strFtpFileName,strDwgSavePath, FALSE, FILE_ATTRIBUTE_NORMAL, FTP_TRANSFER_TYPE_BINARY, 1))
{
DWORD dw = GetLastError();
CString sError;
AfxMessageBox(_T("ftp getfile error :%d"), dw);
return -3;
}
}
else
{
return -2;
}
return 0;
}
int FtpManage::ConnectFtp()
{
CWaitCursor wait;
CString m_sDomainName(m_strFTPServer);
CString m_ftpUser(m_strFTPUser);
CString m_ftpPassword(m_strFTPPassWd);
m_pFtpConnection = NULL;
m_pInetSession = new CInternetSession(_T("ESAPP"), 1, PRE_CONFIG_INTERNET_ACCESS, NULL, NULL, INTERNET_FLAG_DONT_CACHE);
if (!m_pInetSession)
{
return -1;
}
CString strFtpSite = m_sDomainName;
CString strServerName;
CString strObject;
INTERNET_PORT nPort;
DWORD dwServiceType;
//检查URL是否正确
if (!AfxParseURL(strFtpSite, dwServiceType, strServerName, strObject, nPort) || dwServiceType == AFX_INET_SERVICE_UNK)
{
CString strFtpURL = _T("ftp://");
strFtpURL += strFtpSite;
if (!AfxParseURL(strFtpURL, dwServiceType, strServerName, strObject, nPort))
{
return -2;
}
}
if ((dwServiceType == INTERNET_SERVICE_FTP) && !strServerName.IsEmpty())
{
try
{
//AfxMessageBox(strServerName + _T("\r") + m_ftpUser + _T("\r") + m_ftpPassword);
if (m_strFTPPassive == _T("TRUE"))
{
m_pFtpConnection = m_pInetSession->GetFtpConnection(strServerName, m_ftpUser, m_ftpPassword, nPort, TRUE);
}
else
{
m_pFtpConnection = m_pInetSession->GetFtpConnection(strServerName, m_ftpUser, m_ftpPassword, nPort, FALSE);
}
}
catch (CInternetException* pEx)
{
CString strInteError = _T("");
TCHAR szErr[1024];
if (pEx->GetErrorMessage(szErr, 1024))
{
strInteError.Format(_T("%s"), szErr);
pEx->Delete();
}
AfxMessageBox(strInteError);
return -3;
}
}
return 0;
}
int FtpManage::PutFileToFtp(CString strLocalFilePath, CString strFtpPath)
{
CString sFileName;
int nFind = strLocalFilePath.ReverseFind(_T('\\'));
sFileName = strLocalFilePath.Mid(nFind+1);
return PutFileToFtpEx(strLocalFilePath, strFtpPath, sFileName);
}
int FtpManage::PutFileToFtpEx(CString strLocalFilePath, CString strFtpPath, CString strFtpFileName)
{
int nRes = SetCurrentFtpDir(strFtpPath);
if (nRes!=0)
{
return -1;
}
CFtpFileFind findFile(m_pFtpConnection);
if (findFile.FindFile(strFtpFileName,INTERNET_FLAG_DONT_CACHE))
{
m_pFtpConnection->Remove(strFtpFileName);
}
if (!m_pFtpConnection->PutFile(strLocalFilePath,strFtpFileName))
{
DWORD dw = GetLastError();
int nError = (int)dw;
CString strInterError;
strInterError.Format(_T("%d"), nError);
AfxMessageBox(strInterError);
return -2;
}
return 0;
}
int FtpManage::SetCurrentFtpDir(CString strFtpPath)
{
if (m_pFtpConnection==NULL)
{
return -1;
}
//会存在多级 然后一次设置下去 如果失败了 就返回问题
strFtpPath.Replace(_T("\\"), _T("/"));
vector<CString> vecCatalogue;
//gMyString.Split(strFtpPath, _T("/"), vecCatalogue);
vecCatalogue = getSplitStringVector(strFtpPath, _T("/"));
for (int i=0;i<vecCatalogue.size();i++)
{
CString sTempCatalogue = vecCatalogue[i];
if (sTempCatalogue==_T(""))
{
continue;
}
if (!m_pFtpConnection->SetCurrentDirectory(sTempCatalogue))
{
if (!m_pFtpConnection->CreateDirectory(sTempCatalogue))
{
return -2;
}
if (!m_pFtpConnection->SetCurrentDirectory(sTempCatalogue))
{
return -3;
}
}
}
return 0;
}
int FtpManage::CloseFtpConnect()
{
if (m_pFtpConnection!=NULL)
{
m_pFtpConnection->Close();
delete m_pFtpConnection;
m_pFtpConnection = NULL;
}
if (m_pInetSession!=NULL)
{
m_pInetSession->Close();
delete m_pInetSession;
m_pInetSession = NULL;
}
return 0;
}
wchar_t * ANSIToUnicode(const char* str)
{
int textlen;
wchar_t * result;
textlen = MultiByteToWideChar(CP_ACP, 0, str, -1, NULL, 0);
result = (wchar_t *)malloc((textlen + 1) * sizeof(wchar_t));
memset(result, 0, (textlen + 1) * sizeof(wchar_t));
MultiByteToWideChar(CP_ACP, 0, str, -1, (LPWSTR)result, textlen);
return result;
}
//获取中文文件名乱码
std::vector<CString> FtpManage::getAllFileFromFtpServer()
{
vector<CString> tmpVec;
tmpVec.clear();
if (SetCurrentFtpDir(m_strFTPServer) < 0)
{
return tmpVec;
}
try
{
// use a file find object to enumerate files
CFtpFileFind findFile(m_pFtpConnection);
CString strName, strDirectory;
m_pFtpConnection->GetCurrentDirectory(strDirectory);
// start looping
BOOL bWorking = findFile.FindFile(_T("*"));
//while (bWorking)
//{
// bWorking = findFile.FindNextFile();
// tmpVec.push_back(findFile.GetFileURL());
// //_tprintf_s(_T("%s\n"), (LPCTSTR)findFile.GetFileURL());
//}
BOOL bFind = findFile.FindFile(_T("/"), INTERNET_FLAG_EXISTING_CONNECT);
bool flag = false;
while (bFind)
{
bFind = findFile.FindNextFile();
if (findFile.IsDots())
{
continue;
}
CString remoteFileName = findFile.GetFileName();
// 转换为 UTF-8 编码
std::string fileNameUtf8 = std::wstring_convert<std::codecvt_utf8<wchar_t>>().to_bytes(findFile.GetFileName());
CString strName = fileNameUtf8.c_str();
/* CString remoteFilePath = remoteDir + remoteFileName;
CString localFilePath = localDir + remoteFileName;*/
//flag = 1为获取目录下的子文件,flag = 0为获取当前文件夹下的所有子文件。
if (flag)
{
if (findFile.IsDirectory())
{
// 如果是目录,递归下载其中的文件和子目录
//CreateDirectory(localFilePath, nullptr);
strDirectory = strDirectory + _T("/") + strName;
tmpVec.push_back(strName);
}
}
else
{
tmpVec.push_back(strName);
}
}
findFile.Close();
}
catch (CInternetException* pEx)
{
TCHAR sz[1024];
pEx->GetErrorMessage(sz, 1024);
_tprintf_s(_T("ERROR! %s\n"), sz);
pEx->Delete();
}
return tmpVec;
}