摘自: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\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;
}

curl实现SFTP上传下载文件的更多相关文章

  1. SFTP上传下载文件、文件夹常用操作

    SFTP上传下载文件.文件夹常用操作 1.查看上传下载目录lpwd 2.改变上传和下载的目录(例如D盘):lcd  d:/ 3.查看当前路径pwd 4.下载文件(例如我要将服务器上tomcat的日志文 ...

  2. Java Sftp上传下载文件

    需要使用jar包  jsch-0.1.50.jar sftp上传下载实现类 package com.bstek.transit.sftp; import java.io.File; import ja ...

  3. java实操之使用jcraft进行sftp上传下载文件

    sftp作为临时的文件存储位置,在某些场合还是有其应景的,比如对账文件存放.需要提供一个上传的工具类.实现方法参考下: pom.xml中引入类库: <dependency> <gro ...

  4. THINKPHP 3.2 PHP SFTP上传下载 代码实现方法

     一.SFTP介绍:使用SSH协议进行FTP传输的协议叫SFTP(安全文件传输)Sftp和Ftp都是文件传输协议.区别:sftp是ssh内含的协议(ssh是加密的telnet协议),  只要sshd服 ...

  5. Xshell5下利用sftp上传下载传输文件

    sftp是Secure File Transfer Protocol的缩写,安全文件传送协议.可以为传输文件提供一种安全的加密方法.sftp 与 ftp 有着几乎一样的语法和功能.SFTP 为 SSH ...

  6. SFTP远程连接服务器上传下载文件-qt4.8.0-vs2010编译器-项目实例

    本项目仅测试远程连接服务器,支持上传,下载文件,更多功能开发请看API自行开发. 环境:win7系统,Qt4.8.0版本,vs2010编译器 qt4.8.0-vs2010编译器项目实例下载地址:CSD ...

  7. java:工具(汉语转拼音,压缩包,EXCEL,JFrame窗口和文件选择器,SFTP上传下载,FTP工具类,SSH)

    1.汉语转拼音: import net.sourceforge.pinyin4j.PinyinHelper; import net.sourceforge.pinyin4j.format.HanyuP ...

  8. 【liunx命令】上传下载文件的方法

    scp   帮助命令: man scp   scp功能: 下载远程文件或者目录到本地, 如果想上传或者想下载目录,最好的办法是采用tar压缩一下,是最明智的选择.   从远程主机 下载东西到 本地电脑 ...

  9. WebSSH画龙点睛之lrzsz上传下载文件

    本篇文章没有太多的源码,主要讲一下实现思路和技术原理 当使用Xshell或者SecureCRT终端工具时,我的所有文件传输工作都是通过lrzsz来完成的,主要是因为其简单方便,不需要额外打开sftp之 ...

随机推荐

  1. 天脉2(ACoreOS653)操作系统学习02

    天脉2(ACoreOS653)操作系统学习02 一.分区内通信方法 分区内通信指同一分区内进程之间的通信.ARINC 653定义的分区内进程通信机制,包括:缓存队列(Buffers-Queue).黑板 ...

  2. 【UE4 调试】C++ 常见编译 warnnings/errors

    error LNK2019: unresolved external symbol "" referenced in function 描述 Link错误.无法解析的外部符号 解决 ...

  3. 第6次 Beta Scrum Meeting

    本次会议为Beta阶段第6次Scrum Meeting会议 会议概要 会议时间:2021年6月8日 会议地点:「腾讯会议」线上进行 会议时长:15min 会议内容简介:对完成工作进行阶段性汇报:对下一 ...

  4. Allegro如何测量距离,测距工具的使用

    http://www.allegro-skill.com/thread-2480-1-1.html

  5. Spring---IoC(控制反转)原理学习笔记【全】

    1.IoC创建对象的方式 使用无参构造创建对象 假如要使用有参构造创建: 下标赋值constructor-arg <!--有参--> <bean id="User" ...

  6. ES查询区分大小写

    ES查询区分大小写 ES查询在默认的情况下是不区分大小写的,在5.0版本之后将string类型拆分成两种新的数据类型,text用于全文搜索(模糊搜索),keyword用于关键字搜索(精确搜索). 注意 ...

  7. mdev 响应热插拔事件

    热插拔事件是从内核空间发送到用户空间的通知,一般这时候系统配置出现了变化,比如插入/移除耳机,插入/移除SD卡或者USB存储设备等. 热插拔事件默认会调用/sbin/hotplug来加载驱动程序,创建 ...

  8. 回文链表 牛客网 程序员面试金典 C++ Python

    回文链表 牛客网 程序员面试金典  C++ Python 题目描述 请编写一个函数,检查链表是否为回文. 给定一个链表ListNode* pHead,请返回一个bool,代表链表是否为回文. 测试样例 ...

  9. pascals-triangle-ii leetcode C++

    Given an index k, return the k th row of the Pascal's triangle. For example, given k = 3, Return[1,3 ...

  10. Codeforces 1009E Intercity Travelling | 概率与期望

    题目链接 题目大意: 一个人要从$A$地前往$B$地,两地相距$N$千米,$A$地在第$0$千米处,$B$地在第$N$千米处. 从$A$地开始,每隔$1$千米都有$\dfrac{1}{2}$的概率拥有 ...