Chinaunix首页 | 论坛 | 博客
  • 博客访问: 43817
  • 博文数量: 21
  • 博客积分: 1425
  • 博客等级: 上尉
  • 技术积分: 215
  • 用 户 组: 普通用户
  • 注册时间: 2008-07-29 21:56
文章分类

全部博文(21)

文章存档

2009年(19)

2008年(2)

我的朋友

分类: C/C++

2009-09-11 09:14:52

#include "stdafx.h"
#define _AFXDLL
#include
#include
#include
#include
#include
#pragma comment(lib, "ws2_32.lib")
BOOL SendFtpFile(CString ftpSite, CString user, CString password, int port, CString localFile, CString remoteFile)
{
 if (!AfxWinInit(::GetModuleHandle(NULL), NULL, ::GetCommandLine(), 0))
      return FALSE;// catastropic error! MFC can't initialize
 CInternetSession sess(_T("SendFile"));
 CFtpConnection* pConnect = NULL;
 try
 {
  pConnect = sess.GetFtpConnection(ftpSite, user, password, port, FALSE);//ftp.gnu.org  //ftp.microsoft.com
  /*
  //此处注释是获取ftp服务器的文件列表
  CFtpFileFind finder(pConnect);// use a file find object to enumerate files
  BOOL bWorking = finder.FindFile(_T("*"));// start looping
  while (bWorking)
  {
     bWorking = finder.FindNextFile();
     //printf("%s\n", finder.GetFileURL());
  }
  */
  BOOL putFileRes = FtpPutFile(
   HINTERNET(*pConnect),  //HINTERNET hConnect,
   localFile,   //LPCTSTR lpszLocalFile,
   remoteFile,   //LPCTSTR lpszNewRemoteFile,
   FTP_TRANSFER_TYPE_BINARY, //DWORD dwFlags,
   NULL      //DWORD_PTR dwContext
   );
  if (putFileRes == FALSE)
  {
    //printf("putFileRes = FALSE : %d\n", GetLastError());
    if (pConnect != NULL)
    {
      pConnect->Close();
      delete pConnect;
    }
   return FALSE;
  }
 }
 catch (CInternetException* pEx)
 {
   TCHAR sz[1024];
   pEx->GetErrorMessage(sz, 1024);
   //printf_s("ERROR!  %s\n", sz);
   pEx->Delete();
 }
 // if the connection is open, close it
 if (pConnect != NULL)
 {
   pConnect->Close();
   delete pConnect;
 }
 return TRUE;
}
 
 
BOOL GetFtpFile(CString ftpSite, CString user, CString password, int port, CString localFile, CString remoteFile)
{
  if (!AfxWinInit(::GetModuleHandle(NULL), NULL, ::GetCommandLine(), 0))
      return -1;// catastropic error! MFC can't initialize
 CInternetSession sess(_T("GetFile"));
 CFtpConnection* pConnect = NULL;
 try
 {
   pConnect = sess.GetFtpConnection(ftpSite, user, password, port, FALSE);//ftp.gnu.org
   /*
   // use a file find object to enumerate files
   CFtpFileFind finder(pConnect);
   // start looping
   BOOL bWorking = finder.FindFile(_T("*"));
   while (bWorking)
   {
     bWorking = finder.FindNextFile();
     //CString tmpStr = finder.GetFileURL();
     printf("%s\n", finder.GetFileURL());
   }
  */
  BOOL getFileRes = FtpGetFile(
   HINTERNET(*pConnect),  //HINTERNET hConnect,
   remoteFile,            //LPCTSTR lpszRemoteFile,
   localFile,             //LPCTSTR lpszNewFile,
   FALSE,                 //BOOL fFailIfExists,
   FILE_ATTRIBUTE_NORMAL,     //DWORD dwFlagsAndAttributes,
   FTP_TRANSFER_TYPE_BINARY,  //DWORD dwFlags,
   NULL                       //DWORD_PTR dwContext
   );
  if (getFileRes == FALSE)
  {
    pConnect->Close();
    delete pConnect;
    return FALSE;
    //printf("getFileRes = FALSE : %d\n", GetLastError());
  }
 }
 catch (CInternetException* pEx)
 {
   TCHAR sz[1024];
   pEx->GetErrorMessage(sz, 1024);
   //printf_s("ERROR!  %s\n", sz);
   pEx->Delete();
 }
 // if the connection is open, close it
 if (pConnect != NULL)
 {
   pConnect->Close();
   delete pConnect;
 }
 return TRUE;
}
 
//测试
int _tmain(int argc, _TCHAR* argv[])
{
  SendFtpFile("192.168.0.214", "user", "123456", 21, "d:\\Se7en.htm", "a\\Se7en.htm");
  GetFtpFile("192.168.0.214", "user", "123456", 21, "F:\\Se7en.htm", "a\\Se7en.htm");
 return 0;
}
阅读(694) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~