摘自: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. [no code] Scrum Meeting 博客目录

    项目 内容 2020春季计算机学院软件工程(罗杰 任健) 2020春季计算机学院软件工程(罗杰 任健) 作业要求 Scrum Meeting博客目录 我们在这个课程的目标是 远程协同工作,采用最新技术 ...

  2. [no code][scrum meeting] Alpha 5

    项目 内容 会议时间 2020-04-10 会议主题 后端技术讨论 会议时长 30min 参会人员 全体成员 $( "#cnblogs_post_body" ).catalog() ...

  3. Noip模拟19(炸裂的开始) 2021.7.18

    T1 u 差分与前缀的综合练习. 分析数据范围,只能是在修改的时候$O(1)$做到,那么只能是像打标记一样处理那个三角形 正解是建立两个二位前缀和,一个控制竖向,一个控制斜向 每次在三角的左上,右下, ...

  4. SVN查看项目修改记录及修改内容

    工具/原料 svn 一,查看修改记录 1 选择要查看的文件夹,打开之后在空白的地方右键. 2 选择svn里面的"查看日志".show_Log 3 在弹出的日志框里,可以看到,你可以 ...

  5. word-ladder leetcoder C++

    Given two words (start and end), and a dictionary, find the length of shortest transformation sequen ...

  6. python基础数据类型语法

    #标识符:我们自己在写代码的时候,取的名字.#项目名 project name#包名 package name#模块名 .py ptthon文件名#规范:由字母数字下划线组成,但不能以数字#简明知意: ...

  7. 使用getopt 解析参数

    getopt被用来解析命令行选项参数. #include <unistd.h> extern char *optarg; //选项的参数指针 extern int optind, //下一 ...

  8. Python基础入门(1)- Python环境搭建与基础语法

    Python编程环境搭建 Python环境搭建 官网下载:https://www.python.org/ python --version PyCharm下载安装 安装 官网下载:https://ww ...

  9. Fiddler抓包工具简介:(三)手机端代理配置

    1.接入网络:需要在移动终端(手机或pad)上指定代理服务器为Fiddler所在主机的IP,端口默认为8888,要保证手机和安装有fiddler的电脑处在同一局域网内,手机能ping通电脑. [方法] ...

  10. css 按钮悬停效霓虹灯特效

    css 按钮悬停效霓虹灯特效 <!DOCTYPE html> <html lang="en"> <head> <meta charset=