我不能上传文件到FTP服务器
C++ I cant upload file to FTP server
我想上传一个文件到我的FTP服务器使用c++代码,我能够FTP我的服务器与FileZilla。
当我运行我的c++代码时,它抛出一个输出"3"错误(GetLastError()函数将此值返回给ftppputfile()函数
#pragma comment (lib,"wininet.lib")
#include <windows.h>
#include <wininet.h> //for uploadFile function
#include <iostream>
using namespace std;
int main()
{
HINTERNET hint, hftp;
hint = InternetOpen("FTP", INTERNET_OPEN_TYPE_PRECONFIG, 0, 0, INTERNET_FLAG_ASYNC);
hftp = InternetConnect(hint, "MY IP ADRESS", INTERNET_DEFAULT_FTP_PORT, "MY NAME", "MY PASS", INTERNET_SERVICE_FTP, 0, 0);
if (!FtpPutFile(hftp, "C://Users//Elliot//Desktop//log.txt", "//log.txt", FTP_TRANSFER_TYPE_BINARY, 0))
{
cout << "FAIL !" << endl;
cout << GetLastError() << endl;
}
else {
cout << "file sended !";
};
InternetCloseHandle(hftp);
InternetCloseHandle(hint);
system("PAUSE");
}
我尝试过的事情:
更改服务器(我创建了新服务器,但结果仍然相同)
控制防火墙以管理员身份运行
断点(ftppputfile给出错误)
错误信息很清楚。文件不在那里,就没有什么可发送的。在继续使用互联网功能之前,您可以使用std::ifstream
轻松检查文件的路径。
使用FtpSetCurrentDirectory
设置目标目录。在这个例子中,我使用"public_html"
,也许你的服务器是不同的。
#include <windows.h>
#include <wininet.h>
#include <iostream>
#include <string>
#include <fstream>
#pragma comment (lib,"wininet.lib")
using namespace std;
int main()
{
string file = "C:\path.txt";
string site = "www.site.com";
string user = "username";
string pass = "password";
if (!ifstream(file))
{
cout << "no filen";
return 0;
}
HINTERNET hint = InternetOpen(0, INTERNET_OPEN_TYPE_PRECONFIG, 0, 0, 0);
HINTERNET hftp = InternetConnect(hint, site.c_str(), INTERNET_DEFAULT_FTP_PORT,
user.c_str(), pass.c_str(), INTERNET_SERVICE_FTP, 0, 0);
if (FtpSetCurrentDirectory(hftp, "public_html"))
{
if (!FtpPutFile(hftp, file.c_str(), "log.txt", FTP_TRANSFER_TYPE_BINARY, 0))
{
cout << "FAIL!" << endl;
cout << GetLastError() << endl;
}
else
{
cout << "file sended !";
}
}
InternetCloseHandle(hftp);
InternetCloseHandle(hint);
return 0;
}
相关文章:
- FTP服务器试图登录时奇怪的响应
- 连接到 FTP 服务器并访问文件
- 如何在不保存文件的情况下制作打印屏幕并将其发送到FTP服务器?我的工作代码将文件保存到HDD
- 在 FTP 服务器中正确实现 LIST 命令
- Internet Connect无法通过FTP代理连接到FTP服务器
- FTP服务器端口处于主动模式和被动模式
- 使用Libcurl从FTP服务器下载多个文件
- C++:如何使用libcurl检查FTP服务器上是否存在目录
- 判断文件是否为FTP服务器上的目录
- 动态分配内存以接收FTP服务器的LIST响应时的SIGSEGV
- 创建虚假 ftp 服务器的程序
- 如何避免 QNetwork请求将 RHELP 动词发送到 FTP 服务器
- FTP 服务器无法正确处理'rn'
- QNetworkAccessManager上载到本地FTP服务器
- 用于RPi2的UWP FTP服务器
- 如何在ftp服务器/客户端将数据发送到TCP套接字之前从其获取数据
- 如何获得ftp服务器中所有文件和文件夹的列表(c++)
- 终止ftp服务器上的数据传输时的客户端行为
- 为FTP服务器制作文件-当头文件被修改时编译
- 我不能上传文件到FTP服务器