"); //-->
在Linux中curl是一个利用URL规则在命令行下工作的文件传输工具,如下为通过curl命令上传、下载ftp文件的用法:
# curl -T upload_test.txt -u fangye:fangye ftp://192.168.10.105/ //下载ftp文件 -u 账号:密码
更多curl命令用法参考博客: https://blog.csdn.net/gubenpeiyuan/article/details/50803095
libcurl库为通信协议库,支持HTTP, HTTPS, FTP, FTPS, GOPHER, TFTP, SCP, SFTP, SMB, TELNET, DICT, LDAP, LDAPS, FILE, IMAP, SMTP, POP3, RTSP 和 RTMP等多种协议,如下为基于curl库中ftp协议实现的文件上传、下载示例:
ftp-manager.c
————————————————
#include <stdio.h> #include <stdlib.h> #include <curl/curl.h> #include "ftp-manager.h" int get_file_size(FILE *file) //获取文件大小 { int size = 0; fseek(file, 0L, SEEK_END); size = ftell(file); fseek(file, 0L, SEEK_SET); return size; } CURL *curl_init() //curl初始化 { curl_global_init(CURL_GLOBAL_DEFAULT); CURL *curl = curl_easy_init(); if(NULL == curl) { fprintf(stderr, "Init curl failed.\n"); exit(1); } return curl; } void curl_set_upload_opt(CURL *curl, const char *url, const char *user_key, FILE *file) //设置上传配置参数 { curl_easy_setopt(curl, CURLOPT_URL, url); curl_easy_setopt(curl, CURLOPT_USERPWD, user_key); curl_easy_setopt(curl, CURLOPT_READDATA, file); curl_easy_setopt(curl, CURLOPT_UPLOAD, 1); curl_easy_setopt(curl, CURLOPT_INFILESIZE, get_file_size(file)); curl_easy_setopt(curl, CURLOPT_FTP_CREATE_MISSING_DIRS, 1); // curl_easy_setopt(curl, CURLOPT_VERBOSE, 1); } void curl_set_download_opt(CURL *curl, const char *url, const char *user_key, FILE *file) //设置下载配置参数 { curl_easy_setopt(curl, CURLOPT_URL, url); curl_easy_setopt(curl, CURLOPT_USERPWD, user_key); curl_easy_setopt(curl, CURLOPT_WRITEDATA, file); // curl_easy_setopt(curl, CURLOPT_VERBOSE, 1); } void curl_exit(CURL *curl) //退出curl { curl_easy_cleanup(curl); curl_global_cleanup(); } CURLcode curl_perform(CURL *curl) //执行curl { CURLcode ret = curl_easy_perform(curl); if(ret != 0) { fprintf(stderr, "Perform curl failed.\n"); curl_exit(curl); exit(1); } return ret; } FTP_STATE ftp_upload(const FTP_OPT ftp_option) //ftp上传文件 { FTP_STATE state; CURL *curl;; FILE *fp = fopen(ftp_option.file, "r"); if(NULL == fp) { fprintf(stderr, "Open file failed at %s:%d\n", __FILE__, __LINE__); return FTP_UPLOAD_FAILED; } curl = curl_init(); curl_set_upload_opt(curl, ftp_option.url, ftp_option.user_key, fp); if(CURLE_OK == curl_perform(curl)) state = FTP_UPLOAD_SUCCESS; else state = FTP_UPLOAD_FAILED; curl_exit(curl); fclose(fp); return state; } FTP_STATE ftp_download(const FTP_OPT ftp_option) // ftp下载文件 { FTP_STATE state; CURL *curl; FILE *fp = fopen(ftp_option.file, "w"); if(NULL == fp) { fprintf(stderr, "Open file failed at %s:%d\n", __FILE__, __LINE__); return FTP_UPLOAD_FAILED; } curl = curl_init(); curl_set_download_opt(curl, ftp_option.url, ftp_option.user_key, fp); if(CURLE_OK == curl_perform(curl)) state = FTP_DOWNLOAD_SUCCESS; else state = FTP_DOWNLOAD_FAILED; curl_exit(curl); fclose(fp); return state; }
ftp-manager.h
#ifndef _FTP_MANAGER_H_ #define _FTP_MANAGER_H_ typedef enum FTP_STATE { FTP_UPLOAD_SUCCESS, FTP_UPLOAD_FAILED, FTP_DOWNLOAD_SUCCESS, FTP_DOWNLOAD_FAILED }FTP_STATE; typedef struct FTP_OPT { char *url; /*url of ftp*/ char *user_key; /*username:password*/ char *file; /*filepath*/ }FTP_OPT; #ifdef __cplusplus extern "C" { #endif FTP_STATE ftp_upload(const FTP_OPT ftp_option); //上传文件 FTP_STATE ftp_download(const FTP_OPT ftp_option); //下载文件 #ifdef __cplusplus } #endif #endif
main.c //ftp文件上传、下载调用curl示例
#include <stdio.h> #include "ftp-manager.h" #define UPLOAD #define DOWNLOAD int main() { FTP_OPT ftp_opt; #ifdef UPLOAD ftp_opt.url = "ftp://192.168.10.105/upload/upload_test.txt"; //传到ftp服务器路径名 ftp_opt.user_key = "fangye:fangye"; //ftp 账号密码 格式<账号:密码> ftp_opt.file = "./upload_test.txt"; //指本地文件要上传的文件 if(FTP_UPLOAD_SUCCESS == ftp_upload(ftp_opt)) printf("Upload success.\n"); else printf("Upload failed.\n"); #endif #ifdef DOWNLOAD ftp_opt.url = "ftp://192.168.10.105/download/ZeroMQ.pdf"; //ftp服务器文件 ftp_opt.file = "zmq.pdf"; //指下载到本地文件名 if(FTP_DOWNLOAD_SUCCESS == ftp_download(ftp_opt)) printf("Download success.\n"); else printf("Download failed.\n"); #endif return 0; }
*博客内容为网友个人发布,仅代表博主个人观点,如有侵权请联系工作人员删除。