gpt4 book ai didi

c++ - 在 ubuntu C++ 中使用 FTP 上传 Libcurl 错误

转载 作者:行者123 更新时间:2023-11-28 03:30:05 25 4
gpt4 key购买 nike

在 Windows 中,这段代码工作文件,但现在,我想将它转换成 ubuntu:

  // callback read function to upload file from local to ftp server
size_t read_callback (void* ptr, size_t size, size_t nmemb, FILE *stream){
//return fread(ptr,size,nmemb, (FILE*) stream);
return fread(ptr,size,nmemb,stream);
}

// get file name from a path
string FTPClientConnector::getFileName(string path){
int length = path.size();
for(int i = length - 1; i >= 0; i--){
if(path[i] == '/' || path[i] == '\\'){
return path.substr(i+1, length-i-1);
}
}
}

//function to upload a file to FTP server
int FTPClientConnector::uploadFile(string filePath, string serverPath ){
CURL *curl;
CURLcode res;
FILE *hd_src;
struct stat file_info;
curl_off_t fsize;

char* local_file = new char[filePath.size()+1];
std::copy(filePath.begin(), filePath.end(), local_file);
local_file[filePath.size()] = '\0';

// stat the local file
if(stat(local_file, &file_info)){
printf("couldn't open file\n");
delete local_file;
return -1;
}

// convert URL and username and password to connect to remote server
string urlPath = this->hostName + serverPath;
urlPath += getFileName(filePath);
char *url = new char[urlPath.size() + 1];
std::copy(urlPath.begin(), urlPath.end(), url);
url[urlPath.size()] = '\0';

string userAndPassString = this->userName + ":" + this->password;
char* usernameAndPassword = new char[userAndPassString.size() +1];
std::copy(userAndPassString.begin(), userAndPassString.end(), usernameAndPassword);
usernameAndPassword[userAndPassString.size()] = '\0';

// get the file to open
hd_src = fopen(local_file, "rb");

curl_global_init(CURL_GLOBAL_ALL);
curl = curl_easy_init();
if(curl){

/* specify target */
curl_easy_setopt(curl,CURLOPT_URL, url);
curl_easy_setopt(curl, CURLOPT_PORT, this->port);
curl_easy_setopt(curl, CURLOPT_USERPWD, usernameAndPassword);

/* we want to use our own read function */
curl_easy_setopt(curl, CURLOPT_READFUNCTION, read_callback);

/* enable uploading */
curl_easy_setopt(curl, CURLOPT_UPLOAD, 1L);

/* now specify which file to upload */
curl_easy_setopt(curl, CURLOPT_READDATA, hd_src);

/* Now run off and do what you've been told! */
res = curl_easy_perform(curl);

if(res != CURLE_OK){
printf("Upload file failed!\n");
delete local_file;
delete url;
delete usernameAndPassword;
return -1;
}
curl_easy_cleanup(curl);
}
fclose(hd_src);

delete local_file;
delete url;
delete usernameAndPassword;
return 0;
}

这是我在 main.cpp 中调用的:

FTPClientConnector connector(host,user,password,port);
connector.uploadFile("xml/kingfisher.xml", "/xml_test_upload");

上面的代码在 Ubuntu 中不能运行,有错误:

220 ProFTPD 1.3.4a Server (Debian) [::ffff:10.244.31.244]
500 PUT not understood
500 AUTHORIZATION: not understood
500 HOST: not understood
550 */*: Forbidden command argument
500 TRANSFER-ENCODING: not understood
500 EXPECT: not understood
500 Invalid command: try being more creative
500 2A2 not understood

编辑:这是我的Makefile:

uploader:
g++ -o uploader FTPClientConnector.cpp main.cpp -lcurl

最佳答案

输出似乎表明您正在向 FTP 服务器发送 HTTP。确保您的 URL 正确使用 FTP://前缀作为 FTP,因为没有协议(protocol)前缀 libcurl 会猜测您想要的协议(protocol)并且它默认为 HTTP...

关于c++ - 在 ubuntu C++ 中使用 FTP 上传 Libcurl 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12814391/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com