摘自: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. 第五课第四周笔记4:Transformer Network变压器网络

    Transformer Network变压器网络 你已经了解了 self attention,你已经了解了 multi headed attention.在这个视频中,让我们把它们放在一起来构建一个变 ...

  2. pagelayout中边界灵敏度动画时间kv

    <PageLayoutWidget>: # 默认是50dp 设置边界 border:'100dp' # 默认哪一页 page:2 # 设置翻页动画及持续时间 anim_kwargs:{'d ...

  3. MySQL:提高笔记-4

    MySQL:提高笔记-4 学完基础的语法后,进一步对 MySQL 进行学习,前几篇为: MySQL:提高笔记-1 MySQL:提高笔记-2 MySQL:提高笔记-3 MySQL:提高笔记-4,本文 说 ...

  4. TensorFlow从入门到入坑(2)

    TensorFlow学习(2) 一.jupyter notebook的安装和使用 1. 什么是jupyter notebook jupyter notebook(http://jupyter.org/ ...

  5. PCIe知识摘要记录

    摘: 一. 在PCIe的Spec中,并没有特别详细的关于Root Complex的定义,从实际的角度来讲,可以把Root Complex理解为CPU与PCIe总线系统通信的媒介.Endpoint处于P ...

  6. Tarjan算法离线 求 LCA(最近公共祖先)

    本文是网络资料整理或部分转载或部分原创,参考文章如下: https://www.cnblogs.com/JVxie/p/4854719.html http://blog.csdn.net/ywcpig ...

  7. Openeuler安装完整man手册

    Openeuler安装完整man手册 ​ 在 Debian 和 Ubuntu 中安装了Shell 前端软件包管理器apt(Advanced Packaging Tool),可以通过如下方式安装. ​ ...

  8. 如何系统学习C 语言(中)之 指针篇

    谈到指针,我们可能会想到钟表上的指针,但这里的指针不是现实生活中看得见摸得着的钟表上的指针,c 语言中的指针只存在于逻辑思维中,物理上并不存在. 同时,指针也是C 语言中最精华的部分,通过灵活地运用指 ...

  9. Java的了解

    Java的基础了解 第一天学习的重要知识点: 1.任何的程序本质有三个:变量:if语句:循环语句. 2.Java的类型:即是编译型也是解释型. 3.JVM:执行bteecode字节码的"虚拟 ...

  10. C++ 函数模板实现原理剖析

    C++ 函数模板实现机制原理剖析 重点 编译器并不是把函数模板处理成能够处理任意类的函数 编译器从函数模板通过具体类型来产生不同的函数 编译器会对函数模板进行两次编译 (1)在声明的位置对模板代码进行 ...