/********************************************************************
* File: FtpClient.h, interface for the CFtpClient class.
* Author: Wen Xiaoyong(Wenxy), 20050111,a.m.,
[email protected]
* Copyright: (C) Wenxy,2005
* Fix history:
*
********************************************************************/
#if !defined(AFX_FTPCLIENT_H__6D565AC0_46D0_4E18_B1F6_3663A9F9AAF2__INCLUDED_)
#define AFX_FTPCLIENT_H__6D565AC0_46D0_4E18_B1F6_3663A9F9AAF2__INCLUDED_
#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000
// CFtipClient class
#include <stdio.h>
#include <afx.h>
#include <afxwin.h>
#include <afxinet.h>
class CFtpClient
{
public:
bool GetFtpFileList(); // 获取FTP服务器上的文件列表
bool DownloadFile(CString &strRemoteFile, CString &strLocalFile); // 下载文件
bool LookFtpServerInfo(); // 查看FTP服务器的升级信息
CFtpClient(CString strFtpIp);
virtual ~CFtpClient();
protected:
CInternetSession m_sess; // CInternetSession对象
CFtpConnection *m_pConnect; // CFtpConnection对象指针
CFtpFileFind *m_pFinder; // CFtpFileFind对象指针
CString m_strFtpServer; // ftp server IP
CString m_strLocalFile; // local file
CString m_strRemoteFile; // remore file
int m_nErrorCode; // error code
CString m_strErrorMsg; // error massage
};
#endif // !defined(AFX_FTPCLIENT_H__6D565AC0_46D0_4E18_B1F6_3663A9F9AAF2__INCLUDED_)
/********************************************************************
* File: FtpClient.cpp: implementation of the CFtpClient class.
* Author: Wen Xiaoyong(Wenxy), 20050111,a.m.,
[email protected]
* Copyright: (C) Wenxy,2005
* Fix history:
*
********************************************************************/
#include "stdafx.h"
#include "FtpClient.h"
// Construction/Destruction
CFtpClient::CFtpClient(CString strFtpIp):m_sess(_T("MyProgram/1.0"))
{
m_pConnect = NULL;
m_pFinder = NULL;
m_strFtpServer = strFtpIp;
m_strLocalFile = "";
m_strRemoteFile = "";
}
CFtpClient::~CFtpClient()
{
if (m_pConnect != NULL)
{
m_pConnect->Close();
delete m_pConnect;
}
if (m_pFinder != NULL)
{
delete m_pFinder;
}
}
bool CFtpClient::LookFtpServerInfo()
{
// create a session object to initialize WININET library
// Default parameters mean the access method in the registry
// (that is, set by the "Internet" icon in the Control Panel)
// will be used.
try
{
// Request a connection to ftp.microsoft.com. Default
// parameters mean that we'll try with username = ANONYMOUS
// and password set to the machine name @ domain name
m_pConnect = m_sess.GetFtpConnection(m_strFtpServer);
// use a file find object to enumerate files
m_pFinder = new CFtpFileFind (m_pConnect);
}
catch (CInternetException* pEx)
{
TCHAR sz[1024];
pEx->GetErrorMessage(sz, 1024);
printf("ERROR! %s/n", sz);
pEx->Delete();
return false;
}
return true;
}
// 下载文件
bool CFtpClient::DownloadFile(CString &strRemoteFile, CString &strLocalFile)
{
try
{
// download file
if(0 == m_pConnect->GetFile(strRemoteFile, strLocalFile))
{
m_nErrorCode = GetLastError();
printf("Error: 获取文件[%s]失败![Error=%d], ", strRemoteFile, m_nErrorCode);
switch(m_nErrorCode)
{
case 3:
m_strErrorMsg = "系统找不到路径";
break;
case 80:
m_strErrorMsg = "本地磁盘已存在此文件";
break;
case 12003:
m_strErrorMsg = "FTP服务器上不存在此文件";
break;
}
printf("%s[%s]/n/n", m_strErrorMsg, strLocalFile);
return false;
}
else
{
printf("Note: 从FTP服务器下载文件[%s]成功./n", strRemoteFile);
}
}
catch (...)
{
printf("Error: download file[%s] failed./n", strRemoteFile);
}
return true;
}
// 获取FTP服务器上的文件列表
bool CFtpClient::GetFtpFileList()
{
// get file list in ftp server
// start looping
BOOL bWorking = m_pFinder->FindFile(_T("*"));
if (false == bWorking)
{
return false;
}
while (bWorking)
{
bWorking = m_pFinder->FindNextFile();
printf("%s/n", (LPCTSTR) m_pFinder->GetFileURL());
}
return true;
}