📄 simpleftp.cpp
字号:
#include <afxwin.h>
#include <afxdlgs.h>
#include <afxinet.h> // 一定要包含此文件才能使用Winlnet API
#include <afxsock.h>
#include "resource.h"
//
class CFtpDlg : public CDialog
{
public:
CFtpDlg(CWnd *pParent = NULL);
//
virtual BOOL OnInitDialog();
virtual BOOL PreTranslateMessage(MSG* pMsg);
virtual void OnOK();
//
void OnConnectbtn();
void OnAnonymous();
void OnUpDownFile(UINT nID);
void OnTransfermode(UINT nID);
void OnFilebtn(UINT nID);
void OnDblclkList();
void OnSelchangeList();
void OnSendbtn();
void ResetListContent();
//
protected:
HICON m_hIcon;
CString m_ftpserver;
int m_nPort;
CString m_username;
CString m_password;
BOOL m_bIsAnonymous;
BOOL m_bIsUpload;
BOOL m_bIsBinarymode;
BOOL m_bIsStartDownload;
CString m_srcfilename;
CString m_targetname;
CListBox* m_pListbox;
//
void GetFTPFile();
void EnableItem(BOOL bEnable = FALSE);
DECLARE_MESSAGE_MAP()
};
//
BEGIN_MESSAGE_MAP(CFtpDlg, CDialog)
ON_BN_CLICKED(IDC_CONNECTBTN, OnConnectbtn)
ON_BN_CLICKED(IDC_SENDBTN, OnSendbtn)
ON_BN_CLICKED(IDC_ANONYMOUS, OnAnonymous)
ON_CONTROL_RANGE(BN_CLICKED, IDC_UPLOADFILE, IDC_DOWNLOADFILE, OnUpDownFile)
ON_CONTROL_RANGE(BN_CLICKED, IDC_TRANSFERMODE1, IDC_TRANSFERMODE2, OnTransfermode)
ON_CONTROL_RANGE(BN_CLICKED, IDC_SRCFILEBTN, IDC_TARGETFILEBTN, OnFilebtn)
ON_LBN_DBLCLK(IDC_LISTFILE, OnDblclkList)
ON_LBN_SELCHANGE(IDC_LISTFILE, OnSelchangeList)
END_MESSAGE_MAP()
//
CFtpDlg::CFtpDlg(CWnd *pParent)
:CDialog(IDD_FTPDLG, pParent)
{
m_ftpserver = _T("");
m_username = _T("Anonymous");
m_password = _T("guest");
m_bIsAnonymous = TRUE;
m_bIsBinarymode = TRUE;
m_bIsStartDownload = FALSE;
m_bIsUpload = FALSE;
m_srcfilename = _T("");
m_targetname = _T("");
m_nPort = 21;
m_pListbox = 0;
m_hIcon = AfxGetApp()->LoadIcon(IDI_NET08);
};
//
void CFtpDlg::EnableItem(BOOL bEnable)
{
GetDlgItem(IDC_USERNAME)->EnableWindow(bEnable);
GetDlgItem(IDC_PASSWORD)->EnableWindow(bEnable);
};
//
BOOL CFtpDlg::OnInitDialog()
{
DWORD dwStyle = ::GetClassLong(m_hWnd, GCL_STYLE);
dwStyle |= CS_NOCLOSE;
::SetClassLong(m_hWnd, GCL_STYLE, dwStyle);
//
SetIcon(m_hIcon, TRUE);
SetIcon(m_hIcon, FALSE);
SetDlgItemText(IDC_USERNAME, m_username);
SetDlgItemText(IDC_PASSWORD, m_password);
SetDlgItemInt(IDC_PORTNUM, m_nPort);
((CButton*)GetDlgItem(IDC_ANONYMOUS))->SetCheck(1);
((CButton*)GetDlgItem(IDC_DOWNLOADFILE))->SetCheck(1);
((CButton*)GetDlgItem(IDC_TRANSFERMODE2))->SetCheck(1);
GetDlgItem(IDC_SRCFILEBTN)->EnableWindow(FALSE);
m_pListbox = (CListBox*)GetDlgItem(IDC_LISTFILE);
EnableItem();
return TRUE;
};
//
void CFtpDlg::OnUpDownFile(UINT nID)
{
CButton* b1 = new CButton, *b2 = new CButton;
b1->Attach(GetDlgItem(IDC_UPLOADFILE)->m_hWnd);
b2->Attach(GetDlgItem(IDC_DOWNLOADFILE)->m_hWnd);
//
if (nID == IDC_UPLOADFILE)
{
b2->SetCheck(0);
b1->SetCheck(1);
}
else
{
b1->SetCheck(0);
b2->SetCheck(1);
};
(b1->GetCheck())
? GetDlgItem(IDC_SRCFILEBTN)->EnableWindow()
: GetDlgItem(IDC_SRCFILEBTN)->EnableWindow(FALSE);
b1->Detach();
b2->Detach();
delete b1;
delete b2;
};
//
void CFtpDlg::OnTransfermode(UINT nID)
{
CButton* b1 = new CButton, *b2 = new CButton;
b1->Attach(GetDlgItem(IDC_TRANSFERMODE1)->m_hWnd);
b2->Attach(GetDlgItem(IDC_TRANSFERMODE2)->m_hWnd);
//
if (nID == IDC_TRANSFERMODE1)
{
b2->SetCheck(0);
b1->SetCheck(1);
}
else
{
b1->SetCheck(0);
b2->SetCheck(1);
};
b1->Detach();
b2->Detach();
delete b1;
delete b2;
};
//
void CFtpDlg::ResetListContent()
{
CWaitCursor s;
//
while (m_pListbox->GetCount())
m_pListbox->DeleteString(0);
//
CInternetSession* pSession = 0;
CFtpConnection* pConnection = 0;
CFtpFileFind* pFile = 0;
CString name = _T("");
BOOL bProcess = TRUE;
// 建立一个新的CInternetSession 对象
pSession = new CInternetSession(AfxGetAppName(), 1,
PRE_CONFIG_INTERNET_ACCESS);
if (!pSession)
{
delete pSession;
return;
};
//
try
{
// 连接至FTP服务器
pConnection = pSession->GetFtpConnection(m_ftpserver, m_username, m_password, m_nPort);
}
catch (CInternetException* e)
{
// 将错误删除
e->Delete();
pConnection = 0;
};
//
if (pConnection != 0)
{
// 建立CFtpFileFind对象
pFile = new CFtpFileFind(pConnection);
bProcess = pFile->FindFile(_T("*"));
//
if (!bProcess)
// 如果搜索文件失败
{
pFile->Close();
pFile = 0;
};
//
while (bProcess)
{
bProcess = pFile->FindNextFile();
// 寻找下个符合的文件
name = pFile->GetFileName();
// 取得文件名称
if (pFile->IsDirectory())
// 如果是目录
{
name = _T("[") + name + _T("]");
};
m_pListbox->AddString(name);
};
//
if (pFile)
{
pFile->Close();
// 关闭文件
pFile = 0;
};
};
//
delete pFile;
if (pConnection)
{
pConnection->Close();
delete pConnection;
pConnection = 0;
};
delete pSession;
pSession = 0;
s.Restore();
};
//
void CFtpDlg::OnConnectbtn()
{
GetDlgItemText(IDC_FTPSERVER, m_ftpserver);
GetDlgItemText(IDC_USERNAME, m_username);
GetDlgItemText(IDC_PASSWORD, m_password);
m_nPort = GetDlgItemInt(IDC_PORTNUM);
ResetListContent();
};
//
void CFtpDlg::OnAnonymous()
{
if (!((CButton*)GetDlgItem(IDC_ANONYMOUS))->GetCheck())
{
EnableItem(TRUE);
GetDlgItem(IDC_USERNAME)->SetFocus();
m_bIsAnonymous = FALSE;
}
else
{
EnableItem();
m_bIsAnonymous = TRUE;
};
};
//
void CFtpDlg::OnFilebtn(UINT nID)
{
if (nID == IDC_SRCFILEBTN)
{
CFileDialog dlg(TRUE, NULL, NULL, OFN_HIDEREADONLY|OFN_FILEMUSTEXIST,
_T("所有文件|*.*||"), this);
if (dlg.DoModal() == IDOK)
{
m_srcfilename = dlg.GetPathName();
SetDlgItemText(IDC_SRCFILE, m_srcfilename);
};
}
else
{
CFileDialog dlg(FALSE, NULL, NULL,
OFN_HIDEREADONLY|OFN_OVERWRITEPROMPT,
_T("所有文件|*.*||"), this);
if (dlg.DoModal() == IDOK)
{
m_targetname = dlg.GetPathName();
SetDlgItemText(IDC_TARGETFILE, m_targetname);
};
if (m_bIsStartDownload)
GetFTPFile();
m_bIsStartDownload = FALSE;
};
};
//
void CFtpDlg::OnSelchangeList()
{
CButton *b1 = new CButton;
b1->Attach(GetDlgItem(IDC_DOWNLOADFILE)->m_hWnd);
if (!b1->GetCheck())
goto DeleteButton;
//
m_pListbox->GetText(m_pListbox->GetCurSel(), m_srcfilename);
if (m_srcfilename.Find(_T("[")))
SetDlgItemText(IDC_SRCFILE, m_srcfilename);
DeleteButton:
b1->Detach();
delete b1;
};
//
void CFtpDlg::OnDblclkList()
{
CButton *b1 = new CButton;
b1->Attach(GetDlgItem(IDC_DOWNLOADFILE)->m_hWnd);
if (b1->GetCheck())
// 如果目前的选项是下载
{
m_pListbox->GetText(m_pListbox->GetCurSel(), m_srcfilename);
if (m_srcfilename.Find(_T("[")))
// 如果所选择的名称不是目录的话,开始下载
{
m_bIsStartDownload = TRUE;
OnFilebtn(IDC_TARGETFILEBTN);
return;
};
};
// 切换目录
CInternetSession *pSession = 0;
CFtpConnection *pConnection = 0;
//
pSession = new CInternetSession(AfxGetAppName(), 1,
PRE_CONFIG_INTERNET_ACCESS);
if (!pSession)
return;
//
try
{
pConnection = pSession->GetFtpConnection(m_ftpserver, m_username, m_password, m_nPort);
}
catch(CInternetException *e)
{
e->Delete();
delete pConnection;
pConnection = 0;
delete pSession;
pSession = 0;
return;
};
//
CString dir;
pConnection->GetCurrentDirectory(dir);
// 取出目前的路径
CString tmp = m_srcfilename.Right(m_srcfilename.GetLength()-1), t;
tmp = tmp.Left(tmp.GetLength()-1);
t = tmp;
if (dir.GetLength() != 1 && dir.Compare(_T("/")))
tmp = dir + _T("/") + tmp;
else
tmp = dir + tmp;
if (!pConnection->SetCurrentDirectory(tmp))
// 设定新的路径
{
tmp = dir + _T("\\") + t;
if (!pConnection->SetCurrentDirectory(tmp))
MessageBox(_T("发生错误!无法切换目录!"), _T("错误"), MB_ICONSTOP);
};
pConnection->Close();
delete pConnection;
delete pSession;
ResetListContent();
};
//
void CFtpDlg::OnSendbtn()
{
if (m_srcfilename.IsEmpty())
return;
CInternetSession *pSession = 0;
CFtpConnection *pConnection = 0;
//
pSession = new CInternetSession(AfxGetAppName(), 1,
PRE_CONFIG_INTERNET_ACCESS);
if (!pSession)
return;
//
try
{
pConnection = pSession->GetFtpConnection(m_ftpserver, m_username, m_password, m_nPort);
}
catch(CInternetException *e)
{
e->Delete();
delete pConnection;
pConnection = 0;
delete pSession;
pSession = 0;
return;
};
//
CButton* b1 = new CButton;
b1->Attach(GetDlgItem(IDC_TRANSFERMODE1)->m_hWnd);
DWORD dwFlags = (b1->GetCheck()) ? FTP_TRANSFER_TYPE_ASCII: FTP_TRANSFER_TYPE_BINARY;
// 文件传输模式
CString dir;
pConnection->GetCurrentDirectory(dir);
if (dir.GetLength() != 1)
dir = dir + _T("/") + m_targetname;
else
dir += m_targetname;
if (pConnection->PutFile(m_srcfilename, dir, dwFlags))
// 上传文件至目前的目录下
MessageBeep(0xFFFF);
else
MessageBox(_T("上传文件失败!"), _T("错误"), MB_ICONSTOP);
b1->Detach();
delete b1;
pConnection->Close();
delete pConnection;
delete pSession;
ResetListContent();
};
//
void CFtpDlg::GetFTPFile()
{
CInternetSession *pSession = 0;
CFtpConnection *pConnection = 0;
//
pSession = new CInternetSession(AfxGetAppName(), 1,
PRE_CONFIG_INTERNET_ACCESS);
if (!pSession)
return;
//
try
{
pConnection = pSession->GetFtpConnection(m_ftpserver, m_username, m_password, m_nPort);
}
catch(CInternetException *e)
{
e->Delete();
delete pConnection;
pConnection = 0;
delete pSession;
pSession = 0;
return;
};
//
if (pConnection->GetFile(m_srcfilename, m_targetname))
// 从目前的目录中下载所指定的文件
MessageBeep(0xFFFF);
else
MessageBox(_T("文件存取失败!"), _T("错误"), MB_ICONSTOP);
pConnection->Close();
delete pConnection;
delete pSession;
};
//
void CFtpDlg::OnOK()
{
CDialog::OnOK();
};
//
BOOL CFtpDlg::PreTranslateMessage(MSG* pMsg)
{
if ((pMsg->message == WM_KEYDOWN) && (pMsg->wParam == VK_RETURN))
{
SendMessage(WM_COMMAND, IDC_CONNECTBTN, 0);
return TRUE;
};
return CDialog::PreTranslateMessage(pMsg);
};
//
class CFtpApp : public CWinApp
{
public:
CFtpApp() {};
//
BOOL InitInstance()
{
CDialog *dlg = new CFtpDlg;
m_pMainWnd = dlg;
dlg->DoModal();
//
return TRUE;
};
}theApp;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -