📄 ftpget.cpp
字号:
// FtpGet.cpp: implementation of the CFtpGet class.
#include "stdafx.h"
#include "FtpGet.h"
#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
CFtpGet::CFtpGet()
{
// get the name of the app
strAppName.LoadString(AFX_IDS_APP_TITLE);
// create an internet session
pInternetSession = new CInternetSession(strAppName,
INTERNET_OPEN_TYPE_PRECONFIG);
// if Not good, show message + return
// should never failed anyway
if(!pInternetSession)
{
AfxMessageBox("初始化会话失败!");
return;
}
}
CFtpGet::~CFtpGet()
{
// close the internet session
pInternetSession->Close();
// delete the session
if(pInternetSession != NULL)
delete pInternetSession;
}
// function, in logical order
bool CFtpGet::SetAccessRight(CString userName,
CString userPass)
{
// simply get username and password
m_strPass = userPass;
m_strUser = userName;
if( (m_strPass == "") || (m_strUser == ""))
return 0;
return 1;
}
bool CFtpGet::OpenConnection(CString strServer)
{
CWaitCursor cursor;
if(strServer == "")
return 0;
CString strTemp;
strTemp = "ftp://";
m_strFullURL=strServer;
if (!AfxParseURL(m_strFullURL, dwServiceType, m_strServerName, strAppName, nPort))
{
m_strFullURL=strTemp+ m_strFullURL;
if (!AfxParseURL(m_strFullURL, dwServiceType, m_strServerName, strAppName, nPort))
{
AfxMessageBox("无法解析服务器地址");
return 0;
}
}
//提取path
int nIndex=strTemp.GetLength()+m_strServerName.GetLength();
m_strFullPath=m_strFullURL.Right(m_strFullURL.GetLength()-nIndex);
// put the server name in the CFtpGet class
try {
// try to connect to a ftp server
pFtpConnection = pInternetSession->GetFtpConnection(m_strServerName,
m_strUser,
m_strPass);
} 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);
pEx->Delete();
return 0;// return 1 but previous error box have been showed
}
return 1;
}
int CFtpGet::GetMultipleFile(CStringArray *remoteArray,
CStringArray *localArray,
int number_file)
{
CWaitCursor cursor;
// init some var
BOOL goodfile;
int x=0;
pFtpConnection->SetCurrentDirectory(m_strFullPath);
// while loop to transfer every file in the array
while(x<number_file)
{
if(remoteArray->GetAt(x).Right(5)=="<DIR>")
{
AfxMessageBox("对不起,本软件不支持下载目录");
return 0;
}
// try to get file
else
{
goodfile = pFtpConnection->GetFile(remoteArray->GetAt(x),
localArray->GetAt(x),
FALSE,
FILE_ATTRIBUTE_NORMAL,
FTP_TRANSFER_TYPE_BINARY | INTERNET_FLAG_RELOAD | INTERNET_FLAG_NO_CACHE_WRITE);
if(!goodfile)
AfxMessageBox("下载时出错,请重试!");
}
x++;
}
return 1;
}
int CFtpGet::GetMultipleFileName(CStringArray *localNameArray)
//localNameArray作为输出参数
{
CWaitCursor cursor;
// init some var
BOOL goodfile;
int x=0;
int nFileNumber=0;
pFtpConnection->SetCurrentDirectory(m_strFullPath);
CFtpFileFind fFiles(pFtpConnection);
goodfile=fFiles.FindFile(m_strFullPath+_T("/*"));
if(goodfile==FALSE)
{
AfxMessageBox("目录为空。");
return 0;
}
// while loop to transfer every file in the array
CString str;
while(goodfile)
{
goodfile=fFiles.FindNextFile();
// try to get file name
str=fFiles.GetFileName();
if(fFiles.IsDirectory())
{
str+=" <DIR>";
}
localNameArray->InsertAt(x,str);
nFileNumber++;
x++;
}
//return the number of missing file, if any.
return nFileNumber;
}
bool CFtpGet::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 + -