C++基于libcurl的FTP上传测试
最近有需求通过FTP上传视频文件和照片,现将DEMO列出来
1.先上完整程序
/************************************************************************* > File Name: 6.c > Created Time: Tue 02 Aug 2022 10:17:39 AM +08 ************************************************************************/ #include <stdio.h> #include <stdlib.h> #include <iostream> #include <string.h> #include <curl/curl.h> #include <sys/types.h> #include <sys/stat.h> #include <fcntl.h> #include <errno.h> #include <unistd.h> #define LOCAL_FILE "/opt/example.txt" #define REMOTE_URL "ftp://xxxx.w238.cndns5.com/wwwroot/example.txt" class FTPs { public: static int ftpUpload(const char* remoteFtpPath, const char* localFilePath, long timeout); //FTP上传函数 private: static size_t readFunc(void* ptr, size_t size, size_t nmemb, void* stream); //FTP读取函数 }; /****************************************************************************** * Name : FTP::readFunc * Author : cqnews * Version : V1.0.0 * Data : 2022.08.02 * Describe : FTP读取函数 ******************************************************************************/ size_t FTPs::readFunc(void* ptr, size_t size, size_t nmemb, void* stream) { size_t n; FILE* f = (FILE*)stream; if (ferror(f)) { return CURL_READFUNC_ABORT; } n = fread(ptr, size, nmemb, f) * size; return n; } /****************************************************************************** * Name : FTP::ftpUpload * Author : cqnews * Version : V1.0.0 * Data : 2022.08.02 * Describe : FTP上传函数 * remoteFtpPath: 需要上传的ftp服务端地址和文件名 * localFilePath: 本地需要上传的文件完整路径 * timeout : 上传超时时间 0:disable * return : 0:成功 -1:失败 ******************************************************************************/ int FTPs::ftpUpload(const char* remoteFtpPath, const char* localFilePath, long timeout) { CURL* curl = NULL; try { curl_global_init(CURL_GLOBAL_ALL); struct stat file_info; curl_off_t fsize; if (stat(localFilePath, &file_info)) /* 获得上传文件的大小 */ { printf("FTP file could n't open '%s': %s.\n", localFilePath, strerror(errno)); return -1; } fsize = (curl_off_t)file_info.st_size; FILE* fd; fd = fopen(localFilePath, "rb"); if (fd == NULL) { printf("FTP fopen file failed.\n"); return -1; } curl = curl_easy_init(); if (curl == NULL) { printf("FTP curl_easy_init create curl failed."); return -1; } curl_easy_setopt(curl, CURLOPT_UPLOAD, 1L); //使能上传功能 curl_easy_setopt(curl, CURLOPT_URL, remoteFtpPath); //设置特定目标 curl_easy_setopt(curl, CURLOPT_USERNAME, "xxxx"); curl_easy_setopt(curl, CURLOPT_PASSWORD, "xxxx"); curl_easy_setopt(curl, CURLOPT_READFUNCTION, FTPs::readFunc); //使用curl提供的Read功能 curl_easy_setopt(curl, CURLOPT_READDATA, fd); //将参数fd传递给回调函数readfunc的第四个参数 curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L); //调试信息打印 curl_easy_setopt(curl, CURLOPT_INFILESIZE_LARGE, fsize); /*设置要上传的文件的大小(可选) */ if (timeout) { curl_easy_setopt(curl, CURLOPT_FTP_RESPONSE_TIMEOUT, timeout); } CURLcode r = curl_easy_perform(curl); fclose(fd); if (r != CURLE_OK) { printf("FTP upload event %s.\n", curl_easy_strerror(r)); return -1; } } catch (...) { } if (curl) { curl_easy_cleanup(curl); } curl_global_cleanup(); return 0; } int main() { FTPs::ftpUpload(REMOTE_URL, LOCAL_FILE, 0); return 0; }
2.编译
arm-linux-gnueabihf-g++ 6.c -o 6.out -I/usr/arm32/include/ -L/usr/arm32/lib -lcurl
3.测试效果
4.远程效果