现在的位置: 首页 > 综合 > 正文

我的win32 FTP 类。。。

2013年09月08日 ⁄ 综合 ⁄ 共 3188字 ⁄ 字号 评论关闭
#pragma once
#include "stdafx.h"
#include <stdio.h>
#include <WinInet.h>
#pragma comment(lib, "wininet.lib")
class CFtpTransfer
{
public:
	CString m_ftpPath;
	CString m_localPath;
	CFtpTransfer(void);
	BOOL Login();
	void Logout();

	BOOL CreateRemoteDir(CString DirName );
	BOOL DeleteRemoteDir(CString DirName );

	BOOL Upload(CString localFilePath,CString localFileName);
	BOOL Download(CString remoteFile, CString localFile);

	BOOL DeleteRemoteFile( CString fileName );

	BOOL UploadAll();
	CString GetLastError();

	CString GetFtpPath();
public:
	~CFtpTransfer(void);
private:
	void SetFtpPath();
	CString m_serverHost;
	CString m_serverName;
	CString m_serverPass;
	CString m_serverFtpDir;

	HINTERNET m_hInternet;
	HINTERNET m_hFtpSession;
// 	CInternetSession *m_pInternetSession;
// 	CFtpConnection *m_pFtpConnection;
};

#include "CFtpTransfer.h"

CFtpTransfer::CFtpTransfer(void)
{
	m_hInternet = NULL;
	m_hFtpSession = NULL;

	m_serverHost = FtpAdress;
	m_serverName = FtpUserName;
	m_serverPass =FtppassWord;
	m_serverFtpDir = _T("zhaopin");
}

CFtpTransfer::~CFtpTransfer(void)
{
}

BOOL CFtpTransfer::Login()
{
	bool result = false;
	try{
		//m_hInternet = InternetOpen(NULL,INTERNET_OPEN_TYPE_DIRECT,NULL,NULL,0);
		m_hInternet = InternetOpen(_T("InetURL/1.0"), INTERNET_OPEN_TYPE_PRECONFIG, NULL, NULL, 0);
			if (m_hInternet != NULL)
			{
				//m_hFtpSession = InternetConnect(m_hInternet,m_serverHost, INTERNET_DEFAULT_FTP_PORT,m_serverName,m_serverPass,INTERNET_SERVICE_FTP, 0, 0);		
				m_hFtpSession = InternetConnect(m_hInternet,m_serverHost, INTERNET_DEFAULT_FTP_PORT,m_serverName,m_serverPass,INTERNET_SERVICE_FTP,INTERNET_FLAG_PASSIVE, 0);
				if(m_hFtpSession != NULL)//链接成功
				{
					result = true;
					SetFtpPath();
				}
			}		
	}
	catch(runtime_error e)
	{
		 result = false;
	}
	catch(...)
	{
		 result = false;
	}

	return result;
}

void CFtpTransfer::Logout()
{
	if (m_hFtpSession != NULL)
	{
		InternetCloseHandle(m_hFtpSession);
		if (m_hInternet != NULL)
		{
			InternetCloseHandle(m_hInternet);
		}
	}
}

CString CFtpTransfer::GetFtpPath()
{
	// \\zhaopin\\2012-09-07(11-37-31-328)\

	return m_ftpPath;
}

void CFtpTransfer::SetFtpPath()
{
	SYSTEMTIME sys;
	GetLocalTime(&sys);
	CString directoryName;
	directoryName.Format(_T("%4d-%02d-%02d(%02d-%02d-%02d-%03d)"),sys.wYear,sys.wMonth,sys.wDay,sys.wHour,sys.wMinute, sys.wSecond,sys.wMilliseconds); 
	m_ftpPath = _T("\\") +m_serverFtpDir + _T("\\") + directoryName + _T("\\");
	//   \\zhaopin\\2012-09-07(11-28-26-468)
	CreateRemoteDir(m_ftpPath);
}

BOOL CFtpTransfer::CreateRemoteDir(CString DirName )
{
	if (m_hFtpSession != NULL)
	{
		return FtpCreateDirectory(m_hFtpSession,DirName);
	}
	return FALSE;
}

BOOL CFtpTransfer::DeleteRemoteDir(CString DirName )
{
	if (m_hFtpSession != NULL)
	{
		return FtpRemoveDirectory(m_hFtpSession,DirName);
	}
	return FALSE;
}

BOOL CFtpTransfer::Upload(CString localFilePath,CString localFileName)
{
	if(m_hFtpSession != NULL)
	{
		CString uploadPath = m_ftpPath+ _T("\\") + localFileName;
		//\\zhaopin\\2012-09-07(11-28-26-468)\陈鹏 121024082_cn.htm
		return FtpPutFile(m_hFtpSession,localFilePath,uploadPath,FTP_TRANSFER_TYPE_BINARY,1);
	}
	return FALSE;
}

BOOL CFtpTransfer::Download(CString remoteFile, CString localFile)
{
	if(m_hFtpSession != NULL)
	{
		//return FtpGetFile(m_hFtpSession,m_localPath+,m_ftpPath + localFileName,FTP_TRANSFER_TYPE_BINARY,1))
	}

	return false;
}

BOOL CFtpTransfer::DeleteRemoteFile( CString fileName )
{
	if (m_hFtpSession != NULL)
	{
		return FtpDeleteFile(m_hFtpSession,fileName);
	}
	return FALSE;
}

BOOL CFtpTransfer::UploadAll()
{
	return false;
}

CString CFtpTransfer::GetLastError()
{
	return "";
}

抱歉!评论已关闭.