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

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

    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include <unistd.h>
    #include <pthread.h>
    #include <time.h>
    #include <sys/stat.h>
    #include <signal.h>
    #include <curl/curl.h>
    #include <curl/easy.h>
    #include <string.h>
    
    /*
    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
    */
    
    static void gloale_init(void)
    {
        curl_global_init(CURL_GLOBAL_DEFAULT);
        return;
    }
    
    static size_t read_callback(void *ptr, size_t size, size_t nmemb, void *stream) //回调函数
    {
        curl_off_t nread;
        size_t retcode = fread(ptr, size, nmemb, (FILE*)(stream));
        nread = (curl_off_t)retcode;
        return retcode;
    }
    
    static size_t write_data(void *ptr, size_t size, size_t nmemb, void *stream)
    {
        int written = fwrite(ptr, size, nmemb, (FILE *)stream);
        return written;
    }
    
    static size_t upload(const char *user, const char *passwd, const char *url, const char *path)
    {
        CURL *curl = NULL;
        CURLcode res;
        // char *s3 = NULL;
        // asprintf(&s3, "%s:%s", user, passwd);
        // free(s3);
    
        // system("ls write_file");
        FILE *pSendFile = fopen(path, "r");
        if (pSendFile == NULL)
        {
            printf("open failed
    ");
            return 1;
        }
    
        fseek(pSendFile, 0L, SEEK_END);
    
        size_t iFileSize = ftell(pSendFile);
    
        fseek(pSendFile, 0L, SEEK_SET);
        printf("begin easy_init
    ");
    
        curl = curl_easy_init();
        printf("curl_easy_init success
    ");
        if (curl) {
            curl_easy_setopt(curl, CURLOPT_URL,url);
            // curl_easy_setopt(curl, CURLOPT_USERPWD, s3.c_str());
            curl_easy_setopt(curl, CURLOPT_USERNAME, user);
            curl_easy_setopt(curl, CURLOPT_PASSWORD, passwd);
            curl_easy_setopt(curl, CURLOPT_READFUNCTION, read_callback);
            curl_easy_setopt(curl, CURLOPT_READDATA, pSendFile);
            curl_easy_setopt(curl, CURLOPT_FTP_CREATE_MISSING_DIRS, 0);
            curl_easy_setopt(curl, CURLOPT_UPLOAD, 1);
            curl_easy_setopt(curl, CURLOPT_INFILESIZE, iFileSize);
    
            printf("curl_easy_setopt success
    ");
            res = curl_easy_perform(curl);
    
            curl_easy_cleanup(curl);
    
            if (CURLE_OK != res)
            {
    
                fprintf(stdout, "curl told us %d
    ", res);
            }
        }
        fclose(pSendFile);
        curl_global_cleanup();
        return 0;
    }
    
    static int download(const char *user, const char *passwd, const char *url, const char *filePath)
    {
        CURL *curl = NULL;
        CURLcode curl_code;
        // char *s3 = NULL;
        // asprintf(&s3, "%s:%s", user, passwd);
        // free(s3);
    
        curl = curl_easy_init();
        curl_easy_setopt(curl, CURLOPT_URL, url);
    //    curl_easy_setopt(curl, CURLOPT_USERPWD, s3.c_str());
        curl_easy_setopt(curl, CURLOPT_USERNAME, user);
        curl_easy_setopt(curl, CURLOPT_PASSWORD, passwd);
    
        FILE *fp = fopen(filePath, "wb+");
        if (NULL == fp)
        {
            curl_easy_cleanup(curl);
            printf("fopen failed
    ");
            return -1;
        }
    
        curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_data);
        curl_easy_setopt(curl, CURLOPT_WRITEDATA, fp);
        curl_code = curl_easy_perform(curl);
        printf("curl_code = %d
    ",curl_code);
        if (CURLE_OK != curl_code)
        {
            printf("perform failed
    ");
            curl_easy_cleanup(curl);
            fclose(fp);
            remove(filePath);
            return -1;
        }
        curl_easy_cleanup(curl);
    
        fclose(fp);
    
        return 0;
    }
    
    int main(int argc, char *argv[])
    {
        gloale_init();
        char *serverip = "172.17.6.157";
        char *port = "22";
        char *serverpath = "/root/2.xml.bak";
        char *user = "root";
        char *passwd = "root@1234";
        char *savepath = "/root/2.xml";
        char url[125] = {0};
    
        sprintf(url,"sftp://%s:%s/%s",serverip,port,serverpath);
        printf("url: %s
    ", url);
        // download(user,passwd,url,savepath);
        upload(user,passwd,url,savepath);
    
        return 0;
    }
  • 相关阅读:
    超链接标签、链接地址、锚文本及图片标签
    有序无序列表,div盛放逻辑版块,table表格
    函数的默认值与动态参数arguments的总结
    浏览器中常见的html语义化标签
    html基本介绍,了解html与css,html语法和结构
    js函数与作用域,了解函数基本概念
    JavaScrip流程控制之switch选择,for循环
    JavaScript之if流程控制演练,if写在区间内怎么解决
    JavaScript数据类型typeof()和转换
    C++走向远洋——60(十四周阅读程序、STL中的简单容器和迭代器)
  • 原文地址:https://www.cnblogs.com/Brickert/p/15175746.html
Copyright © 2011-2022 走看看