16,472
社区成员
发帖
与我相关
我的任务
分享
bool CMTView::Upload_FTP_Daily_Record_Excel_File(const wchar_t * wcp_Excel_File_Path)
{
//
BOOL bFlag;
CInternetSession * p_InternetSession = NULL;
CFtpConnection * p_FtpConnection = NULL;
CString cstrCurrDir, cstrNewCurrDir, csFile_Name;
//
try
{
//提取文件名
if (this->m_cla_GFC.Get_FileName_From_FilePath(wcp_Excel_File_Path, true, &csFile_Name) == false)
{
this->Show_REdit_Info(_T("[INFO]Get Log File Name failed"), true);
throw(1);
}
//connect to ftp
try
{
//创建一个CInternetSession对象
p_InternetSession = new CInternetSession();
//创建CFtpConnection对象并返回一个指向该对象的指针
p_FtpConnection = p_InternetSession->GetFtpConnection(
this->m_csFTP_LOG_Server,
this->m_csFTP_LOG_User_Name,
this->m_csFTP_LOG_Password,
this->m_iFTP_LOG_Port, true);
}
catch (CInternetException * pEx)//error:can not connect to specific ftp
{
//fail
this->Show_REdit_Info(_T("[INFO]Connect to FTP failed"), true);
//
if (p_FtpConnection != NULL)
{
delete p_FtpConnection;
p_FtpConnection = NULL;
}
if (p_InternetSession != NULL)
{
delete p_InternetSession;
p_InternetSession = NULL;
}
//建立FTP连接失败
throw(2);
}
//设置FTP路径"TestLog"
cstrNewCurrDir.Format(_T("//TestLog/"));
//
bFlag = p_FtpConnection->SetCurrentDirectory(cstrNewCurrDir);
//
if (bFlag != TRUE)//set current directory error
{
//this->Show_REdit_Msg(_T("ERROR, SetCurrentDirectory( TestLog ) is fail!"), true);
//fail
this->Show_REdit_Info(_T("[INFO]Set FTP TestLog Directory failed"), true);
throw(3);
}
//设置当前目录SetCurrentDirectory,失败就创建目录CreateDirectory,还失败就关闭ftp返回错误
//设置FTP路径"年份"
cstrNewCurrDir.Format(_T("//TestLog/%s/"), this->m_csMO_Create_Year);
//
bFlag = p_FtpConnection->SetCurrentDirectory(cstrNewCurrDir);
//
if (bFlag != TRUE)//set current directory error
{
//创建年份路径
//create directory on ftp
bFlag = p_FtpConnection->CreateDirectory(cstrNewCurrDir);
//
if (bFlag == TRUE)//create directory on ftp success
{
//设置FTP路径"年份"
bFlag = p_FtpConnection->SetCurrentDirectory(cstrNewCurrDir);
//
if (bFlag != TRUE)//set current directory error
{
this->Show_REdit_Info(_T("[INFO]Set FTP Year Directory failed"), true);
throw(4);
}
}
else
{
this->Show_REdit_Info(_T("[INFO]Create FTP Year Directory failed"), true);
//fail
throw(5);
}
}
//设置当前目录SetCurrentDirectory,失败就创建目录CreateDirectory,还失败就关闭ftp返回错误
//设置FTP路径"MO"
cstrNewCurrDir.Format(_T("//TestLog/%s/%s/"), this->m_csMO_Create_Year, this->m_dlg_Config.m_csMO_Name);
//
bFlag = p_FtpConnection->SetCurrentDirectory(cstrNewCurrDir);
//
if (bFlag != TRUE)//set current directory error
{
//创建MO路径
//create directory on ftp
bFlag = p_FtpConnection->CreateDirectory(cstrNewCurrDir);
//
if (bFlag == TRUE)//create directory on ftp success
{
//设置FTP路径"MO"
bFlag = p_FtpConnection->SetCurrentDirectory(cstrNewCurrDir);
//
if (bFlag != TRUE)//set current directory error
{
this->Show_REdit_Info(_T("[INFO]Set FTP MO Directory failed"), true);
throw(4);
}
}
else
{
this->Show_REdit_Info(_T("[INFO]Create FTP MO Directory failed"), true);
//fail
throw(5);
}
}
//设置FTP路径"Write"
cstrNewCurrDir.Format(_T("//TestLog/%s/%s/Write/"), this->m_csMO_Create_Year, this->m_dlg_Config.m_csMO_Name);
//
bFlag = p_FtpConnection->SetCurrentDirectory(cstrNewCurrDir);
//
if (bFlag != TRUE)//set current directory error
{
//创建RF_FT路径
//create directory on ftp
bFlag = p_FtpConnection->CreateDirectory(cstrNewCurrDir);
//
if (bFlag == TRUE)//create directory on ftp success
{
//设置FTP路径"RF_FT"
bFlag = p_FtpConnection->SetCurrentDirectory(cstrNewCurrDir);
//
if (bFlag != TRUE)//set current directory error
{
this->Show_REdit_Info(_T("[INFO]Set FTP Write Directory failed"), true);
throw(6);
}
}
else
{
this->Show_REdit_Info(_T("[INFO]Create FTP Write Directory failed"), true);
//fail
throw(7);
}
}
//upload file to ftp
bFlag = p_FtpConnection->PutFile(wcp_Excel_File_Path, csFile_Name);
if (bFlag != TRUE) //upload file fail
{
this->Show_REdit_Info(_T("[INFO]Upload log file to ftp failed"), true);
//fail
throw(8);
}
//
cstrNewCurrDir.Format(_T("[PASS]Upload file(%s) to ftp"), csFile_Name);
this->Show_REdit_Info(cstrNewCurrDir, true);
//断开FTP
if (p_FtpConnection != NULL)
{
p_FtpConnection->Close();
//delete
delete p_FtpConnection;
p_FtpConnection = NULL;
}
if (p_InternetSession != NULL)
{
delete p_InternetSession;
p_InternetSession = NULL;
}
}
catch (int iErr)
{
//断开FTP
if (p_FtpConnection != NULL)
{
p_FtpConnection->Close();
//delete
delete p_FtpConnection;
p_FtpConnection = NULL;
}
if (p_InternetSession != NULL)
{
delete p_InternetSession;
p_InternetSession = NULL;
}
//fail
return false;
}
//pass
return true;
}