当前位置: 开源爱好者 » 编程教程 » VC++教程 » 正文

VC++ FTP上传下载文件程序

#include "stdafx.h"
#include "MyFtp.h"
#include "MyFtpDlg.h"
BOOL CMyFtpDlg::OnInitDialog()
{
	CDialog::OnInitDialog();
	//初始化两个列表框控件
	SetListCtrlStyle(&m_ctrlLocalFiles);
	SetListCtrlStyle(&m_ctrlRemoteFiles);
	AddHeaders(&m_ctrlLocalFiles);
	AddHeaders(&m_ctrlRemoteFiles);
	//本地文件目录以c盘为根目录
	BrowseDir("c:",&m_ctrlLocalFiles,&m_LocalFinder,&m_arrLocalFiles);
	return TRUE;  // return TRUE  unless you set the focus to a control
}
//设置列表控件CListCtrl的风格
void CMyFtpDlg::SetListCtrlStyle(CListCtrl* pLstCtrl)
{
	//获得原有风格
	DWORD dwStyle = GetWindowLong(pLstCtrl->m_hWnd, GWL_STYLE);
	dwStyle &= ~(LVS_TYPEMASK);
	dwStyle &= ~(LVS_EDITLABELS);
	//设置新风格
    SetWindowLong(pLstCtrl->m_hWnd, GWL_STYLE, dwStyle | LVS_REPORT|LVS_NOLABELWRAP|LVS_SHOWSELALWAYS);
	//设置扩展风格
	DWORD styles = LVS_EX_FULLROWSELECT|LVS_EX_GRIDLINES;
	ListView_SetExtendedListViewStyleEx(pLstCtrl->m_hWnd, styles, styles );
}
//给列表控件CListCtrl增加标题头
void CMyFtpDlg::AddHeaders(CListCtrl* pLstCtrl)
{
	TCHAR rgtsz[2][10] = {_T("文件名"),
		_T("类型")};
	LV_COLUMN lvcolumn;
	CRect rect;
	pLstCtrl->GetWindowRect(&rect);
	for(int i=0;i<2;i++)
	{
		lvcolumn.mask = LVCF_FMT | LVCF_SUBITEM | LVCF_TEXT
						| LVCF_WIDTH | LVCF_ORDER;
		lvcolumn.fmt = LVCFMT_LEFT;
		lvcolumn.pszText = rgtsz[i];
		lvcolumn.iSubItem = i;
		lvcolumn.iOrder = i;
		if(i==0)
		{
			lvcolumn.cx = rect.Width()*2/3 ;
		}
		else
			lvcolumn.cx = rect.Width()/3 ;
		pLstCtrl->InsertColumn(i, &lvcolumn);
	}
}
//连接ftp服务器
void CMyFtpDlg::OnConnect()
{
	UpdateData(TRUE);
	//新建对话
	m_pInetSession=new CInternetSession(AfxGetAppName(),1,PRE_CONFIG_INTERNET_ACCESS);
	try
	{
		//新建连接对象
		m_pFtpConnection=m_pInetSession->GetFtpConnection(m_strServer,m_strUserName,
			m_strPassword);
	}
	catch(CInternetException *pEx)
	{
		//获取错误
		TCHAR szError[1024];
		if(pEx->GetErrorMessage(szError,1024))
			AfxMessageBox(szError);
		else
			AfxMessageBox("There was an exception");
		pEx->Delete();
		m_pFtpConnection=NULL;
		return;
	}
	m_pRemoteFinder = new CFtpFileFind(m_pFtpConnection);
	//获得服务器根目录的所有文件并在列表框中显示
	BrowseDir("",&m_ctrlRemoteFiles,m_pRemoteFinder,&m_arrRemoteFiles);
}
//获得指定目录下的所有文件,并在列表框中显示
void CMyFtpDlg::BrowseDir(CString strDir,CListCtrl* pLstCtrl,CFileFind* pFinder,CArray<FILEITEM,FILEITEM&>* pFilesArr)
{
	//清空列表框
	pLstCtrl->DeleteAllItems();
	pFilesArr->RemoveAll();
	int nIndex = 0;
	CString szDir = strDir;
	if(szDir.Right(1) != "\\")
		szDir += "\\";
	szDir += "*.*";
	//查找目录下的文件
	BOOL res = pFinder->FindFile(szDir);
	while(res)
	{
		res = pFinder->FindNextFile();
		//是目录
		if(pFinder->IsDirectory() && !pFinder->IsDots())
		{
			FILEITEM fileitem;
			fileitem.nItem = nIndex;
			fileitem.bDir = TRUE;
			fileitem.strFileName = pFinder->GetFileName();
			pFilesArr->Add(fileitem);
			AddItem(pLstCtrl,nIndex,TRUE,pFinder->GetFileName());
		}
		//是文件
		else if(!pFinder->IsDirectory() && !pFinder->IsDots())
		{
			FILEITEM fileitem;
			fileitem.nItem = nIndex;
			fileitem.bDir = FALSE;
			fileitem.strFileName = pFinder->GetFileName();
			pFilesArr->Add(fileitem);
			AddItem(pLstCtrl,nIndex,FALSE,pFinder->GetFileName());
		}
		nIndex++;
	}
}
//给列表框增加一条记录
void CMyFtpDlg::AddItem(CListCtrl* pLstCtrl,int nIndex,BOOL bDir,CString strText)
{
	LV_ITEM lvitem;
	int iActualItem;
	for(int iSubItem=0;iSubItem<2;iSubItem++)
	{
		lvitem.mask = LVIF_TEXT|(iSubItem == 0? LVIF_IMAGE : 0);
		lvitem.iItem = (iSubItem == 0)? nIndex : iActualItem;
		lvitem.iSubItem = iSubItem;
		switch(iSubItem)
		{
		case 0:
			lvitem.pszText =(LPTSTR)(LPCTSTR)strText;
			break;
		case 1:
			if(bDir)
				lvitem.pszText = "文件夹";
			else
				lvitem.pszText = "文件";
			break;
		}
		if (iSubItem == 0)
			iActualItem = pLstCtrl->InsertItem(&lvitem);
		else
			pLstCtrl->SetItem(&lvitem);
	}
}
//响应“download”按钮
void CMyFtpDlg::OnDownload()
{
	//如果没有建立连接,退出
	if(m_pFtpConnection==NULL)
		return;
	//下载所有选中文件
	POSITION pos = m_ctrlRemoteFiles.GetFirstSelectedItemPosition();
	if (pos == NULL)
		AfxMessageBox("请选择要下载的文件");
	else
	{
		while (pos)
		{
			int nItem = m_ctrlRemoteFiles.GetNextSelectedItem(pos);
			DownFile(m_arrRemoteFiles.GetAt(nItem));
		}
	}
}
//响应“Upload”按钮
void CMyFtpDlg::OnUpload()
{
	//如果没有建立连接,退出
	if(m_pFtpConnection==NULL)
		return;
	//上传所有选中文件
	POSITION pos = m_ctrlLocalFiles.GetFirstSelectedItemPosition();
	if (pos == NULL)
		AfxMessageBox("请选择要上传的文件");
	else
	{
		while (pos)
		{
			int nItem = m_ctrlLocalFiles.GetNextSelectedItem(pos);
			UpFile(m_arrLocalFiles.GetAt(nItem));
		}
	}
}
//下载单个文件
void CMyFtpDlg::DownFile(FILEITEM fileItem)
{
	if(fileItem.bDir == TRUE)
	{
		AfxMessageBox("本程序暂时不支持下载整个文件夹,请选择文件下载");
	}
	else
	{
		//格式化文件名
		CString strLocalFile,strRemoteFile;
		strRemoteFile.Format("%s\\%s",m_pRemoteFinder->GetRoot(),fileItem.strFileName);
		strLocalFile.Format("%s\\%s",m_LocalFinder.GetRoot(),fileItem.strFileName);
		//下载
		if(m_pFtpConnection->GetFile(strLocalFile,strLocalFile))
		{
			CString strMsg;
			strMsg.Format("下载文件%s成功!",fileItem.strFileName);
			AfxMessageBox(strMsg);
		}
	}
}
//上传单个文件
void CMyFtpDlg::UpFile(FILEITEM fileItem)
{
	if(fileItem.bDir == TRUE)
	{
		AfxMessageBox("本程序暂时不支持上载整个文件夹,请选择文件上载");
	}
	else
	{
		//格式化文件名
		CString strLocalFile,strRemoteFile;
		strRemoteFile.Format("%s\\%s",m_pRemoteFinder->GetRoot(),fileItem.strFileName);
		strLocalFile.Format("%s\\%s",m_LocalFinder.GetRoot(),fileItem.strFileName);
		//上传
		if(m_pFtpConnection->PutFile(strLocalFile,strLocalFile))
		{
			CString strMsg;
			strMsg.Format("上载文件%s成功!",fileItem.strFileName);
			AfxMessageBox(strMsg);
		}
	}
}
//本地文件目录中向上一层
void CMyFtpDlg::OnLocalUp()
{
	if(m_strLocalParentRoot=="")
		return;
	BrowseDir(m_strLocalParentRoot,&m_ctrlLocalFiles,&m_LocalFinder,&m_arrLocalFiles);
}
//本地文件目录中向下一层
void CMyFtpDlg::OnLocalDownd()
{
	POSITION pos = m_ctrlLocalFiles.GetFirstSelectedItemPosition();
	if(pos)
	{
		int nItem = m_ctrlRemoteFiles.GetNextSelectedItem(pos);
		FILEITEM fileItem = m_arrLocalFiles.GetAt(nItem);
		if(fileItem.bDir==TRUE)
		{
			CString strDir;
			strDir.Format("%s\\%s",m_LocalFinder.GetRoot(),fileItem.strFileName);
			m_strLocalParentRoot = m_LocalFinder.GetRoot();
			BrowseDir(strDir,&m_ctrlLocalFiles,&m_LocalFinder,&m_arrLocalFiles);
		}
	}
}
//远程文件目录中向上一层
void CMyFtpDlg::OnRemoteUp()
{
	if(m_pFtpConnection==NULL)
		return;
	if(m_strRemoteParentRoot=="")
		return;
	BrowseDir(m_strRemoteParentRoot,&m_ctrlRemoteFiles,m_pRemoteFinder,&m_arrRemoteFiles);
}
//远程文件目录中向下一层
void CMyFtpDlg::OnRemoteDownd()
{
	POSITION pos = m_ctrlRemoteFiles.GetFirstSelectedItemPosition();
	if(pos)
	{
		int nItem = m_ctrlRemoteFiles.GetNextSelectedItem(pos);
		FILEITEM fileItem = m_arrRemoteFiles.GetAt(nItem);
		if(fileItem.bDir==TRUE)
		{
			CString strDir;
			strDir.Format("%s\\%s",m_pRemoteFinder->GetRoot(),fileItem.strFileName);
			m_strRemoteParentRoot = m_pRemoteFinder->GetRoot();
			BrowseDir(strDir,&m_ctrlRemoteFiles,m_pRemoteFinder,&m_arrRemoteFiles);
		}
	}
}

这个基本的FTP程序,可获得指定目录下的所有文件,并在列表框中显示,并给列表控件CListCtrl增加标题头,上传和下载单个文件,暂时不支持下载整个文件夹。

CopyRight 2018© 开源爱好者 All Rights Reserved 豫ICP备16030607号 隐私声明 网站地图