MFC FTP 文件上传 下载

// 下载文件夹

BOOL CWLFtpDlg::OnDownloadDirectory(CString direPath)

{

int deep = 0;

int isDir;

HANDLE hFile;

DWORD ftpLength = 0;

DWORD dwWriteDataSize;

CString localFile,showPath;

CStringArray saveDir;

CFtpFileFind finder(mpFtpConnection);

mpFtpConnection->SetCurrentDirectory(direPath.GetBuffer(0));

CreateDirectory(m_filePath + "\\" + direPath,NULL);

BOOL bFind = finder.FindFile("*.*");

BOOL bResult = FALSE;

while(bFind)

{

bFind = finder.FindNextFile();

showPath = finder.GetFileURL();

TRACE("%s\r\n",showPath);

if (finder.IsDots())

{

continue;

}

if (finder.IsDirectory())

{

continue;

}

else

{

int progressNum = 0;

int progressTimes = 0;

FILETIME time,fileLastWritetime;

finder.GetLastWriteTime(&time);

LocalFileTimeToFileTime(&time,&fileLastWritetime);

ftpLength = finder.GetLength();

localFile = direPath +"\\"+ finder.GetFileName();

CInternetFile *pFtpFile = NULL;

pFtpFile = mpFtpConnection->OpenFile(finder.GetFileName());

if (pFtpFile)

{

hFile = CreateFile(m_filePath + "\\" + localFile,GENERIC_READ | GENERIC_WRITE,FILE_SHARE_READ,NULL,OPEN_ALWAYS,FILE_ATTRIBUTE_NORMAL,NULL);

char *downData = new char[MAX_SIZE];

while(TRUE)

{

Sleep(20);

int nowBytes = 0;

try

{

nowBytes = pFtpFile->Read(downData,MAX_SIZE);

}

catch (CInternetException* pEx)

{

char szErr[1024] = {0};

pEx->GetErrorMessage(szErr,1024);

pEx->Delete();

return FALSE;

}


WriteFile(hFile,downData,nowBytes,&dwWriteDataSize,NULL);

if (nowBytes < MAX_SIZE || nowBytes == 0)

{

break;

}

}

delete [] downData;

SetFileTime(hFile,NULL,NULL,&fileLastWritetime);

CloseHandle(hFile);

pFtpFile->Close();

}

else

{

return FALSE;

}

}

}

finder.Close();


return TRUE;

}


// 下载文件

BOOL CWLFtpDlg::OnDownloadFile(CString configFile)

{

BOOL bRes;

HINTERNET hInternet;

HINTERNET hConnect;

hInternet = InternetOpen("A3GS Sample",

INTERNET_OPEN_TYPE_DIRECT,

NULL,

NULL,

INTERNET_FLAG_NO_CACHE_WRITE);


if ( NULL == hInternet )

{

printf( "InternetOpen Error:%d\n", GetLastError() );

return FALSE;

}


hConnect  = InternetConnect(hInternet,

Dlg->m_strServer,

INTERNET_DEFAULT_FTP_PORT,

FTP_USERNAME,

FTP_PASSWORD,

INTERNET_SERVICE_FTP,

INTERNET_FLAG_EXISTING_CONNECT || INTERNET_FLAG_PASSIVE,

0 );


if ( NULL == hInternet )

{

printf( "InternetConnect Error:%d\n", GetLastError() );

InternetCloseHandle(hInternet);

return FALSE;

}


// 先下载本目录下面的文件

CString strServerDir=_T("./");

if(m_strCurPath.Compare("."))

{

strServerDir.Append(m_strCurPath);

strServerDir.Append("/");

}


if(!FtpSetCurrentDirectory (hConnect,strServerDir.GetBuffer(0)))

return FALSE;


CString strLocalPath = m_filePath;

strLocalPath.Append(configFile);

bRes = FtpGetFile(hConnect,

configFile.GetBuffer(0),

strLocalPath.GetBuffer(0),

FALSE,

FILE_ATTRIBUTE_NORMAL,

FTP_TRANSFER_TYPE_BINARY,

1);


InternetCloseHandle(hConnect);

InternetCloseHandle(hInternet);


return TRUE;

}


你可能感兴趣的:(文件夹)