码迷,mamicode.com
首页 > Web开发 > 详细

FTP客户端上传下载实现

时间:2015-12-08 17:46:57      阅读:346      评论:0      收藏:0      [点我收藏+]

标签:

1、第一次感觉MS也有这么难用的MFC类;

2、CFtpFileFind类只能实例化一个,多个实例同时查找会出错(因此下载时不能递归);

3、本程序支持文件夹嵌套上传下载;

4、boost::filesystem::create_directory不能递归创建文件夹,需手动实现

 

代码如下:

CFtpClient.h

 1 #ifndef __ftp_client_h__
 2 #define __ftp_client_h__
 3 
 4 #include <afxinet.h>
 5 #include <iostream>
 6 #include <sstream>
 7 #include <queue>
 8 #include <boost/filesystem/operations.hpp>
 9 #include <boost/filesystem/path.hpp>
10 
11 class CFtpClient
12 {
13 public:
14     CFtpClient();
15     ~CFtpClient();
16 
17     bool Connect();
18     bool Close();
19     int Download(std::string strServerDir = "\\", std::string strFilename = "*");
20     int Upload(const std::string strLocalPath);
21 private:
22     //下载文件夹
23     int DownloadFolder();
24     //下载文件
25     int DownloadFile(std::string strServerDir, std::string strFilename);
26     //创建多级目录
27     void CreateDirectory(boost::filesystem::path path);
28 private:
29     CInternetSession* m_pInternetSession;
30     CFtpConnection* m_pFtpConnection;
31 
32     //参数配置
33     std::string m_strServerAddress;               //FTP:服务器地址
34     int m_nServerPort;                            //FTP:服务器端口号
35     std::string m_strUsername;                    //FTP:登录用户名
36     std::string m_strPassword;                    //FTP:登录密码
37     //上传目录
38     std::string m_strUploadServerDir;             //上传服务器存储目录
39     //下载目录
40     std::string m_strDownloadLocalDir;            //下载本地存储目录
41 
42     std::queue<std::string> queServerDir;
43 };
44 
45 
46 #endif

CFtpClient.cpp

  1 // 模块名称:
  2 // 模块描述:FTP上传下载类
  3 // 开发作者:可笑痴狂
  4 // 创建日期:2015-12-04
  5 // 模块版本:1.0.0.0
  6 //------------------------------------------------------------------------------
  7 
  8 #include <string>
  9 #include <iostream>
 10 #include <sstream>
 11 #include "CFtpClient.h"
 12 #include <boost/assert.hpp>
 13 #include <boost/lexical_cast.hpp>
 14 #include <boost/foreach.hpp>
 15 
 16 
 17 CFtpClient::CFtpClient()
 18 {
 19     m_pInternetSession = NULL;
 20     m_pFtpConnection = NULL;
 21     m_strServerAddress = "127.0.0.1";
 22     m_strUsername = "123";
 23     m_strPassword = "123";
 24     m_nServerPort = 21;
 25     m_strDownloadLocalDir = "D:\\DownloadLocal";
 26     m_strUploadServerDir = "\\";
 27 }
 28 
 29 CFtpClient::~CFtpClient()
 30 {
 31     this->Close();
 32 }
 33 
 34 bool CFtpClient::Connect()
 35 {
 36     if (m_pInternetSession != NULL)
 37     {
 38         return true;
 39     }
 40 
 41     m_pInternetSession = new CInternetSession("OTC_FTP_Client");
 42     int nCnt = 1;
 43     while(nCnt++)
 44     {
 45         try
 46         {
 47             m_pFtpConnection = m_pInternetSession->GetFtpConnection(m_strServerAddress.c_str(), m_strUsername.c_str(), m_strPassword.c_str(), 
                                          m_nServerPort);
48 } 49 catch(CInternetException *pEx) 50 { 51 char szErrMsg[1024+1] = {0}; 52 if (pEx->GetErrorMessage(szErrMsg, 1024)) 53 { 54 std::cout << "连接FTP服务器失败, ErrMsg:" << szErrMsg << std::endl; 55 if (nCnt > 5) 56 { 57 return false; 58 } 59 std::cout << "正在尝试重新连接[" << nCnt-1 << "]......" << std::endl; 60 } 61 continue; 62 } 63 return true; 64 } 65 66 return true; 67 } 68 69 bool CFtpClient::Close() 70 { 71 if (NULL != m_pInternetSession) 72 { 73 m_pInternetSession->Close(); 74 delete m_pInternetSession; 75 m_pInternetSession = NULL; 76 } 77 if (NULL != m_pFtpConnection) 78 { 79 m_pFtpConnection->Close(); 80 delete m_pFtpConnection; 81 m_pFtpConnection = NULL; 82 } 83 84 return true; 85 } 86 int CFtpClient::Download(std::string strServerDir /*="\\"*/, std::string strFilename /*= "*"*/) 87 { 88 if ("*" == strFilename) 89 { 90 queServerDir.push(strServerDir); 91 return this->DownloadFolder(); 92 } 93 else 94 { 95 return this->DownloadFile(strServerDir, strFilename); 96 } 97 98 } 99 int CFtpClient::DownloadFile(std::string strServerDir, std::string strFilename) 100 { 101 int nRet = 0; 102 CFtpFileFind* pFtpFileFind = NULL; 103 if(!this->Connect()) 104 { 105 nRet = -1; 106 goto __end; 107 } 108 else 109 { 110 boost::filesystem::path LocalPath(m_strDownloadLocalDir); 111 if (!boost::filesystem::exists(LocalPath)) 112 { 113 this->CreateDirectory(LocalPath); 114 } 115 if (0 == m_pFtpConnection->SetCurrentDirectory(strServerDir.c_str())) 116 { 117 nRet = -1; 118 std::cout << "设置服务器下载目录[" << strServerDir << "]失败!ErrCode=" << GetLastError() << std::endl; 119 DWORD dw = 0; 120 char szBuf[512]={0}; 121 DWORD dwLen = sizeof(szBuf)-1; 122 InternetGetLastResponseInfo(&dw, szBuf, &dwLen); 123 std::cout << "错误信息:" << szBuf << std::endl; 124 goto __end; 125 } 126 if (pFtpFileFind == NULL) 127 { 128 pFtpFileFind = new CFtpFileFind(m_pFtpConnection); 129 } 130 int nRet = pFtpFileFind->FindFile(strFilename.c_str()); 131 if(nRet) 132 { 133 nRet = pFtpFileFind->FindNextFile(); 134 135 std::string strLocalFilename = m_strDownloadLocalDir + "\\" + strFilename; 136 std::string strServerFilename = strFilename; 137 if(m_pFtpConnection->GetFile(strServerFilename.c_str(), strLocalFilename.c_str(), FALSE)) 138 std::cout << "下载文件[" << strServerFilename << "]到[" << strLocalFilename << "]成功!" << std::endl; 139 else 140 { 141 std::cout << "下载文件[" << strServerFilename << "]到[" << strLocalFilename << "]失败!" << std::endl; 142 char szBuf[512]={0}; 143 DWORD dw = 0; 144 DWORD dwLen = sizeof(szBuf)-1; 145 InternetGetLastResponseInfo(&dw, szBuf, &dwLen); 146 dw = GetLastError(); 147 std::cout << "错误信息:" << szBuf << std::endl; 148 nRet = -1; 149 } 150 } 151 } 152 __end: 153 this->Close(); 154 if (pFtpFileFind) 155 { 156 pFtpFileFind->Close(); 157 delete pFtpFileFind; 158 } 159 160 return nRet; 161 } 162 int CFtpClient::DownloadFolder() 163 { 164 std::string strServerDir; 165 std::string strFilename; 166 CFtpFileFind* pFtpFileFind = NULL; 167 int nRet = 0; 168 169 if(!this->Connect()) 170 { 171 nRet = -1; 172 goto __end; 173 } 174 else 175 { 176 while(!queServerDir.empty()) 177 { 178 strServerDir = queServerDir.front(); 179 queServerDir.pop(); 180 181 boost::filesystem::path LocalPath(m_strDownloadLocalDir); 182 if (!boost::filesystem::exists(LocalPath / strServerDir)) 183 { 184 this->CreateDirectory(LocalPath / strServerDir); 185 } 186 187 if (0 == m_pFtpConnection->SetCurrentDirectory(strServerDir.c_str())) 188 { 189 nRet = -1; 190 std::cout << "设置服务器下载目录[" << strServerDir << "]失败!ErrCode=" << GetLastError() << std::endl; 191 DWORD dw = 0; 192 char szBuf[512]={0}; 193 DWORD dwLen = sizeof(szBuf)-1; 194 InternetGetLastResponseInfo(&dw, szBuf, &dwLen); 195 std::cout << "错误信息:" << szBuf << std::endl; 196 goto __end; 197 } 198 if (pFtpFileFind == NULL) 199 { 200 pFtpFileFind = new CFtpFileFind(m_pFtpConnection); 201 } 202 203 int nRet = pFtpFileFind->FindFile(); 204 while(nRet) 205 { 206 nRet = pFtpFileFind->FindNextFile(); 207 strFilename = pFtpFileFind->GetFilePath(); 208 if (pFtpFileFind->IsDirectory()) 209 { 210 queServerDir.push(std::string(pFtpFileFind->GetFilePath())); 211 continue; 212 } 213 214 std::string strLocalFilename = m_strDownloadLocalDir + strFilename; 215 std::string strServerFilename = strFilename; 216 217 if(m_pFtpConnection->GetFile(strServerFilename.c_str(), strLocalFilename.c_str(), FALSE)) 218 std::cout << "下载文件[" << strServerFilename << "]到[" << strLocalFilename << "]成功!" << std::endl; 219 else 220 { 221 std::cout << "下载文件[" << strServerFilename << "]到[" << strLocalFilename << "]失败!" << std::endl; 222 char szBuf[512]={0}; 223 DWORD dw = 0; 224 DWORD dwLen = sizeof(szBuf)-1; 225 InternetGetLastResponseInfo(&dw, szBuf, &dwLen); 226 dw = GetLastError(); 227 std::cout << "错误信息:" << szBuf << std::endl; 228 nRet = -1; 229 } 230 } 231 } 232 } 233 __end: 234 this->Close(); 235 if (pFtpFileFind != NULL) 236 { 237 pFtpFileFind->Close(); 238 delete pFtpFileFind; 239 } 240 return nRet; 241 } 242 243 int CFtpClient::Upload(const std::string strLocalPath) 244 { 245 int nRet = 0; 246 CFtpFileFind* pFtpFileFind = NULL; 247 const boost::filesystem::path& localPath(strLocalPath); 248 249 if(!this->Connect()) 250 { 251 nRet = -1; 252 goto __end; 253 } 254 else 255 { 256 if (0 == m_pFtpConnection->SetCurrentDirectory(m_strUploadServerDir.c_str())) 257 { 258 nRet = -1; 259 std::cout << "设置服务器上传目录[" << m_strUploadServerDir << "]失败!ErrCode=" << GetLastError() << std::endl; 260 DWORD dw = 0; 261 char szBuf[512]={0}; 262 DWORD dwLen = sizeof(szBuf)-1; 263 InternetGetLastResponseInfo(&dw, szBuf, &dwLen); 264 std::cout << "错误信息:" << szBuf << std::endl; 265 goto __end; 266 } 267 268 if (boost::filesystem::exists(localPath)) 269 { 270 if (boost::filesystem::is_directory(localPath)) 271 { 272 m_pFtpConnection->CreateDirectory(localPath.leaf().string().c_str()); 273 274 boost::filesystem::directory_iterator itor_begin(localPath); 275 boost::filesystem::directory_iterator itor_end; 276 277 std::string strServerDir = m_strUploadServerDir; 278 for (; itor_begin != itor_end; itor_begin++) 279 {//回溯算法 280 m_strUploadServerDir += std::string("\\") + localPath.leaf().string(); 281 this->Upload(itor_begin->path().string()); 282 m_strUploadServerDir = strServerDir; 283 } 284 } 285 else 286 { 287 if(m_pFtpConnection->PutFile(localPath.string().c_str(), localPath.leaf().string().c_str(), 288 FTP_TRANSFER_TYPE_BINARY,0)) 289 std::cout << "上传文件[" << localPath.leaf().string() << "]成功!" << std::endl; 290 else 291 { 292 DWORD dw0 = GetLastError(); 293 if (dw0) 294 { 295 char szBuf[512]={0}; 296 DWORD dw = 0; 297 DWORD dwLen = sizeof(szBuf)-1; 298 InternetGetLastResponseInfo(&dw, szBuf, &dwLen); 299 std::cout << "上传文件[" << localPath.leaf().string() << "]失败!ErrCode:" << dw0 << "ErrMsg:" << szBuf << std::endl; 300 goto __end; 301 } 302 } 303 } 304 } 305 else 306 { 307 std::cout << "指定上传文件不存在!" << std::endl; 308 nRet = -1; 309 goto __end; 310 } 311 } 312 313 __end: 314 if (pFtpFileFind != NULL) 315 { 316 pFtpFileFind->Close(); 317 delete pFtpFileFind; 318 } 319 this->Close(); 320 return nRet; 321 } 322 323 void CFtpClient::CreateDirectory(boost::filesystem::path path) 324 { 325 boost::filesystem::path::iterator itor = path.begin(); 326 boost::filesystem::path pathTmp; 327 while(itor != path.end()) 328 { 329 pathTmp /= (*itor).string(); 330 if (!pathTmp.empty() && !boost::filesystem::exists(pathTmp)) 331 { 332 boost::filesystem::create_directory(pathTmp); 333 } 334 ++itor; 335 } 336 }

 

FTP客户端上传下载实现

标签:

原文地址:http://www.cnblogs.com/dongsheng/p/5029561.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!