zoukankan      html  css  js  c++  java
  • curl实现SFTP上传下载文件

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

      1 #pragma once
      2 #include <stdio.h>
      3 #include <stdlib.h>
      4 #include <string.h>
      5 #include <unistd.h>
      6 #include <pthread.h>
      7 #include <time.h>
      8 #include <sys/stat.h>
      9 #include <signal.h>
     10 
     11 #include <curl/curl.h>
     12 #include <curl/easy.h>
     13 
     14 #include <string.h>
     15 #include <fstream>
     16 #include <iostream>
     17 
     18 /*
     19 g++ 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
     20 */
     21 
     22 using namespace std;
     23 
     24 void gloale_init()
     25 {
     26     
     27     curl_global_init(CURL_GLOBAL_DEFAULT);
     28     return;
     29 }
     30 
     31 size_t read_callback(void *ptr, size_t size, size_t nmemb, void *stream) //回调函数
     32 {
     33     curl_off_t nread;
     34     size_t retcode = fread(ptr, size, nmemb, (FILE*)(stream));
     35     nread = (curl_off_t)retcode;
     36     return retcode;
     37 }
     38 
     39 
     40 size_t write_data(void *ptr, size_t size, size_t nmemb, void *stream)
     41 {
     42     int written = fwrite(ptr, size, nmemb, (FILE *)stream);
     43     return written;
     44 }
     45 
     46 size_t upload(const char * user,const char * passwd,const char * url, const char * path)
     47 {    
     48     CURL *curl;
     49     CURLcode res;
     50     string s1(user);
     51     string s2(passwd);
     52     string s3 = s1 + ":" + s2;
     53     cout<<s3<<endl;    
     54     // system("ls write_file");
     55     FILE* pSendFile = fopen(path,"r");
     56     if(pSendFile == NULL)
     57     {
     58         printf("open failed
    ");
     59         return 1;
     60     }
     61 
     62     fseek(pSendFile, 0L, SEEK_END);
     63 
     64     size_t iFileSize = ftell(pSendFile);
     65 
     66     fseek(pSendFile, 0L, SEEK_SET);
     67     printf("begin easy_init
    "); 
     68  
     69     curl = curl_easy_init();
     70     printf("curl_easy_init success
    ");
     71     if(curl) {
     72         curl_easy_setopt(curl, CURLOPT_URL,url);
     73     curl_easy_setopt(curl, CURLOPT_USERPWD,s3.c_str());  
     74     curl_easy_setopt(curl, CURLOPT_READFUNCTION, read_callback);
     75     curl_easy_setopt(curl, CURLOPT_READDATA, pSendFile);
     76     curl_easy_setopt(curl, CURLOPT_FTP_CREATE_MISSING_DIRS, 0);
     77     curl_easy_setopt(curl, CURLOPT_UPLOAD, 1);
     78     curl_easy_setopt(curl, CURLOPT_INFILESIZE, iFileSize);
     79  
     80     printf("curl_easy_setopt success");
     81     res = curl_easy_perform(curl);
     82  
     83     curl_easy_cleanup(curl);
     84  
     85     if(CURLE_OK != res) 
     86     {
     87  
     88         fprintf(stdout, "curl told us %d
    ", res);
     89     }
     90   }
     91     fclose(pSendFile);
     92     curl_global_cleanup();
     93     return 0;
     94 }
     95 
     96 
     97 int download(const char * user,const char * passw,const char * url,const char * filePath)
     98 {
     99     CURL *curl;
    100     CURLcode curl_code;
    101     string s1(user);
    102     string s2(passw);
    103     string s3 = s1 + ":" + s2;
    104     cout<<s3<<endl;    
    105     curl = curl_easy_init();
    106     curl_easy_setopt(curl, CURLOPT_URL, url);
    107 //    curl_easy_setopt(curl, CURLOPT_USERPWD, s3.c_str());
    108     curl_easy_setopt(curl, CURLOPT_USERNAME, user);
    109     curl_easy_setopt(curl, CURLOPT_PASSWORD, passw);
    110     
    111     FILE *fp = fopen(filePath, "wb+");
    112     if (NULL == fp)
    113     {
    114         curl_easy_cleanup(curl);
    115         printf("fopen failed
    ");
    116         return -1;
    117     }
    118     
    119     curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_data);
    120     curl_easy_setopt(curl, CURLOPT_WRITEDATA, fp);
    121     curl_code = curl_easy_perform(curl);
    122     printf("curl_code = %d
    ",curl_code);
    123     if (CURLE_OK != curl_code)
    124     {
    125         printf("perform failed
    ");
    126         curl_easy_cleanup(curl);
    127         fclose(fp);
    128         remove(filePath);        
    129         return -1;
    130     }
    131     curl_easy_cleanup(curl);
    132     
    133     fclose(fp);
    134 
    135     return 0;
    136 }
    137 
    138 int main(int argc, char *argv[])
    139 {
    140     gloale_init();
    141     char * serverip = "192.168.6.100";
    142     char * port = "22";
    143     char * serverpath = "/root/2.xml.bak";
    144     char *user = "root";
    145     char * passwd = "123456";
    146     char * savepath = "/root/2.xml";
    147     char url[125] = {0};
    148     
    149     sprintf(url,"sftp://%s:%s/%s",serverip,port,serverpath);
    150     printf("url: %s
    ", url);
    151     // download(user,passwd,url,savepath);
    152     upload(user,passwd,url,savepath);
    153 
    154     return 0;
    155 }
  • 相关阅读:
    web框架本质及第一个Django实例
    jQuery练习题HTML文件
    jQuery快速入门
    前端之JS
    前端基础之HTML
    前端基础之css
    并发编程之 协程
    Linux目录结构详解
    第三周 time库
    网络打印机拒绝访问,无法连接处理方法汇总
  • 原文地址:https://www.cnblogs.com/LiuYanYGZ/p/15128093.html
Copyright © 2011-2022 走看看