libcurl之ftp上传 & 下载
由于项目的需要,利用libcurl编写程序与ftp服务器通信,进行文件上传或下载,愈发地感到curl功能的强悍。其实在命令行下直接使用curl命令就可以完成这些功能,但是需要获取上传下载行为的状态以便更好地控制。直接上代码:
View Code
1 //ftp-manager.h 2 3 #ifndef _FTP_MANAGER_H_ 4 #define _FTP_MANAGER_H_ 5 6 /*FTP OPERATION CODE*/ 7 typedef enum FTP_STATE 8 { 9 FTP_UPLOAD_SUCCESS, 10 FTP_UPLOAD_FAILED, 11 FTP_DOWNLOAD_SUCCESS, 12 FTP_DOWNLOAD_FAILED 13 }FTP_STATE; 14 15 /*FTP OPERATIONS OPTIONS*/ 16 typedef struct FTP_OPT 17 { 18 char *url; /*url of ftp*/ 19 char *user_key; /*username:password*/ 20 char *file; /*filepath*/ 21 }FTP_OPT; 22 23 #ifdef __cplusplus 24 extern "C" { 25 #endif 26 27 /*upload file to ftp server*/ 28 FTP_STATE ftp_upload(const FTP_OPT ftp_option); 29 30 /*download file from ftp server*/ 31 FTP_STATE ftp_download(const FTP_OPT ftp_option); 32 33 #ifdef __cplusplus 34 } 35 #endif 36 37 #endif
View Code
1 //ftp-manager.c 2 3 #include <stdio.h> 4 #include <stdlib.h> 5 #include <curl/curl.h> 6 7 #include "ftp-manager.h" 8 9 /*****************util api******************/ 10 int get_file_size(FILE *file) 11 { 12 int size = 0; 13 fseek(file, 0L, SEEK_END); 14 size = ftell(file); 15 fseek(file, 0L, SEEK_SET); 16 return size; 17 } 18 19 /******************curl api****************/ 20 CURL *curl_init() 21 { 22 curl_global_init(CURL_GLOBAL_DEFAULT); 23 CURL *curl = curl_easy_init(); 24 if(NULL == curl) 25 { 26 fprintf(stderr, "Init curl failed.\n"); 27 exit(1); 28 } 29 return curl; 30 } 31 32 void curl_set_upload_opt(CURL *curl, const char *url, const char *user_key, FILE *file) 33 { 34 curl_easy_setopt(curl, CURLOPT_URL, url); 35 curl_easy_setopt(curl, CURLOPT_USERPWD, user_key); 36 curl_easy_setopt(curl, CURLOPT_READDATA, file); 37 curl_easy_setopt(curl, CURLOPT_UPLOAD, 1); 38 curl_easy_setopt(curl, CURLOPT_INFILESIZE, get_file_size(file)); 39 curl_easy_setopt(curl, CURLOPT_FTP_CREATE_MISSING_DIRS, 1); 40 // curl_easy_setopt(curl, CURLOPT_VERBOSE, 1); 41 } 42 43 void curl_set_download_opt(CURL *curl, const char *url, const char *user_key, FILE *file) 44 { 45 curl_easy_setopt(curl, CURLOPT_URL, url); 46 curl_easy_setopt(curl, CURLOPT_USERPWD, user_key); 47 curl_easy_setopt(curl, CURLOPT_WRITEDATA, file); 48 // curl_easy_setopt(curl, CURLOPT_VERBOSE, 1); 49 } 50 51 void curl_exit(CURL *curl) 52 { 53 curl_easy_cleanup(curl); 54 curl_global_cleanup(); 55 } 56 57 CURLcode curl_perform(CURL *curl) 58 { 59 CURLcode ret = curl_easy_perform(curl); 60 if(ret != 0) 61 { 62 fprintf(stderr, "Perform curl failed.\n"); 63 curl_exit(curl); 64 exit(1); 65 } 66 return ret; 67 } 68 69 /****************ftp upload & download api******************/ 70 FTP_STATE ftp_upload(const FTP_OPT ftp_option) 71 { 72 FTP_STATE state; 73 CURL *curl;; 74 FILE *fp = fopen(ftp_option.file, "r"); 75 if(NULL == fp) 76 { 77 fprintf(stderr, "Open file failed at %s:%d\n", __FILE__, __LINE__); 78 return FTP_UPLOAD_FAILED; 79 } 80 81 curl = curl_init(); 82 curl_set_upload_opt(curl, ftp_option.url, ftp_option.user_key, fp); 83 if(CURLE_OK == curl_perform(curl)) 84 state = FTP_UPLOAD_SUCCESS; 85 else 86 state = FTP_UPLOAD_FAILED; 87 88 curl_exit(curl); 89 fclose(fp); 90 return state; 91 } 92 93 FTP_STATE ftp_download(const FTP_OPT ftp_option) 94 { 95 FTP_STATE state; 96 CURL *curl; 97 FILE *fp = fopen(ftp_option.file, "w"); 98 if(NULL == fp) 99 { 100 fprintf(stderr, "Open file failed at %s:%d\n", __FILE__, __LINE__); 101 return FTP_UPLOAD_FAILED; 102 } 103 104 curl = curl_init(); 105 curl_set_download_opt(curl, ftp_option.url, ftp_option.user_key, fp); 106 if(CURLE_OK == curl_perform(curl)) 107 state = FTP_DOWNLOAD_SUCCESS; 108 else 109 state = FTP_DOWNLOAD_FAILED; 110 111 curl_exit(curl); 112 fclose(fp); 113 return state; 114 }
View Code