curl实现SFTP上传下载文件

摘自:https://blog.csdn.net/swj9099/article/details/85292444

  1 #include <stdio.h>
  2 #include <stdlib.h>
  3 #include <string.h>
  4 #include <unistd.h>
  5 #include <pthread.h>
  6 #include <time.h>
  7 #include <sys/stat.h>
  8 #include <signal.h>
  9 #include <curl/curl.h>
 10 #include <curl/easy.h>
 11 #include <string.h>
 12 
 13 /*
 14 gcc curl_test.cpp -I /usr/local/curl/include/ -I /usr/local/libssh2/include/ -I /usr/local/openssl/include/  -L /usr/local/curl/lib/ -L /usr/local/libssh2/lib/ -L /usr/local/openssl/lib/ -lrt -lcurl -lssh2 -lssl -lcrypto -ldl -lz
 15 */
 16 
 17 static void gloale_init(void)
 18 {
 19     curl_global_init(CURL_GLOBAL_DEFAULT);
 20     return;
 21 }
 22 
 23 static size_t read_callback(void *ptr, size_t size, size_t nmemb, void *stream) //回调函数
 24 {
 25     curl_off_t nread;
 26     size_t retcode = fread(ptr, size, nmemb, (FILE*)(stream));
 27     nread = (curl_off_t)retcode;
 28     return retcode;
 29 }
 30 
 31 static size_t write_data(void *ptr, size_t size, size_t nmemb, void *stream)
 32 {
 33     int written = fwrite(ptr, size, nmemb, (FILE *)stream);
 34     return written;
 35 }
 36 
 37 static size_t upload(const char *user, const char *passwd, const char *url, const char *path)
 38 {    
 39     CURL *curl = NULL;
 40     CURLcode res;
 41     // char *s3 = NULL;
 42     // asprintf(&s3, "%s:%s", user, passwd);
 43     // free(s3);
 44       
 45     // system("ls write_file");
 46     FILE *pSendFile = fopen(path, "r");
 47     if (pSendFile == NULL)
 48     {
 49         printf("open failed
");
 50         return 1;
 51     }
 52 
 53     fseek(pSendFile, 0L, SEEK_END);
 54 
 55     size_t iFileSize = ftell(pSendFile);
 56 
 57     fseek(pSendFile, 0L, SEEK_SET);
 58     printf("begin easy_init
"); 
 59  
 60     curl = curl_easy_init();
 61     printf("curl_easy_init success
");
 62     if (curl) {
 63         curl_easy_setopt(curl, CURLOPT_URL,url);
 64         // curl_easy_setopt(curl, CURLOPT_USERPWD, s3.c_str()); 
 65         curl_easy_setopt(curl, CURLOPT_USERNAME, user);
 66         curl_easy_setopt(curl, CURLOPT_PASSWORD, passwd);     
 67         curl_easy_setopt(curl, CURLOPT_READFUNCTION, read_callback);
 68         curl_easy_setopt(curl, CURLOPT_READDATA, pSendFile);
 69         curl_easy_setopt(curl, CURLOPT_FTP_CREATE_MISSING_DIRS, 0);
 70         curl_easy_setopt(curl, CURLOPT_UPLOAD, 1);
 71         curl_easy_setopt(curl, CURLOPT_INFILESIZE, iFileSize);
 72     
 73         printf("curl_easy_setopt success
");
 74         res = curl_easy_perform(curl);
 75     
 76         curl_easy_cleanup(curl);
 77     
 78         if (CURLE_OK != res) 
 79         {
 80     
 81             fprintf(stdout, "curl told us %d
", res);
 82         }
 83     }
 84     fclose(pSendFile);
 85     curl_global_cleanup();
 86     return 0;
 87 }
 88 
 89 static int download(const char *user, const char *passwd, const char *url, const char *filePath)
 90 {
 91     CURL *curl = NULL;
 92     CURLcode curl_code;
 93     // char *s3 = NULL;
 94     // asprintf(&s3, "%s:%s", user, passwd);
 95     // free(s3);
 96     
 97     curl = curl_easy_init();
 98     curl_easy_setopt(curl, CURLOPT_URL, url);
 99 //    curl_easy_setopt(curl, CURLOPT_USERPWD, s3.c_str());
100     curl_easy_setopt(curl, CURLOPT_USERNAME, user);
101     curl_easy_setopt(curl, CURLOPT_PASSWORD, passwd);
102     
103     FILE *fp = fopen(filePath, "wb+");
104     if (NULL == fp)
105     {
106         curl_easy_cleanup(curl);
107         printf("fopen failed
");
108         return -1;
109     }
110     
111     curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_data);
112     curl_easy_setopt(curl, CURLOPT_WRITEDATA, fp);
113     curl_code = curl_easy_perform(curl);
114     printf("curl_code = %d
",curl_code);
115     if (CURLE_OK != curl_code)
116     {
117         printf("perform failed
");
118         curl_easy_cleanup(curl);
119         fclose(fp);
120         remove(filePath);        
121         return -1;
122     }
123     curl_easy_cleanup(curl);
124     
125     fclose(fp);
126 
127     return 0;
128 }
129 
130 int main(int argc, char *argv[])
131 {
132     gloale_init();
133     char *serverip = "172.17.6.157";
134     char *port = "22";
135     char *serverpath = "/root/2.xml.bak";
136     char *user = "root";
137     char *passwd = "root@1234";
138     char *savepath = "/root/2.xml";
139     char url[125] = {0};
140     
141     sprintf(url,"sftp://%s:%s/%s",serverip,port,serverpath);
142     printf("url: %s
", url);
143     // download(user,passwd,url,savepath);
144     upload(user,passwd,url,savepath);
145 
146     return 0;
147 }
原文地址:https://www.cnblogs.com/LiuYanYGZ/p/15128158.html