查看: 91|回复: 0

[转载图文] C语言实现FTP文件上传下载

[复制链接]
发表于 2020-6-15 20:56:33 | 显示全部楼层 |阅读模式
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[dwDownloadDataSize];

RtlZeroMemory(pDownloadData, dwDownloadDataSize);
pBuf = new BYTE[4096];
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[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;
}


温馨提示:
1.如果您喜欢这篇帖子,请给作者点赞评分,点赞会增加帖子的热度,评分会给作者加学币。(评分不会扣掉您的积分,系统每天都会重置您的评分额度)。
2.回复帖子不仅是对作者的最好奖励,还可以获得学币奖励,请尊重作者的劳动成果,拒绝做伸手党!
3.发广告、灌水回复等违规行为一经发现直接禁言,如果本帖内容涉嫌违规,请点击论坛底部的举报反馈按钮,也可以在【投诉建议】板块发帖举报。
论坛交流群:672619046
微信公众号
快速回复 返回顶部 返回列表