curl实现SFTP上传下载文件
阅读原文时间:2023年07月08日阅读:3

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

#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include

/*
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\\n");  
    return 1;  
}

fseek(pSendFile, 0L, SEEK\_END);

size\_t iFileSize = ftell(pSendFile);

fseek(pSendFile, 0L, SEEK\_SET);  
printf("begin easy\_init\\n");

curl = curl\_easy\_init();  
printf("curl\_easy\_init success\\n");  
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\\r\\n");  
    res = curl\_easy\_perform(curl);

    curl\_easy\_cleanup(curl);

    if (CURLE\_OK != res)  
    {

        fprintf(stdout, "curl told us %d\\n", 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\\n");  
    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\\n",curl\_code);  
if (CURLE\_OK != curl\_code)  
{  
    printf("perform failed\\n");  
    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\\n", url);  
// download(user,passwd,url,savepath);  
upload(user,passwd,url,savepath);

return 0;  

}

手机扫一扫

移动阅读更方便

阿里云服务器
腾讯云服务器
七牛云服务器

你可能感兴趣的文章