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

📄 mainfrm.cpp

📁 这是主要完成文件上传功能,对要写ftp程序者是一个好的参考源码
💻 CPP
📖 第 1 页 / 共 4 页
字号:
	}
	if(temp[strlen(temp)-1]=='\\'||temp[strlen(temp)-1]=='/')
	{
		temp[strlen(temp)-1]=0x00;
	}//强制规范路径
	csWebPath.Format("%s",temp);
	strcpy(sProjectAttrib[iNowProjectNo][4],temp);
	m_wndDlgBar.SetDlgItemText(IDC_WEBPATH,temp);
	m_wndDlgBar.GetDlgItemText(IDC_LOCALPATH,csLocalPath);
	if(iTotalProject==0)
	{
		strcpy(sProjectAttrib[0][0],csName);
		strcpy(sProjectAttrib[0][1],csUser);
		strcpy(sProjectAttrib[0][2],csName);
		strcpy(sProjectAttrib[0][3],csPassword);
		strcpy(sProjectAttrib[0][4],csWebPath);
		strcpy(sProjectAttrib[0][5],csLocalPath);
		iTotalProject=1;
	}
	if((MyFtpLogoThread = AfxBeginThread(MyFtpLogoThreadFunction,this)) == NULL)
	{
		//建立失败处理
		return;
	}
	else    //函数会建立一个辅助线程并暂时将其挂起。
	{
		MyFtpLogoThread->SetThreadPriority(THREAD_PRIORITY_BELOW_NORMAL);
		MyFtpLogoThread->ResumeThread();//调用CWinThread:: ResumeThread使线程开始运行
	}
}
 
void CMainFrame::OnCtlBreak() 
{
	// TODO: Add your command handler code here
	bExitApp=true;
	bAutoLoad=false;
	SetEvent(pLocalView->m_hPostEvent);//通知LocalView退出循环
}

BOOL CMainFrame::DestroyWindow() 
{
	// TODO: Add your specialized code here and/or call the base class
	bLinkDataGood=false;
	bExitApp=true;
	if(pLocalView!=NULL)
	{
		if(pLocalView->m_hPostEvent)
		{
			SetEvent(pLocalView->m_hPostEvent);//通知LocalView退出循环
		}
	}
	if(pWebView!=NULL)
	{
		if(pWebView->m_hPostEvent)
		{
			SetEvent(pWebView->m_hPostEvent);//通知WebView退出循环
		}
	}

	//***************去掉任务栏图标
	NOTIFYICONDATA tnid; 
 	tnid.cbSize = sizeof(NOTIFYICONDATA); 
	tnid.hWnd = GetSafeHwnd();
	tnid.uID = WM_SCNOTIFYICON; 
	Shell_NotifyIcon(NIM_DELETE, &tnid); 
	//--------------------
	KillTimer(101);

	return CFrameWnd::DestroyWindow();
}

void CMainFrame::OnButtonExit() 
{
	// TODO: Add your command handler code here
	DestroyWindow();
	//PostMessage(WM_QUIT,0,0);
}


UINT MyFtpLogoThreadFunction(LPVOID lpParam)//char *cPath, char *cBasePath)
{
	bLinkDataGood=false;
	char temp[256];
	int i,j,k;
	if(bWebToLocal)//如果是记录比较模式
	{
		iTotalWebFile=0;
		pMessageView->PostMessage(WM_MY_MESSAGE_UPDATE,0,(LPARAM)(LPCTSTR)"清除服务器列表...");
		ResetEvent(pWebView->m_hPostEvent);
		pWebView->PostMessage(WM_MY_WEBLIST_DELETE,0,0);
		if(WaitForSingleObject(pWebView->m_hPostEvent,0xffffffff)==WAIT_TIMEOUT)
		{
			pMessageView->PostMessage(WM_MY_MESSAGE_UPDATE,0,(LPARAM)(LPCTSTR)"超时");
		}
		pMessageView->PostMessage(WM_MY_MESSAGE_UPDATE,0,(LPARAM)(LPCTSTR)"\r\n");
		FILE *fo;
		int i,j,k,iTotal;
		strcpy(temp,sProjectAttrib[iNowProjectNo][2]);//得到地址
		strcat(temp,sProjectAttrib[iNowProjectNo][1]);//得到名称
		strcat(temp,".lst");
		fo=fopen(temp,"rb");
		if(fo==NULL)
		{
			//strcat(temp,"提取失败\r\n");
			pMessageView->PostMessage(WM_MY_MESSAGE_UPDATE,0,(LPARAM)(LPCTSTR)"提取记录失败,必须访问服务器\r\n");
		}
		else
		{
			fscanf(fo,"%d",&iTotal);
			for(i=0;i<iTotal;i++)
			{
				fscanf(fo,"%d",&k);
				fscanf(fo,"%c%c",&temp[0],&temp[1]);
				for(j=0;j<k;j++)
				{
					fscanf(fo,"%c",&temp[j]);
				}
				temp[j]=0x00;
				strcpy((char *)pWebView->m_csName,temp);
				//得到名称
				fscanf(fo,"%d",&k);
				fscanf(fo,"%c%c",&temp[0],&temp[1]);
				for(j=0;j<k;j++)
				{
					fscanf(fo,"%c",&temp[j]);
				}
				temp[j]=0x00;
				strcpy((char *)pWebView->m_csTime,temp);
				//得到时间
				fscanf(fo,"%d",&k);
				fscanf(fo,"%c%c",&temp[0],&temp[1]);
				for(j=0;j<k;j++)
				{
					fscanf(fo,"%c",&temp[j]);
				}
				temp[j]=0x00;
				strcpy((char *)pWebView->m_csSize,temp);
				//得到大小
				fscanf(fo,"%d",&k);
				fscanf(fo,"%c%c",&temp[0],&temp[1]);
				for(j=0;j<k;j++)
				{
					fscanf(fo,"%c",&temp[j]);
				}
				temp[j]=0x00;
				strcpy((char *)pWebView->m_csPath,temp);
				//得到路径
				strcpy((char *)pWebView->m_csAttrib,"未知");
				//得到属性
				iTotalWebFile++;
				ResetEvent(pWebView->m_hPostEvent);
				pWebView->PostMessage(WM_MY_WEBLIST_INSERT,0,0);
				if(WaitForSingleObject(pWebView->m_hPostEvent,0xffffffff)==WAIT_TIMEOUT)
				{
					pMessageView->PostMessage(WM_MY_MESSAGE_UPDATE,0,(LPARAM)(LPCTSTR)"\r\n超时");
				}
			}
			fclose(fo);
			sprintf(temp,"\r\n查找文件结束,文件:%d个\r\n",
				iTotalWebFile);
			pMessageView->PostMessage(WM_MY_MESSAGE_UPDATE,0,(LPARAM)(LPCTSTR)temp);
			if(bAutoLoad)//自动模式
			{
				pWorkView->PostMessage(WM_MY_WORK_LISTNEW);
			}
			return 1;		 //跳过服务器访问并退出线程
		}

	}
	CMainFrame *pMain=(CMainFrame*)lpParam;
	CString csMessage;
	bool bIsDir=false;
	bWebTry=false;
	if(bAutoDial)	//如果是自动拨号模式
	{
		if(!Dial())//拨号失败循环
		{
			if(bAutoDialAgain)//失败重拨模式
			{
				for(i=0;i<iDialLoop;i++)
				{
					HangUp();
					csMessage.Format("\r\n拨号失败,延迟%d秒后进行第%d次(共%d次)重试\r\n",iDialDelay,i+1,iDialLoop);
					pMessageView->PostMessage(WM_MY_MESSAGE_UPDATE,0,(LPARAM)(LPCTSTR)csMessage);
					::Sleep(iDialDelay*1000);
					if(Dial())
						break;
					if(bExitApp)
						break;
				}
				if(i>=iDialLoop)//拨号已满
				{
					bAutoLoad=false;//关闭自动模式
					pMessageView->PostMessage(WM_MY_MESSAGE_UPDATE,0,(LPARAM)(LPCTSTR)"拨号失败");
					return 0;//关闭退出线程
				}
			}
			else
			{
				bAutoLoad=false;//关闭自动模式
				pMessageView->PostMessage(WM_MY_MESSAGE_UPDATE,0,(LPARAM)(LPCTSTR)"拨号失败");
				return 0;//关闭退出线程
			}
		}//拨号成功
	}
	pMessageView->PostMessage(WM_MY_MESSAGE_UPDATE,0,(LPARAM)(LPCTSTR)"清除服务器列表...");
	ResetEvent(pWebView->m_hPostEvent);
	pWebView->PostMessage(WM_MY_WEBLIST_DELETE,0,0);
	if(WaitForSingleObject(pWebView->m_hPostEvent,0xffffffff)==WAIT_TIMEOUT)
	{
		pMessageView->PostMessage(WM_MY_MESSAGE_UPDATE,0,(LPARAM)(LPCTSTR)"超时");
	}
	pMessageView->PostMessage(WM_MY_MESSAGE_UPDATE,0,(LPARAM)(LPCTSTR)"\r\n");
	iTotalWebFile=0;
	iTotalWebDir=0;
	dwTotalWebFileLong=0;
	bWebTry=true;		//允许服务器重试
	HINTERNET hInetSession=InternetOpen("EasyFtp-SerchWeb",INTERNET_OPEN_TYPE_PRECONFIG,NULL,NULL,0);
	csMessage="正在连接->";
	csMessage+=csName;
	csMessage+="服务器\r\n";
	pMessageView->PostMessage(WM_MY_MESSAGE_UPDATE,0,(LPARAM)(LPCTSTR)csMessage);
	HINTERNET hFtpConn=InternetConnect(hInetSession,csName,21,
		csUser,csPassword,INTERNET_SERVICE_FTP,INTERNET_FLAG_PASSIVE,0);
	if(!hFtpConn)
	{
		csMessage=csName;
		csMessage+="服务器未接上\r\n";
		pMessageView->PostMessage(WM_MY_MESSAGE_UPDATE,0,(LPARAM)(LPCTSTR)csMessage);
		InternetCloseHandle(hInetSession);
		if(bWebAgain)//服务器失败重试模式
		{
			pMainFrame->PostMessage(WM_MY_WEBLOOP_LOGO);
		}
		else
		{
			bAutoLoad=false;	//关闭自动模式
		}
		return 0;//关闭退出线程
	}
	bLinkDataGood=true;
	csMessage=csUser;
	csMessage+="已经成功登录";
	csMessage+=csName;
	csMessage+="服务器\r\n";
	pMessageView->PostMessage(WM_MY_MESSAGE_UPDATE,0,(LPARAM)(LPCTSTR)csMessage);
	
	::Sleep(1000);

	/////////////////////////
	//strcpy(sNowWebPath,"\\");
	//strcat(sNowWebPath,csWebPath);
	char cWebTruePath[256];
	//char temp[256];
	char temp2[256];
	char *ptemp;
	char tempPath[256];
	strcpy(tempPath,csWebPath);
loop:
	strcpy(sNowWebPath,tempPath);
	strcpy(temp,tempPath);
	if(temp[0]!='\\')
	{
		strcpy(temp,"\\");
		strcat(temp,tempPath);
	}
	ptemp=temp;
	strcpy(temp2,ptemp+1);
	if(strcmp(temp2,"")!=0)
	{
		int kkk=strlen(temp2);
		temp2[kkk]='\\';
		temp2[kkk+1]=0x00;
	}
	strcpy(cWebTruePath,temp2);
	strcpy(temp2,csWebPath);
	if(strcmp(temp2,"")==0)//没有路径则恢复
	{
		strcpy(temp2,cWebTruePath);
	}
	else	//去掉基本路径
	{
		i=strlen(temp2);
		j=strlen(cWebTruePath);
		for(k=0;k<(j-i-1);k++)
		{
			cWebTruePath[k]=cWebTruePath[k+i+1];
		}
		cWebTruePath[k]=0x00;
		strcpy(temp2,cWebTruePath);
	}
	strcpy((char *)pWebView->m_csPath,temp2);
	if(strcmp(temp,"\\")!=0)
	{
		csMessage="\r\n进入目录->";
		csMessage+=temp;
		pMessageView->PostMessage(WM_MY_MESSAGE_UPDATE,0,(LPARAM)(LPCTSTR)csMessage);
		for(unsigned int i=0;i<(strlen(temp)+1);i++)
		{
			cWebTruePath[i]=temp[i];
			if(cWebTruePath[i]=='\\')
			{
				cWebTruePath[i]='/';
			}
		}
		if(FtpSetCurrentDirectory(hFtpConn,cWebTruePath))
		{
			pMessageView->PostMessage(WM_MY_MESSAGE_UPDATE,0,(LPARAM)(LPCTSTR)" 成功\r\n");;
			ptemp=temp;
			strcpy(sNowWebPath,temp);
			strcat(sNowWebPath,"\\");
		}
		else//服务器访问失败
		{
			bLinkDataGood=false;
			pMessageView->PostMessage(WM_MY_MESSAGE_UPDATE,0,(LPARAM)(LPCTSTR)"失败\r\n连接关闭\r\n");
			InternetCloseHandle(hFtpConn);
			InternetCloseHandle(hInetSession);//关闭连接
			if(bWebAgain)//服务器失败重试模式
			{
				pMainFrame->PostMessage(WM_MY_WEBLOOP_LOGO);
			}
			else
			{
				bAutoLoad=false;	//关闭自动模式
			}
			return 0;//关闭退出线程
		}
		/*
		pMessageView->PostMessage(WM_MY_MESSAGE_UPDATE,0,(LPARAM)(LPCTSTR)"目前目录:");
		if(FtpGetCurrentDirectory(hFtpConn,temp,&dwLength))
		{
			pMessageView->PostMessage(WM_MY_MESSAGE_UPDATE,0,(LPARAM)(LPCTSTR)temp);
			pMessageView->PostMessage(WM_MY_MESSAGE_UPDATE,0,(LPARAM)(LPCTSTR)"\r\n");
		}
		else
		{
			pMessageView->PostMessage(WM_MY_MESSAGE_UPDATE,0,(LPARAM)(LPCTSTR)"未知\r\n");
		}*/
	}
	FILETIME fileTime;
	HINTERNET hFind;
	WIN32_FIND_DATA findData;
	CString szFile;
//	DWORD dwLength;
	//FtpGetCurrentDirectory(hFtpConn,NULL,&dwLength);
	pMessageView->PostMessage(WM_MY_MESSAGE_UPDATE,0,(LPARAM)(LPCTSTR)"正在检查服务器文件...");
	strcpy((char *)pWebView->m_csAttrib,"未知");
	hFind=FtpFindFirstFile(hFtpConn,_T("*"),&findData,
		INTERNET_FLAG_RELOAD,0);
	if(!(hFind))
	{
		if (GetLastError()  == ERROR_NO_MORE_FILES) 
		{
			pMessageView->PostMessage(WM_MY_MESSAGE_UPDATE,0,(LPARAM)(LPCTSTR)"目录为空,没有多余的内容");
			goto end;
		}
		else		//防止空文件处理
		{
			pMessageView->PostMessage(WM_MY_MESSAGE_UPDATE,0,(LPARAM)(LPCTSTR)"文件查找出错,未知原因\r\n");
			InternetCloseHandle(hFind);
			if(bWebAgain)//服务器重试模式
			{
				InternetCloseHandle(hFtpConn);
				InternetCloseHandle(hInetSession);//关闭连接
				pMessageView->PostMessage(WM_MY_MESSAGE_UPDATE,0,(LPARAM)(LPCTSTR)"连接已经关闭\r\n");
				bLinkDataGood=false;
				pMainFrame->PostMessage(WM_MY_WEBLOOP_LOGO);
				return 0;
			}
			else
			{
				bAutoLoad=false;
				goto ok;
			}
		}
	}
	do{
		if(bExitApp)
			break;
		szFile=findData.cFileName;
		csMessage="\r\n文件名";
		csMessage+=szFile;
		strcpy((char *)pWebView->m_csName,szFile);
		fileTime=findData.ftLastWriteTime;
		CTime time=CTime(fileTime);
		CString str;
		str.Format("%04d-%02d-%02d %02d:%02d",time.GetYear(),
			time.GetMonth(),time.GetDay(),time.GetHour()-8,time.GetMinute());
		csMessage+="文件时间";
		csMessage+=str;
		strcpy((char *)pWebView->m_csTime,str);
		bIsDir=false;
		if(findData.dwFileAttributes&FILE_ATTRIBUTE_DIRECTORY)
		{
			bIsDir=true;
			iTotalWebDir+=1;
			csMessage+=" <目录>";
			strcpy(sWebPathNeedSerch[iPathNo],sNowWebPath);
			//AfxMessageBox(findData.cFileName);
			//AfxMessageBox(sNowWebPath);
			strcat(sWebPathNeedSerch[iPathNo++],findData.cFileName);
			//AfxMessageBox(sWebPathNeedSerch[iPathNo-1]);
		}
		else//找到文件
		{
			iTotalWebFile+=1;
			dwTotalWebFileLong+=findData.nFileSizeLow;
			DWORD i=findData.nFileSizeLow;
			str.Format("%ld",i);
			csMessage+="文件大小";
			csMessage+=str;
			strcpy((char *)pWebView->m_csSize,str);
		}
		pMessageView->PostMessage(WM_MY_MESSAGE_UPDATE,0,(LPARAM)(LPCTSTR)csMessage);

		if(!bIsDir)
		{
			ResetEvent(pWebView->m_hPostEvent);
			pWebView->PostMessage(WM_MY_WEBLIST_INSERT,0,0);
			if(WaitForSingleObject(pWebView->m_hPostEvent,0xffffffff)==WAIT_TIMEOUT)
			{
				pMessageView->PostMessage(WM_MY_MESSAGE_UPDATE,0,(LPARAM)(LPCTSTR)"\r\n超时");
			}
		}
	}
	while(InternetFindNextFile(hFind,&findData));
end:InternetCloseHandle(hFind);
	if(iPathNo>0)
	{
		if(bExitApp)
		{
			goto ok;
		}
		else
		{
			strcpy(tempPath,sWebPathNeedSerch[--iPathNo]);
			goto loop;
		}
	}
ok:
	csMessage.Format("\r\n查找文件结束,文件:%d个、目录:%d个、共%ld字节\r\n",
		iTotalWebFile,iTotalWebDir,dwTotalWebFileLong);
	pMessageView->PostMessage(WM_MY_MESSAGE_UPDATE,0,(LPARAM)(LPCTSTR)csMessage);
	::Sleep(1000);
	InternetCloseHandle(hFtpConn);
	InternetCloseHandle(hInetSession);//关闭连接
	pMessageView->PostMessage(WM_MY_MESSAGE_UPDATE,0,(LPARAM)(LPCTSTR)"连接已经关闭\r\n");
	bLinkDataGood=false;
	if(bAutoLoad)//自动模式
	{
		pWorkView->PostMessage(WM_MY_WORK_LISTNEW);
	}
	return 1;
}


void CMainFrame::OnCtlHuangup() 
{
	// TODO: Add your command handler code here
	HangUp();
}

void CMainFrame::OnUpdateProject(WPARAM wParam, LPARAM lParam)
{
	m_wndDlgBar.SetDlgItemText(IDC_FTPADD,sProjectAttrib[iNowProjectNo][2]);
	m_wndDlgBar.SetDlgItemText(IDC_FTPUSER,sProjectAttrib[iNowProjectNo][1]);
	m_wndDlgBar.SetDlgItemText(IDC_FTPPASSWORD,sProjectAttrib[iNowProjectNo][3]);
	m_wndDlgBar.SetDlgItemText(IDC_WEBPATH,sProjectAttrib[iNowProjectNo][4]);
	m_wndDlgBar.SetDlgItemText(IDC_LOCALPATH,sProjectAttrib[iNowProjectNo][5]);
}

void CMainFrame::OnChangeFtpadd() 
{
	// TODO: If this is a RICHEDIT control, the control will not
	// send this notification unless you override the CFrameWnd::OnInitDialog()
	// function and call CRichEditCtrl().SetEventMask()
	// with the ENM_CHANGE flag ORed into the mask.
	CString te;
	m_wndDlgBar.GetDlgItemText(IDC_FTPADD,te);
	strcpy(sProjectAttrib[iNowProjectNo][2],te);
	// TODO: Add your control notification handler code here
}

void CMainFrame::OnChangeFtpuser() 
{
	// TODO: If this is a RICHEDIT control, the control will not
	// send this notification unless you override the CFrameWnd::OnInitDialog()
	// function and call CRichEditCtrl().SetEventMask()
	// with the ENM_CHANGE flag ORed into the mask.
	CString te;
	m_wndDlgBar.GetDlgItemText(IDC_FTPUSER,te);
	strcpy(sProjectAttrib[iNowProjectNo][1],te);
	// TODO: Add your control notification handler code here
	
}

void CMainFrame::OnChangeFtppassword() 
{
	// TODO: If this is a RICHEDIT control, the control will not
	// send this notification unless you override the CFrameWnd::OnInitDialog()
	// function and call CRichEditCtrl().SetEventMask()
	// with the ENM_CHANGE flag ORed into the mask.
	CString te;
	m_wndDlgBar.GetDlgItemText(IDC_FTPPASSWORD,te);
	strcpy(sProjectAttrib[iNowProjectNo][3],te);
	// TODO: Add your control notification handler code here
}

void CMainFrame::OnChangeLocalpath() 
{
	// TODO: If this is a RICHEDIT control, the control will not
	// send this notification unless you override the CFrameWnd::OnInitDialog()
	// function and call CRichEditCtrl().SetEventMask()
	// with the ENM_CHANGE flag ORed into the mask.
	CString te;
	m_wndDlgBar.GetDlgItemText(IDC_LOCALPATH,te);
	strcpy(sProjectAttrib[iNowProjectNo][5],te);
	// TODO: Add your control notification handler code here
}

void CMainFrame::OnChangeWebpath() 
{

⌨️ 快捷键说明

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