📄 ftp.cpp
字号:
// Ftp.cpp: implementation of the CFtp class.
//
//////////////////////////////////////////////////////////////////////
#include "stdafx.h"
#include "DynamicIP.h"
#include "Ftp.h"
#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
CFtp::CFtp()
{
// get the name of the app
strAppName.LoadString(AFX_IDS_APP_TITLE);
// create an internet session
pInetSession = new CInternetSession(strAppName, INTERNET_OPEN_TYPE_PRECONFIG);
// if Not good, show message + return
// should never failed anyway
if(!pInetSession){
AfxMessageBox("Can't start Internet session");
return;
}
}
CFtp::~CFtp()
{
// close the internet session
pInetSession->Close();
// delete the session
if(pInetSession != NULL)
delete pInetSession;
}
bool CFtp::ConnectToServer(LPCTSTR strServerName, LPCTSTR strUserName, LPCTSTR strPassword)
{
if(*strServerName == '\0'){
AfxMessageBox("FTP Server name missing.", MB_OK|MB_ICONERROR);
return 0;
}
if(*strUserName == '\0'){
AfxMessageBox("User name is missing.", MB_OK|MB_ICONERROR);
return 0;
}
if(*strPassword == '\0'){
AfxMessageBox("Password missing.", MB_OK|MB_ICONERROR);
return 0;
}
try {
// try to connect to a ftp server
pFtpConnection = pInetSession->GetFtpConnection(strServerName,
strUserName,
strPassword);
} catch (CInternetException* pEx){
// if failed, just show the error
// Oops! We failed to connect!
TCHAR szErr[1024];
pEx->GetErrorMessage(szErr, 1024);
TRACE(szErr);
AfxMessageBox(szErr, MB_OK|MB_ICONERROR);
pEx->Delete();
return 0;// return 1 but previous error box have been showed
}
return 1;
}
bool CFtp::GetFile(LPCTSTR remoteFile, LPCTSTR localFile, int bType)
{
BOOL bGotFile;
BOOL xfrType;
TCHAR szErr[1024];
xfrType = bType ? FTP_TRANSFER_TYPE_BINARY : FTP_TRANSFER_TYPE_ASCII;
// Try to get the file
try{
bGotFile = pFtpConnection->GetFile(remoteFile,
localFile, FALSE,
FILE_ATTRIBUTE_NORMAL,
xfrType | INTERNET_FLAG_RELOAD |
INTERNET_FLAG_NO_CACHE_WRITE);
} catch (CInternetException* pEx){
// Oops! We failed to dowload the file!
pEx->GetErrorMessage(szErr, sizeof(szErr));
TRACE(szErr);
AfxMessageBox(szErr, MB_OK|MB_ICONERROR);
pEx->Delete();
}
return bGotFile ? 1 : 0 ;
// if bGotFile is 0 ( FALSE ), return 0
// if bGotFile is 1 ( TRUE ), return 1
}
bool CFtp::PutFile(LPCTSTR localFile, LPCTSTR remoteDir, LPCTSTR remoteFile, int bType)
{
BOOL bGotFile;
BOOL xfrType;
TCHAR szErr[1024];
try{
pFtpConnection->SetCurrentDirectory(remoteDir);
} catch (CInternetException* pEx){
// Oops! We failed to change thr directory!
pEx->GetErrorMessage(szErr, sizeof(szErr));
TRACE(szErr);
AfxMessageBox(szErr, MB_OK|MB_ICONERROR);
pEx->Delete();
return 0;
}
xfrType = bType ? FTP_TRANSFER_TYPE_BINARY : FTP_TRANSFER_TYPE_ASCII;
// Try to upload the file
try{
bGotFile = pFtpConnection->PutFile(localFile, remoteFile,
xfrType | INTERNET_FLAG_RELOAD |
INTERNET_FLAG_NO_CACHE_WRITE);
} catch (CInternetException* pEx){
// Oops! We failed to upload the file!
pEx->GetErrorMessage(szErr, sizeof(szErr));
TRACE(szErr);
AfxMessageBox(szErr, MB_OK|MB_ICONERROR);
pEx->Delete();
}
return bGotFile ? 1 : 0 ;
// if bGotFile is 0 ( FALSE ), return 0
// if bGotFile is 1 ( TRUE ), return 1
}
bool CFtp::CloseConnection()
{
// close the connection to server, you can reconnect latter
if(pFtpConnection == NULL)
return 0;
try{
pFtpConnection->Close();
}catch(...)
{
return 0;
}
if(pFtpConnection != NULL)
delete pFtpConnection;
return 1;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -