// FTPClient.cpp : 定义控制台应用程序的入口点。
//
#include "stdafx.h"
#include <windows.h>
#include <wininet.h>
#include <stdio.h>
#pragma comment (lib, "wininet.lib")
BOOL FtpUpload(const char *serverIp,const char *userName, const char *passWord, const char *localFile,const char *newRemoteFile);
BOOL FtpDownload(const char *serverIp,const char *userName,const char *passWord,const char *newFile, const char *remoteFile);
void Usage();
int main(int argc, char* argv[])
{
if(argc==8)
{
if(strcmp(argv[1],"-ftp") == 0 && strcmp(argv[5],"-U") == 0)
{
FtpUpload(argv[2],argv[3],argv[4],argv[6],argv[7]);
return 0;
}
else if (strcmp(argv[1],"-ftp") == 0 && strcmp(argv[5],"-D") == 0)
{
FtpDownload(argv[2],argv[3],argv[4],argv[6],argv[7]);
return 0;
}
else
{
Usage();
return 0;
}
}
else if(argc==2 && strcmp(argv[1],"-pink") == 0)
{
Usage();
return 0;
}
else
{
printf("\n%s\n","the wrong command line argument");
Usage();
return 0;
}
}
void Usage()
{
printf("\nUsage:\n\t%s\t%s\n","DownLoad:"," -ftp ServerIp UserName PassWord -D NewLocal RemoteFile");
printf("\t%s\t\t%s\n","UpLoad:"," -ftp ServerIp UserName PassWord -U LocalFile NewRemoteFile");
}
BOOL FtpUpload(const char *serverIp,const char *userName, const char *passWord, const char *localFile,const char *newRemoteFile)
{
HINTERNET hInternet = NULL;
HINTERNET hFtp = NULL;
DWORD dwFlags = 0;
DWORD_PTR dwContext = NULL;
char * agentName = "FTP";
hInternet = InternetOpen( agentName, INTERNET_OPEN_TYPE_DIRECT,NULL,NULL,INTERNET_FLAG_ASYNC);
if(hInternet == NULL)
{
printf("InternetOpen failed with the code of:%d\n",GetLastError());
return false;
}
hFtp = InternetConnect(hInternet, serverIp,INTERNET_DEFAULT_FTP_PORT, userName,passWord,INTERNET_SERVICE_FTP,dwFlags,dwContext);
if(hFtp == NULL)
{
printf("InternetConnect failed with the code of:%d\n",GetLastError());
return false;
}
if(!FtpPutFile(hFtp,localFile,newRemoteFile,FTP_TRANSFER_TYPE_UNKNOWN,NULL))
{
printf("FtpPutFile failed with the code of:%d\n",GetLastError());
return false;
}
printf("\nthe file upload from %s to %s successfully. \n", localFile, newRemoteFile );
InternetCloseHandle(hInternet);
InternetCloseHandle(hFtp);
return true;
}
BOOL FtpDownload(const char *serverIp,const char *userName,const char *passWord,const char *newFile, const char *remoteFile)
{
HINTERNET hConnect = NULL;
HINTERNET hInternet = NULL;
DWORD_PTR dwContext = NULL;
DWORD dwFlags = 0;
char * agentName = "FTP";
hInternet = InternetOpen( agentName, INTERNET_OPEN_TYPE_DIRECT,NULL,NULL,INTERNET_FLAG_ASYNC);
if(hInternet == NULL)
{
printf("InternetOpen failed with the code of:%d\n",GetLastError());
return false;
}
hConnect = InternetConnect(hInternet,serverIp,INTERNET_DEFAULT_FTP_PORT,userName,passWord,INTERNET_SERVICE_FTP,dwFlags,dwContext);
if(hConnect == NULL)
{
printf("InternetConnect failed with the code of:%d\n",GetLastError());
return false;
}
if(!FtpGetFile(hConnect,remoteFile,newFile,true,FILE_ATTRIBUTE_NORMAL,FTP_TRANSFER_TYPE_UNKNOWN,NULL))
{
if(GetLastError() == 80)
{
printf(" The file already exits.\n");
return false;
}
else
{
printf("FtpGetFile failed with the code of:%d\n",GetLastError());
return false;
}
}
printf("\nthe file download from %s to %s successfully. \n", remoteFile,newFile );
InternetCloseHandle(hInternet);
InternetCloseHandle(hConnect);
return true;
}
FTP.rar_ftp
版权申诉
84 浏览量
2022-09-24
15:39:11
上传
评论
收藏 16KB RAR 举报
钱亚锋
- 粉丝: 88
- 资源: 1万+
最新资源
- 设计1.ms14
- vscode-1.64.1.tar源码文件
- vscode-1.64.0.tar源码文件
- vscode-1.52.0.tar源码文件
- Music-Player +PlayerActivity+ rockplayer+ SeeJoPlayer 播放器JAVA源码
- vscode-1.46.0.tar源码文件
- 最近很火植物大战僵尸杂交版2.08苹果+安卓+PC+防闪退工具V2+修改工具+高清工具+通关存档整合包更新
- 超级好用的截图工具PixPin,可录制Gif图
- Screenshot_2024-05-21-17-06-42-64_2332cb9b27b851b548ba47a91682926c.jpg
- 毕业设计参考 - 基于树莓派、OpenCV及Python的人脸识别
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈