roger 发表于 2020-6-15 20:56:33

C语言实现FTP文件上传下载

FTP文件下载#include <stdio.h>
#include <Windows.h>
#include <WinInet.h>
#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;
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);

hFTPFile = FtpOpenFile(hConnect, szUrlPath, GENERIC_READ, FTP_TRANSFER_TYPE_BINARY | INTERNET_FLAG_RELOAD, NULL);
dwDownloadDataSize = FtpGetFileSize(hFTPFile, NULL);
pDownloadData = new BYTE;

RtlZeroMemory(pDownloadData, dwDownloadDataSize);
pBuf = new BYTE;
RtlZeroMemory(pBuf, 4096);
do
{
bRet = InternetReadFile(hFTPFile, pBuf, 4096, &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;
}
FTP文件上传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 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;
::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;
}

页: [1]
查看完整版本: C语言实现FTP文件上传下载