实现FTP文件下载:
#include
#include
#include
#pragma comment(lib, "WinInet.lib")
BOOL Ftp_SaveToFile(char *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;
}
BOOL FTP_Download(char *szHostName, char *szUserName, char *szPassword, char *szUrlPath,char *SavePath)
{
HINTERNET hInternet, hConnect, hFTPFile = NULL;
BYTE *pDownloadData = NULL;
DWORD dwDownloadDataSize = 0;
DWORD dwBufferSize = 4096;
BYTE *pBuf = NULL;
DWORD dwBytesReturn = 0;
DWORD dwOffset = 0;
BOOL bRet = FALSE;
// 建立会话
hInternet = InternetOpen("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);
// 打开FTP文件, 文件操作和本地操作相似
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[dwBufferSize];
RtlZeroMemory(pBuf, dwBufferSize);
// 接收数据
do
{
bRet = InternetReadFile(hFTPFile, pBuf, dwBufferSize, &dwBytesReturn);
if (FALSE == bRet)
break;
RtlCopyMemory((pDownloadData + dwOffset), pBuf, dwBytesReturn);
dwOffset = dwOffset + dwBytesReturn;
} while (dwDownloadDataSize > dwOffset);
// 将数据保存为文件
Ftp_SaveToFile(SavePath, pDownloadData, dwDownloadDataSize);
// 释放内存
delete[]pDownloadData;
pDownloadData = NULL;
return TRUE;
}
int main(int argc,char * argv[])
{
// FTP主机地址 用户名 密码 下载网络文件地址 保存到本机文件夹
BOOL bRET = FTP_Download("192.168.1.10", "lyshark", "123321", "/Xuetr.exe","c://xuetr.exe");
printf("%d \n", bRET);
system("pause");
return 0;
}
实现FTP文件上传:
#include
#include
#include
#pragma comment(lib, "WinInet.lib")
BOOL FTP_Upload(char *szHostName, char *szUserName, char *szPassword, char *szUrlPath, char *FilePath)
{
HINTERNET hInternet, hConnect, hFTPFile = NULL;
DWORD dwBytesReturn = 0;
DWORD UploadDataSize = 0;
BYTE *pUploadData = NULL;
DWORD dwRet, bRet = 0;
// 建立会话
hInternet = InternetOpen("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);
// 打开FTP文件, 文件操作和本地操作相似
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)
{
delete[]pUploadData;
return FALSE;
}
delete[]pUploadData;
return TRUE;
}
int main(int argc,char * argv[])
{
// FTP主机地址 用户名 密码 上传到目标FTP目录 上传的本地文件
BOOL bRET = FTP_Upload("192.168.1.10", "lyshark", "123321", "/abc.exe","c://nc.exe");
printf("%d \n", bRET);
system("pause");
return 0;
}
版权声明:本博客文章与代码均为学习时整理的笔记,文章 [均为原创] 作品,转载请 [添加出处] ,您添加出处是我创作的动力!