Ftp文件下载工具

读取txt文件,在ftp中下载对应文件

void CAboutDlg::OnBnClickedButton1()
{
	// TODO: 在此添加控件通知处理程序代码
	TCHAR ch1[200], ch2[200], ch3[200], ch4[200], ch5[200], ch6[200];
	GetDlgItem(IDC_EDIT3)->GetWindowTextW(ch1, 200);
	GetDlgItem(IDC_EDIT5)->GetWindowTextW(ch2, 200);
	GetDlgItem(IDC_EDIT1)->GetWindowTextW(ch3, 200);
	GetDlgItem(IDC_EDIT4)->GetWindowTextW(ch4, 200);
	GetDlgItem(IDC_EDIT2)->GetWindowTextW(ch5, 200);
	GetDlgItem(IDC_EDIT6)->GetWindowTextW(ch6, 200);

	CString url = ch1;
	CString ID = ch2;
	CString Password = ch3;
	CString Ftpflodername = ch4;
	CString Localpath = ch5;
	CString Localtextname = ch6;

	ID.Replace(_T(" "), _T(""));
	Password.Replace(_T(" "), _T(""));

	CInternetSession* pSession = NULL;
	CFtpConnection* pConnection = NULL;
	pSession = new CInternetSession(AfxGetAppName());
	pConnection = pSession->GetFtpConnection(url, ID, Password, 21);

	bool b = pConnection->SetCurrentDirectory(Ftpflodername);
	if (b)
	{
		pConnection->GetFile(Ftpflodername + _T(".txt"), Localpath + _T("/") + Ftpflodername + _T(".txt"));
	}

	std::ifstream of;
	of.open(Localpath + _T("/") + Ftpflodername + _T(".txt"));
	if (!of.is_open())
	{
		AfxMessageBox(L"读取txt文件失败");
	}

	std::string str;
	CString readname;
	CString filename;

	while (!of.eof())
	{
		getline(of, str, '\n');
		readname = str.c_str();
		bool b = pConnection->SetCurrentDirectory(readname);
		filename = _T("XX_") + readname;
		pConnection->GetFile(filename + _T(".csv"), Localpath + filename + _T(".csv"));
		pConnection = pSession->GetFtpConnection(url, ID, Password, 21);
		bool b1 = pConnection->SetCurrentDirectory(Ftpflodername);
	}
	if (pConnection != NULL)
	{
		pConnection->Close();
		delete pConnection;
	}

	if (pSession != NULL)
	{
		pSession->Close();
		delete pSession;
	}
	AfxMessageBox(L"文件下载完毕");
}

ui界面

Ftp文件下载工具_第1张图片

你可能感兴趣的:(C++,c++,mfc,ftp)