⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 mainfrm.cpp

📁 从FTP下载文件下载到本地
💻 CPP
📖 第 1 页 / 共 4 页
字号:
}



//连接ftp服务器
BOOL CMainFrame::OnConnectFTP() 
{   
	CString errormessage;
	m_pInetSession=new CInternetSession(AfxGetAppName(),1,PRE_CONFIG_INTERNET_ACCESS);
	CString  strInfo11=_T("正在連接FTP服務器...");
	this->SendMessage( WM_STATUSMSG, (WPARAM)&strInfo11 );
	SetDoc(strInfo11);
	try 
	{    int  nPort=_wtoi(strFtpPort);
		//新建连接对象
		m_pFtpConnection=m_pInetSession->GetFtpConnection(strFtpServer,strFtpUser,strFtpPass,nPort);
	
	}  
	catch(CInternetException *pEx) 
	{
		//获取错误
		TCHAR szError[1024];
		if(pEx->GetErrorMessage(szError,1024))
		{
		    errormessage.Format(_T("連接FTP服务器%s"),szError);
            this->SendMessage( WM_STATUSMSG, (WPARAM)&errormessage );
			SetDoc(errormessage);
		}
		else 
		{	
			errormessage.Format(_T("連接FTP服务器有一個未知錯誤!"));
			this->SendMessage( WM_STATUSMSG, (WPARAM)&errormessage );
			SetDoc(errormessage);
		}
		pEx->Delete();
		m_pFtpConnection=NULL;		
		return  FALSE;
	}
	return  TRUE;
}


//下载单个文件
BOOL CMainFrame::DownFile()
{
      	//格式化文件名
		CString strLocalFile,strRemoteFile;	
		strLocalFile.Format(_T("%s\\%s"),strLocalDir,m_FileName);
		strRemoteFile.Format(_T("%s/%s"),m_strFtpPath,m_FileName);    
		//下载
		if(m_pFtpConnection->GetFile(strRemoteFile,strLocalFile,FALSE))
		{
			//CString strMsg;
			//int nError=GetLastError();
			//strMsg.Format(_T("下载文件%s成功!"),m_FileName);
			return   TRUE;
		}
		return  FALSE;
}


void CMainFrame::OnLocalSearch() 
{   
	// 字符串 szFilename 表示要查找的文件名    
	wcscpy(szFilename,m_FileName);
	wcscpy(szDir,strLocalDir); 
	_wchdir(szDir); // 进入要查找的路径(也可为某一具体的目录) 	
	// 查找文件, 如果查到则显示文件的路径全 
	Search_Directory(szFilename);	  		 
}

void  CMainFrame::ReSetData()
{
   	DWORD  nlength;
	nlength=50;
	LPTSTR strServerIp,strDataBase,strUserID,strUserPassword;
	strServerIp=new TCHAR[50];
	strDataBase=new TCHAR[50];
	strUserID=new TCHAR[50];
	strUserPassword=new TCHAR[50];
	
	GetPrivateProfileString(_T("SQLSERVER"),_T("ServerIP"),_T("0"),strServerIp,nlength,szConfig);
	GetPrivateProfileString(_T("SQLSERVER"),_T("DataBase"),_T("0"),strDataBase,nlength,szConfig);
	GetPrivateProfileString(_T("SQLSERVER"),_T("USERID"),_T("0"),strUserID,nlength,szConfig);
	GetPrivateProfileString(_T("SQLSERVER"),_T("PASSWORD"),_T("0"),strUserPassword,nlength,szConfig);
	//保存SQL数据库信息
	m_DataSource =strServerIp;
	m_Initial_Catalog = strDataBase;
	m_UserID = strUserID;
	m_Password = strUserPassword;
	
	
	
	///初始化文件设置内容
	LPTSTR  pAutoRun,pReadOne,pFileSource,pLocalDir,pFTPDir,pFileName,pExportFile;
	pAutoRun=new TCHAR[50];
	pReadOne=new TCHAR[50];
	pFileSource=new TCHAR[50];
    pLocalDir=new TCHAR[50];
	pFTPDir=new TCHAR[50];
	pFileName=new TCHAR[50];
	pExportFile=new TCHAR[50];
	GetPrivateProfileString(_T("FileSetting"),_T("Auto"),_T("0"),pAutoRun,nlength,szConfig);
	GetPrivateProfileString(_T("FileSetting"),_T("ReadOne"),_T("0"),pReadOne,nlength,szConfig);
	GetPrivateProfileString(_T("FileSetting"),_T("FileSource"),_T("0"),pFileSource,nlength,szConfig);
	GetPrivateProfileString(_T("FileSetting"),_T("LocalDir"),_T("0"),pLocalDir,nlength,szConfig);
	GetPrivateProfileString(_T("FileSetting"),_T("FTPDir"),_T("0"),pFTPDir,nlength,szConfig);
	GetPrivateProfileString(_T("FileSetting"),_T("FileName"),_T("0"),pFileName,nlength,szConfig);
	GetPrivateProfileString(_T("FileSetting"),_T("ExportFile"),_T("0"),pExportFile,nlength,szConfig);
	CString  strAtuo,strSource,strReadOne,strFileSource;
    strAtuo.Format(_T("%s"),pAutoRun);
    strReadOne.Format(_T("%s"),pReadOne);
	strSource.Format(_T("%s"),pFileSource);
	m_LocalDir.Format(_T("%s"),pLocalDir);
	m_strFTPDir.Format(_T("%s"),pFTPDir);
	m_FileName.Format(_T("%s"),pFileName);
	m_strExportFile.Format(_T("%s"),pExportFile);
	if (strReadOne==_T("YES"))
	{
		bReadHeader=TRUE;
		
	}
	if (strReadOne==_T("NO"))
	{
		bReadHeader=FALSE;
		
	}
	if (strSource==_T("FTP"))
	{
		bFileSource=TRUE;
		
	}
	if (strSource==_T("DEFINE"))
	{
		bFileSource=FALSE;
	}
	
	if (strAtuo==_T("YES"))
	{
		bAutoRun=TRUE;
		//开启自动运行
        AutoRun( );

		
	}
    else// (strAtuo=="NO")
	{
		bAutoRun=FALSE;
		//删除自动运行
		CloseAutoRun();
		
	}
	/////////////////////////////////////////////////////////////////////////////////////////
	//读取FTP设置
	LPTSTR pFtpServer,pFtpUser,pFtpPass,pFtpPort;
	pFtpServer=new TCHAR[50];
	pFtpUser=new TCHAR[50];
	pFtpPass=new TCHAR[50];
	pFtpPort=new TCHAR[50];
	
	
	GetPrivateProfileString(_T("FTP"),_T("FTPSERVER"),_T("0"),pFtpServer,nlength,szConfig);
	GetPrivateProfileString(_T("FTP"),_T("UserName"),_T("0"),pFtpUser,nlength,szConfig);
	GetPrivateProfileString(_T("FTP"),_T("PASSWORD"),_T("0"),pFtpPass,nlength,szConfig);
	GetPrivateProfileString(_T("FTP"),_T("PORT"),_T("0"),pFtpPort,nlength,szConfig);
	//保存FTP数据库信息
	strFtpServer =pFtpServer;
	strFtpUser = pFtpUser;
	strFtpPass = pFtpPass;
	strFtpPort=pFtpPort;
	bFileFind=FALSE;
}
void  CMainFrame::SetDoc(CString  sInfo)
{
    
	CAccountInputDoc* pDoc = (CAccountInputDoc*)GetActiveDocument();
	CAccountInputView* pView = (CAccountInputView*)GetActiveView();
    CTime  tTime=CTime::GetCurrentTime();
	CString  strTime,strInfo;
	int nNum;
	strLogText=_T("");
	strTime=tTime.Format(_T("%c"));
	strInfo.Format(_T("[%s]   %s"),strTime,sInfo);
	strLogText = strInfo;
	strLogText += _T("\r\n");	
//	nNum=strLogText.GetLength();
	m_log.WriteLog(strLogText);
	pDoc->SetPhrase(strLogText);
		
}
void  CMainFrame::RunProgram()
{
	// TODO: Add your command handler code here
	if (bFileSource)
	{
		   if(!OnConnectFTP())
		   {
               
			   strTemp =_T("連接FTP服務器失败!");   
			   //AfxMessageBox(_T("連接FTP服務器失败!"));
			   this->SendMessage( WM_STATUSMSG, (WPARAM)&strTemp );
			   SetDoc(strTemp);
			   return;
		   }else
		   {
               strTemp =_T("連接FTP服務器成功!");   
			   //AfxMessageBox(_T("連接FTP服務器成功!"));
			   this->SendMessage( WM_STATUSMSG, (WPARAM)&strTemp );
			   SetDoc(strTemp);
			  
		   }
		   SelectFTPFile();
		   if(bFTPFileFind)
		   {
			   strTemp =_T("在FTP服務器中存在該文件!");  
			   this->SendMessage( WM_STATUSMSG, (WPARAM)&strTemp );
			   SetDoc(strTemp);
			                  

		   }
		   else
		   {
             strTemp =_T("在FTP服務器中不存在該文件!");   
			   this->SendMessage( WM_STATUSMSG, (WPARAM)&strTemp );
			    
			   SetDoc(strTemp);
			   return; 
		   }
		   if (!DownFile())
		   {
			  strTemp =_T("從FTP中讀取文件失敗!");
             //AfxMessageBox(_T("從FTP中讀取文件失敗!"));
			  this->SendMessage( WM_STATUSMSG, (WPARAM)&strTemp );
			 SetDoc(strTemp);
			 return;
		   }
		   else
		   {
			   strTemp =_T("從FTP中讀取文件成功!");
			   //AfxMessageBox(_T("從FTP中讀取文件成功!"));
			   this->SendMessage( WM_STATUSMSG, (WPARAM)&strTemp );
			   SetDoc(strTemp);
				
		   }
		   OnLocalSearch() ;
		
		   if(OnRead())
		   {
			   strTemp =_T("數據庫已經準備好!");
			   //AfxMessageBox(_T("數據庫已經準備好!!"));
			   this->SendMessage( WM_STATUSMSG, (WPARAM)&strTemp );
			   SetDoc(strTemp);
		   }
		   else
		   {
			   strTemp =_T("對數據庫執行操作失敗!");
			   //AfxMessageBox(_T("對數據庫執行操作失敗!"));
			   this->SendMessage( WM_STATUSMSG, (WPARAM)&strTemp );
			   SetDoc(strTemp);
			   return;
		   }
		  
		   if (ReadCSV())
		   {
			   strTemp =_T("文件導入數據庫成功!");
               CString abc;
			   abc.Format(_T("%s 共写入了%d条记录。"),strTemp,m_nTimes);
			   this->SendMessage( WM_STATUSMSG, (WPARAM)&abc );
			   SetDoc(abc);
		   }
		   else
		   {
			   strTemp =_T("數據導入數據庫失敗!");
			   //AfxMessageBox(_T("數據導入數據庫失敗!"));
			   this->SendMessage( WM_STATUSMSG, (WPARAM)&strTemp );
			   SetDoc(strTemp);
			   return;
		   }          
		   
	}else
	{
        OnButtonSearch();
		if (bFileFind)
		{
		   strTemp =_T("您要讀取的文件存在!");
		   //AfxMessageBox(_T("您要讀取的文件存在!"));
		   this->SendMessage( WM_STATUSMSG, (WPARAM)&strTemp );
		   SetDoc(strTemp);
		}
		else
		{
            //AfxMessageBox(_T("您要文件在當前目錄中不存在!"));
			strTemp =_T("您要文件在當前目錄中不存在!");
			this->SendMessage( WM_STATUSMSG, (WPARAM)&strTemp );
			SetDoc(strTemp);
			return;
		}		
		if (OnRead())
		{
           //AfxMessageBox(_T("數據庫準備就序!!"));
		   strTemp =_T("數據庫準備就序!");
		   this->SendMessage( WM_STATUSMSG, (WPARAM)&strTemp );
		   SetDoc(strTemp);
		}else
		{
			//AfxMessageBox(_T("數據庫操作错误!!"));
			strTemp =_T("數據庫操作错误!");
			this->SendMessage( WM_STATUSMSG, (WPARAM)&strTemp );
			SetDoc(strTemp);
			return;
		}
		if (ReadCSV())
		{
			
			strTemp =_T("文件導入數據庫成功!");
            CString abc;
			abc.Format(_T("%s 共写入了%d条记录。"),strTemp,m_nTimes);
			this->SendMessage( WM_STATUSMSG, (WPARAM)&abc );
			SetDoc(abc);
		}
	    else
		{
			strTemp =_T("文件導入數據庫失敗!");
			//AfxMessageBox(_T("文件導入數據庫失敗!"));
			this->SendMessage( WM_STATUSMSG, (WPARAM)&strTemp );
			SetDoc(strTemp);
			return;
		}			  
	 }
}
void CMainFrame::OnTimer(UINT nIDEvent) 
{
	// TODO: Add your message handler code here and/or call default
	CString  strJudgeTime;
	strJudgeTime=_T("00:00");
	CTime  tTime=CTime::GetCurrentTime();
	CString  strTime,strInfo;
	strTime=tTime.Format(_T("%H:%M"));
	if(strTime==strJudgeTime)
	{
	    OnConnectSql();
	    RunProgram();
		Sleep(1);
		AutoExport();
		OnCloseSql(); 
	}
	
}
LRESULT CMainFrame::OnSTATUSMSG( WPARAM pInfo ,LPARAM  pNun)
{
	CString *szInfo;
	szInfo = (CString*)pInfo;
	//刷新状态栏信息
	m_wndStatusBar.SetPaneText( m_wndStatusBar.CommandToIndex( ID_SEPARATOR), *szInfo);
	m_wndStatusBar.SetPaneText( m_wndStatusBar.CommandToIndex( ID_INDICATOR_CAPS), *szInfo);
	return 1;
}	
  
 void CMainFrame::SelectFTPFile()
{
	CString m_strMsg;
	int nNub;
	bFTPFileFind=FALSE;
	BOOL  bSircl=FALSE;
	BOOL m_bSuc=FALSE;
	m_strFtpPath=_T("");
	CString  strDir;
	CStringArray  m_strDirArray;
	CString   strFtpDir;
	strFtpDir=m_strFTPDir;
	if(strFtpDir.Right(1) != '/')
	{
		strFtpDir += '/';
	}
	if(strFtpDir.Left(1) == '/')
	{
		nNub=strFtpDir.GetLength();
	    strFtpDir=strFtpDir.Right(nNub-1);
	}
	int nPos=strFtpDir.Find('/');
	if(nPos>0)
		bSircl=TRUE;
	while(bSircl)
	{
      
		strDir=strFtpDir.Left(nPos);
		strFtpDir=strFtpDir.Mid(nPos+1);
	    nPos=strFtpDir.Find('/');
		if(nPos>0)
		{
		   bSircl=TRUE;
		}
		else
		{
           bSircl=FALSE;

		}
		m_bSuc=m_pFtpConnection->SetCurrentDirectory(strDir);
		if(!m_bSuc)		//进入目录里面
		{		      
			return;
		}
		m_bSuc=FALSE;

	}
	CString ABC;
	m_pFtpConnection->GetCurrentDirectory(ABC);
	CFtpFileFind   finder(m_pFtpConnection);
    BOOL bWorking=finder.FindFile(_T("*"));
	while (bWorking)
    {
		bWorking = finder.FindNextFile();
		if (finder.IsDots()) 
			continue;
		if (finder.IsDirectory())
		{  
			//m_saDir.Add(finder.GetFileName());	//如果是目录的话,就保存在数组里
		}
		else
		{
			m_strMsg=finder.GetFileName();	//如果不是目录的话,就先显示出来
            if(m_FileName==m_strMsg)
            bFTPFileFind=TRUE;
			m_strFtpPath =finder.GetFilePath();
			return;
		}
	}
	finder.Close();		//连接关闭	
	/*for(int i=0;i<m_saDir.GetSize();i++)	//开始遍历目录里的文件
	{	
		BOOL m_bSuc=FALSE;
		while (!m_bSuc)		//进入目录里面
		{
			
			m_bSuc=m_pFtpConnection->SetCurrentDirectory(m_saDir.GetAt(i));

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -