#include "Download.h"
#define DOWNLOAD_BUFF_SIZE 4096
BOOL Ftp_SaveToFile(TCHAR *pszFileName, BYTE *pData, DWORD dwDataSize)
{
HANDLE hFile = ::CreateFile(pszFileName, GENERIC_READ | GENERIC_WRITE,
FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, CREATE_ALWAYS,
FILE_ATTRIBUTE_ARCHIVE, NULL);
if (INVALID_HANDLE_VALUE == hFile)
{
return FALSE;
}
DWORD dwRet = 0;
WriteFile(hFile, pData, dwDataSize, &dwRet, NULL);
CloseHandle(hFile);
return TRUE;
}
struct InternetConnectParam
{
HINTERNET hInternet;
HINTERNET hSession;
TCHAR* host;
TCHAR* userName;
TCHAR* passWord;
};
BOOL ThreadInternetConnect(PVOID param)
{
InternetConnectParam *p_connect_param = (InternetConnectParam*)param;
p_connect_param->hSession = NULL;
p_connect_param->hSession = InternetConnect(p_connect_param->hInternet, p_connect_param->host, INTERNET_INVALID_PORT_NUMBER,
p_connect_param->userName, p_connect_param->passWord, INTERNET_SERVICE_FTP, INTERNET_FLAG_PASSIVE, 0);
if (p_connect_param->hSession == NULL)
{
printf("[ThreadInternetConnect]InternetConnect failed,err = %u",GetLastError());
return FALSE;
}
return TRUE;
}
BOOL FTP_Download(TCHAR *szHostName, TCHAR *szUserName, TCHAR *szPassword, TCHAR *szUrlPath,TCHAR *SavePath)
{
HINTERNET hInternet, hConnect, hFTPFile = NULL;
BYTE *pDownloadData = NULL;
DWORD dwDownloadDataSize = 0;
BYTE *pBuf = NULL;
DWORD dwBytesReturn = 0;
DWORD dwOffset = 0;
BOOL bRet = FALSE;
InternetConnectParam param;
HANDLE hThread;
hInternet = InternetOpen(L"WinInet Ftp", INTERNET_OPEN_TYPE_PRECONFIG, NULL, NULL, 0);
//没有超时连接
//hConnect = InternetConnect(hInternet, szHostName, INTERNET_INVALID_PORT_NUMBER,
// szUserName, szPassword, INTERNET_SERVICE_FTP, INTERNET_FLAG_PASSIVE, 0);
//InternetConnect连接某些情况会超时,使用线程来InternetConnect,超时则直接结束线程
param.hInternet = hInternet;
param.host = szHostName;
param.userName = szUserName;
param.passWord = szPassword;
hThread = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)ThreadInternetConnect, (LPVOID)¶m, 0, NULL);
//超时1秒,如果等待结果是超时
if (WaitForSingleObject(hThread, 1000) == WAIT_TIMEOUT)
{
printf("[FTPDownload]ThreadInternetConnect TimeOut!");
goto _Clean;
}
hFTPFile = FtpOpenFile(hConnect, szUrlPath, GENERIC_READ, FTP_TRANSFER_TYPE_BINARY | INTERNET_FLAG_RELOAD, NULL);
dwDownloadDataSize = FtpGetFileSize(hFTPFile, NULL);
pDownloadData = new BYTE[dwDownloadDataSize];
RtlZeroMemory(pDownloadData, dwDownloadDataSize);
pBuf = new BYTE[DOWNLOAD_BUFF_SIZE];
RtlZeroMemory(pBuf, DOWNLOAD_BUFF_SIZE);
do
{
bRet = InternetReadFile(hFTPFile, pBuf, DOWNLOAD_BUFF_SIZE, &dwBytesReturn);
if (FALSE == bRet)
{
break;
}
RtlCopyMemory((pDownloadData + dwOffset), pBuf, dwBytesReturn);
dwOffset = dwOffset + dwBytesReturn;
} while (dwDownloadDataSize > dwOffset);
bRet = Ftp_SaveToFile(SavePath, pDownloadData, dwDownloadDataSize);
_Clean:
if (pDownloadData)
{
delete []pDownloadData;
pDownloadData = NULL;
}
if (pBuf)
{
delete []pBuf;
pBuf = NULL;
}
return bRet;
}
BOOL FTP_Upload(TCHAR *szHostName, TCHAR *szUserName, TCHAR *szPassword, TCHAR *szUrlPath, TCHAR *FilePath)
{
HINTERNET hInternet, hConnect, hFTPFile = NULL;
DWORD dwBytesReturn = 0;
DWORD UploadDataSize = 0;
BYTE *pUploadData = NULL;
DWORD dwRet;
BOOL bRet = FALSE;
hInternet = ::InternetOpen(L"WinInet Ftp Upload V1.0", INTERNET_OPEN_TYPE_PRECONFIG, NULL, NULL, 0);
hConnect = ::InternetConnect(hInternet, szHostName, INTERNET_INVALID_PORT_NUMBER, szUserName, szPassword,
INTERNET_SERVICE_FTP, INTERNET_FLAG_PASSIVE, 0);
hFTPFile = ::FtpOpenFile(hConnect, szUrlPath, GENERIC_WRITE, FTP_TRANSFER_TYPE_BINARY | INTERNET_FLAG_RELOAD, NULL);
HANDLE hFile = ::CreateFile(FilePath, GENERIC_READ | GENERIC_WRITE,FILE_SHARE_READ |
FILE_SHARE_WRITE, NULL, OPEN_EXISTING,FILE_ATTRIBUTE_ARCHIVE, NULL);
if (INVALID_HANDLE_VALUE == hFile)
return FALSE;
UploadDataSize = ::GetFileSize(hFile, NULL);
pUploadData = new BYTE[UploadDataSize];
::ReadFile(hFile, pUploadData, UploadDataSize, &dwRet, NULL);
UploadDataSize = dwRet;
bRet = ::InternetWriteFile(hFTPFile, pUploadData, UploadDataSize, &dwBytesReturn);
if (FALSE == bRet)
{
goto _End;
}
_End:
if (pUploadData)
{
delete []pUploadData;
pUploadData = NULL;
}
return bRet;
}
|