本来打算使用QFtp,居然上传不成功
#include <stdio.h>
#include <string.h>
#include <curl/curl.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <errno.h>
#ifdef WIN32
#include <io.h>
#else
#include <unistd.h>
#endif
#define REMOTE_URL "sftp://account:password@xx.xx.xx.xx/"
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, stream);
nread = (curl_off_t)retcode;
fprintf(stderr, "*** We read %" CURL_FORMAT_CURL_OFF_T " bytes from file\n", nread);
return retcode;
}
int main(int argc,char *argv[])
{
if(argc != 3)
{
printf("2 paramenters is needed.\n");
return -1;
}
char* local_file = argv[1];
char* remote_file = argv[2];
printf("call me for update -:%s,%s\n",local_file,remote_file);
char remoteurl[1024] = {0};
strcpy(remoteurl,REMOTE_URL);
strcat(remoteurl,remote_file);
CURL* curl;
CURLcode res;
FILE* file;
struct stat file_info;
curl_off_t fsize;
if(stat(local_file,&file_info))
{
printf("couldnt open '%s': %s\n", local_file, strerror(errno));
return 1;
}
fsize = (curl_off_t)file_info.st_size;
printf("Local file size: %" CURL_FORMAT_CURL_OFF_T " bytes.\n", fsize);
file = fopen(local_file, "rb");
curl_global_init(CURL_GLOBAL_ALL);
curl = curl_easy_init();
if(curl)
{
curl_easy_setopt(curl,CURLOPT_READFUNCTION,read_callback);
curl_easy_setopt(curl,CURLOPT_UPLOAD,1L);
curl_easy_setopt(curl,CURLOPT_URL,remoteurl);
curl_easy_setopt(curl,CURLOPT_READDATA,file);
curl_easy_setopt(curl,CURLOPT_INFILESIZE_LARGE,(curl_off_t)fsize);
curl_easy_setopt(curl,CURLOPT_FTP_RESPONSE_TIMEOUT,120);
res = curl_easy_perform(curl);
if(res != CURLE_OK)
fprintf(stderr,"curl_easy_perform() failed: %s\n",curl_easy_strerror(res));
curl_easy_cleanup(curl);
fprintf(stderr,"finished update.");
}
fclose(file);
curl_global_cleanup();
return 0;
}
以上代码经过实际测试.