• 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;
    }
  • 相关阅读:
    数据库课程设计
    VB.NET 数组的定义 动态使用 多维数组
    Hadoop学习之配置Eclipse远程调试Hadoop
    2014阿里巴巴研发project师暑期实习生面试经验
    SD卡中FAT32文件格式高速入门(图文具体介绍)
    Java抓取网页数据(原网页+Javascript返回数据)
    WPF中的CheckBox的_ (underscore / 下划线)丢失
    初识EPC
    SharePoint 2013 中代码创建列表查阅项字段
    代码编写逻辑(先伪代码,再带方法的逻辑,最后实现具体方法)(先控制器,再模型)
  • 原文地址:https://www.cnblogs.com/Brickert/p/15175746.html
Copyright © 2020-2023  润新知