当前位置: 代码迷 >> 综合 >> FTP下载类, windows平台下对CFtpConnection上传下载的封装类
  详细解决方案

FTP下载类, windows平台下对CFtpConnection上传下载的封装类

热度:51   发布时间:2024-01-20 00:26:56.0

以前一直没找到CFtpConnection的简单封装类,所以今天自己写了一个,供有需要的朋友使用

 

该类只是简单的封装,不可能达到socket来实现FTP功能的灵活和性能。

你也可以自己改造,如类里加入线程启动下载,加入上传下载通知消息等,因为我用不上,所以就不加了

使用HTTP上传下载的方法也类似,可以做为参考

 

使用说明:

 

 

CMyFtp ftpTest("192.168.16.63", "test", "test112", 21);

 

bool result = ftpTest.UploadFile("336.txt","d://111.txt");

if(!result)

AfxMessageBox(ftpTest.GetErrorMsg());

 

ftpTest.DownloadFile( "336.txt", "12.txt");

 

ftpTest.CreateRemoteDirectory("test222");

 

ftpTest.SetRemoteDir("test222");

 

ftpTest.UploadFile("113.txt","d://111.txt");

 

ftpTest.SetRemoteDir("//");

 

ftpTest.UploadFile("116.txt","d://111.txt");

 

 

.h文件

// MyFtpClient.h: interface for the CMyFtpClient class.
//
//#include <afxinet.h>#if !defined(AFX_MYFTPCLIENT_H__A9FC4CC7_20CE_4700_B93F_8381A3E3F5C5__INCLUDED_)
#define AFX_MYFTPCLIENT_H__A9FC4CC7_20CE_4700_B93F_8381A3E3F5C5__INCLUDED_#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000class CMyFtpClient  
{
public:CMyFtpClient();CMyFtpClient(CString str_ip, CString str_user, CString str_pwd, UINT port);virtual ~CMyFtpClient();void setParm(CString str_ip, CString str_user, CString str_pwd, UINT port);void setPassive(BOOL state){ m_passive = state;}//设定FTP Passivestatic bool login();//登录bool logout();//退出//上传文件static DWORD WINAPI UploadFile(LPVOID lpParameter);bool UploadFileThread(CString str_remoteFileName/*远程文件名,必须是当前路径下的文件*/,CString str_LocalFileName/*本地文件名,全路径*/);//上传文件列表,参数参考 UploadFilebool UploadFileList(CStringList str_remoteFileNameList, CStringList str_LocalFileList);//上传//设置远程当前路径bool SetRemoteDir(CString str_remoteDir);//设置远程当前路径为根目录bool SetRemoteRoot(){ return SetRemoteDir(_T("/"));}//得到远程当前路径CString GetRemoteDir(){  m_pConnect->GetCurrentDirectory(m_remoteCurrentDir); return m_remoteCurrentDir; }//创建远程目录,不会改变当前远程路径bool CreateRemoteDir(CString str_dirName);//删除远程目录及目录下的子文件,子文件夹bool DelRemoteDir(CString str_dirName);//删除远程文件bool DelRemoteFile(CString str_FileName);//重命名远程文件bool RenameRemoteFile(CString str_OldFileName,CString str_NewFileName);//下载远程文件bool DownloadFile(CString str_remoteFileName/*远程文件名*/, CString str_LocalFileName/*本地文件名*/);//下载远程文件列表bool DownloadFileList(CStringList str_remoteFileNameList, CStringList str_LocalFileList);//得到错误信息CString GetErrorMsg();//上传进度百分比DWORD GetUploadFilePercent();//上传速度 bytes/sDWORD  GetUploadSpeed();//下载进度百分比DWORD GetDownloadFilePercent();//下载速度 bytes/sDWORD  GetDownloadSpeed();//设定文件每次读写大小,网速越快,值设置越大void SetFileStepSize(DWORD dSize){ m_FileLenStep = dSize*1024; }// 取得目录下的所有文件void GetFileInPath(LPCTSTR lpszPath, CStringArray &arrstrFile, BOOL bFindSubDir/* = FALSE*/,LPCTSTR lpszFileExt);private:static BOOL m_passive;static CString m_errorMsg;static BOOL m_bLogin;CString m_remoteCurrentDir;static CFile m_CFile;static CInternetSession m_session;static CFtpConnection * m_pConnect;static CInternetFile * m_pRemoteFile;static CString m_Ip,m_user,m_pwd;static UINT m_port;static DWORD m_FileLenStep;static DWORD m_TotalUploadSize, m_TotalDownloadSize;static DWORD m_CurUploadSize, m_CurDownloadSize;DWORD m_UploadSpeed, m_DownloadSpeed
  相关解决方案