sftp协议通过调用libcurl库实现文件的上传
2017-09-08 17:00
260 查看
#include <stdio.h>
#include <stdlib.h>
#include <curl.h>
#undef DISABLE_SSH_AGENT
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;
}
int main(void)
{
CURL *curl;
CURLcode res;
const char* urlkey = "用户名:密码"; //服务器用户名密码
FILE* pSendFile = fopen("本地文件路径", "rb");
fseek(pSendFile, 0L, SEEK_END);
size_t iFileSize = ftell(pSendFile);
fseek(pSendFile, 0L, SEEK_SET);
curl_global_init(CURL_GLOBAL_DEFAULT);
curl = curl_easy_init();
if(curl) {
curl_easy_setopt(curl, CURLOPT_URL,
"sftp://服务器IP地址/服务器端的文件路径");
curl_easy_setopt(curl, CURLOPT_USERPWD,urlkey);
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);
#ifndef DISABLE_SSH_AGENT
curl_easy_setopt(curl, CURLOPT_SSH_AUTH_TYPES, CURLSSH_AUTH_PASSWORD);
#endif
curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L);
res = curl_easy_perform(curl);
curl_easy_cleanup(curl);
if(CURLE_OK != res)
{
fprintf(stderr, "curl told us %d\n", res);
}
}
fclose(pSendFile);
curl_global_cleanup();
return 0;
}
#include <stdlib.h>
#include <curl.h>
#undef DISABLE_SSH_AGENT
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;
}
int main(void)
{
CURL *curl;
CURLcode res;
const char* urlkey = "用户名:密码"; //服务器用户名密码
FILE* pSendFile = fopen("本地文件路径", "rb");
fseek(pSendFile, 0L, SEEK_END);
size_t iFileSize = ftell(pSendFile);
fseek(pSendFile, 0L, SEEK_SET);
curl_global_init(CURL_GLOBAL_DEFAULT);
curl = curl_easy_init();
if(curl) {
curl_easy_setopt(curl, CURLOPT_URL,
"sftp://服务器IP地址/服务器端的文件路径");
curl_easy_setopt(curl, CURLOPT_USERPWD,urlkey);
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);
#ifndef DISABLE_SSH_AGENT
curl_easy_setopt(curl, CURLOPT_SSH_AUTH_TYPES, CURLSSH_AUTH_PASSWORD);
#endif
curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L);
res = curl_easy_perform(curl);
curl_easy_cleanup(curl);
if(CURLE_OK != res)
{
fprintf(stderr, "curl told us %d\n", res);
}
}
fclose(pSendFile);
curl_global_cleanup();
return 0;
}
相关文章推荐
- php 通过ssh2协议用sftp实现文件上传、下载
- AS3 下通过HTTP协议 实现Form方式实现二进制文件上传
- 通过SHELL脚本实现SFTP文件自动上传与下载
- (4)通过调用hadoop的java api实现本地文件上传到hadoop文件系统上
- (4)通过调用hadoop的java api实现本地文件上传到hadoop文件系统上
- Ubuntu下通过sftp远程登录linux系统,并实现上传、下载文件
- Ubuntu下通过sftp远程登录linux系统,并实现上传、下载文件
- 腾讯云 CentOS7 使用FileZilla通过SFTP快速实现文件上传
- Ubuntu下通过sftp远程登录linux系统,并实现上传、下载文件
- Android 开发工具类 32_通过 HTTP 协议实现文件上传
- Ubuntu下通过sftp远程登录linux系统,并实现上传、下载文件
- 通过 http 协议上传文件
- Struts2通过commons-fileupload实现文件上传
- 通过Windows批处理脚本实现FTP的多文件自动上传
- 通过解析HTTP协议自己实现文件上传
- C#实现http协议支持上传下载文件的GET、POST请求
- 通过 http 协议上传文件
- 使用SFTP实现Linux上文件上传下载
- 实现支持文件分块多点异步上传的 Web Services 及其客户端(非Web)应用程序调用相关异步执行的 Web Method
- C#实现http协议支持上传下载文件的GET、POST请求