sftp实现文件的下载(依赖libcurl库)
如果想要成功调用libcurl库,则需要将它所依赖的头文件和库文件加载到项目中,如果有需要的话请点击此处获取libcurl库https://pan.baidu.com/s/1ZGaw0NAhmso2Tbt9DGLHwQ#include <stdio.h>#include <stdlib.h>#include <curl.h>#undef DI...
·
如果想要成功调用libcurl库,则需要将它所依赖的头文件和库文件加载到项目中,如果有需要的话请点击此处获取libcurl库
https://pan.baidu.com/s/1ZGaw0NAhmso2Tbt9DGLHwQ
#include <stdio.h>
#include <stdlib.h>
#include <curl.h>
#undef DISABLE_SSH_AGENT
struct FtpFile {
const char *filename;
FILE *stream;
};
static size_t my_fwrite(void *buffer, size_t size, size_t nmemb,
void *stream)
{
struct FtpFile *out=(struct FtpFile *)stream;
if(out && !out->stream) {
out->stream=fopen(out->filename, "wb");
if(!out->stream)
return -1;
}
return fwrite(buffer, size, nmemb, out->stream);
}
int main(void)
{
CURL *curl;
CURLcode res;
const char* urlkey = "用户名:密码"; //服务器用户名及密码
const char* ServerIpPath = "sftp://服务器IP//服务端文件路径";
struct FtpFile ftpfile={"本地文件路径", NULL};
curl_global_init(CURL_GLOBAL_DEFAULT);
curl = curl_easy_init();
if(curl) {
curl_easy_setopt(curl, CURLOPT_URL,ServerIpPath);
curl_easy_setopt(curl, CURLOPT_USERPWD,urlkey);
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, my_fwrite);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &ftpfile);
#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);
}
}
if(ftpfile.stream)
fclose(ftpfile.stream);
curl_global_cleanup();
return 0;
}
更多推荐
所有评论(0)