MFC 与FTP之间的交互

来源:互联网 发布:软件不兼容是什么意思 编辑:程序博客网 时间:2024/05/16 17:09
.h文件
#pragma once  #include <afxinet.h>  #include <vector>  #define FTP_DEFAULT_PORT 21  struct FileProperty  {  CString strFileName;  // FILE_ATTRIBUTE_DIRECTORY   // GetFileAttributes  DWORD dwFileType;  };  class CYWFtpSession  {  public:  CYWFtpSession(void);  ~CYWFtpSession(void);  // 设置FTP服务器地址、端口  void SetServerParam(CString strServerAddr, INTERNET_PORT wServerPort);  // 设置登录名和密码  void SetUserInfo(CString strUserName, CString strPassWord, BOOL bInitiativeMode = FALSE);  // 连接到指定FTP服务器  BOOL ConnectToServer();  // 得到指定目录下的所有文件  void GetFileList(CString strDirPath, std::vector<FileProperty> &vctFileList);  // 上传一个文件到FTP服务器  BOOL PutFileToServer(CString strLocalFile, CString strPutPath);  // 从FTP服务器下载一个文件  BOOL GetFileFromServer(CString strRemoteFile, CString strLocalPath, BOOL bFailIfExists = FALSE);  // 关闭连接  void CloseConnection();  // 检测对象是否存在  BOOL IsConnectionActive();  // 获取最后一个错误信息  CString GetLastErrorMessage();  private:  // FTP服务器地址  CString m_strServerAddr;  // 服务端口  INTERNET_PORT m_wServerPort;  // 登录用户名  CString m_strUserName;  // 登录密码  CString m_strPassWord;  // 被动模式-FALSE  主动模式-TRUE  BOOL m_bInitiativeMode;  // 记录最后一个错误信息  CString m_strLastErrorMsg;  // 创建并初始化一个或多个同时的Internet 会话  CInternetSession m_cInetSession;  // 管理与Internet服务器的FTP连接并允许直接操纵服务器中的目录和文件  CFtpConnection *m_pFtpConn;  };  


.cpp 文件 

#include "StdAfx.h"  #include "CMyFTPInteraction.h"  CYWFtpSession::CYWFtpSession(void)  {  m_pFtpConn = NULL;  m_strServerAddr.Empty();  m_wServerPort = FTP_DEFAULT_PORT;  m_strUserName.Empty();  m_strPassWord.Empty();  m_bInitiativeMode = FALSE;  m_strLastErrorMsg.Empty();  }  CYWFtpSession::~CYWFtpSession(void)  {  CloseConnection();  }  // 设置FTP服务器地址、端口  void CYWFtpSession::SetServerParam(CString strServerAddr, INTERNET_PORT wServerPort)  {  m_strServerAddr = strServerAddr;  m_wServerPort = wServerPort;  }  // 设置登录名和密码  void CYWFtpSession::SetUserInfo(CString strUserName, CString strPassWord, BOOL bInitiativeMode)  {  m_strUserName = strUserName;  m_strPassWord = strPassWord;  m_bInitiativeMode = bInitiativeMode;  }  // 连接到指定FTP服务器  BOOL CYWFtpSession::ConnectToServer()  {  try  {  CloseConnection();  // m_bInitiativeMode = TRUE 为被动模式  m_pFtpConn = m_cInetSession.GetFtpConnection(m_strServerAddr,   m_strUserName, m_strPassWord, m_wServerPort, m_bInitiativeMode);  if (NULL != m_pFtpConn)  return TRUE;  }  catch (CInternetException *e)  {  e->Delete();  }  return FALSE;  }  // 得到指定目录下的所有文件  void CYWFtpSession::GetFileList(CString strDirPath, std::vector<FileProperty> &vctFileList)  {  if (NULL == m_pFtpConn  && !ConnectToServer())  return ;  vctFileList.empty();  CFtpFileFind cFtpFileFind(m_pFtpConn);  // 查找指定目录  BOOL bWorking = cFtpFileFind.FindFile(strDirPath);  while (bWorking)  {  bWorking = cFtpFileFind.FindNextFile();  // .或者..  if (cFtpFileFind.IsDots())  continue;  FileProperty sOneFile;  // 得到文件名  sOneFile.strFileName = cFtpFileFind.GetFileName();  sOneFile.dwFileType = 0;  // 文件夹  if (cFtpFileFind.IsDirectory())  sOneFile.dwFileType |= FILE_ATTRIBUTE_DIRECTORY;  else  sOneFile.dwFileType |= FILE_ATTRIBUTE_NORMAL;   // 普通文件  // 添加进文件列表  vctFileList.push_back(sOneFile);  }  }  // 上传一个文件到FTP服务器  BOOL CYWFtpSession::PutFileToServer(CString strLocalFile, CString strPutPath)  {  if (NULL == m_pFtpConn  && !ConnectToServer())  return FALSE;  return m_pFtpConn->PutFile(strLocalFile, strPutPath);  }  // 从FTP服务器下载一个文件  BOOL CYWFtpSession::GetFileFromServer(CString strRemoteFile, CString strLocalPath, BOOL bFailIfExists)  {  if (NULL == m_pFtpConn  && !ConnectToServer())  return FALSE;  return m_pFtpConn->GetFile(strRemoteFile, strLocalPath, bFailIfExists);  }  // 关闭连接  void CYWFtpSession::CloseConnection()  {  if (NULL != m_pFtpConn)  {  m_pFtpConn->Close();  delete m_pFtpConn;  }  m_pFtpConn = NULL;  }  // 检测对象是否存在  BOOL CYWFtpSession::IsConnectionActive()  {  if (NULL == m_pFtpConn)  return FALSE;  return TRUE;  }  // 获取最后一个错误信息  CString CYWFtpSession::GetLastErrorMessage()  {  m_strLastErrorMsg.Format(_T("错误码:%ld"), ::GetLastError());  return m_strLastErrorMsg;  }  

用法示例

CYWFtpSession Ftp;Ftp.SetServerParam(_T("222.241.151.189"),FTP_DEFAULT_PORT);Ftp.SetUserInfo(_T("账号"),_T("密码"));if (!Ftp.PutFileToServer(_T("本地文件地址"),_T("/other/1.apk"))) // 后面的是服务器地址后面要添加上传之后的文件名{AfxMessageBox(Ftp.GetLastErrorMessage());}Ftp.CloseConnection()



0 0
原创粉丝点击