libcurl ftp下载实例

转载 2013年12月04日 17:25:42

从ftp站点下载文件的例子.

view plaincopy to clipboardprint?
#include <stdio.h>;  
#include < curl/curl.h>;  
#include <curl/types.h>;  
#include < curl/easy.h>;  

struct FtpFile    //定义一个结构为了传递给my_fwrite函数.可用curl_easy_setopt的CURLOPT_WRITEDATA选项传递  
{   
        char *filename;  
        FILE *stream;  
};  

int  my_fwrite(void *buffer, size_t size, size_t nmemb, void *stream)  
{   
        struct FtpFile *out=(struct FtpFile *)stream; // stream指针其实就是  指向struct FtpFile ftpfile的  
        if(out && !out->stream)   
        {  
                out->stream=fopen(out->filename,  "wb"); //没有这个流的话就创建一个 名字是out->filename.  
                 if(!out->stream)  
                return -1;  
        }   
        return fwrite(buffer, size, nmemb, out->stream);  
}   

int main(int argc, char *argv[])  
{  
        CURL *curl;   
        CURLcode res;  
        struct FtpFile ftpfile={argv[2],NULL};  //初始化一个FtpFile结构   
        curl_global_init(CURL_GLOBAL_DEFAULT);   

        curl = curl_easy_init();  
        if(curl)  
         {  
                curl_easy_setopt(curl, CURLOPT_URL,argv[1]);   

                curl_easy_setopt(curl,  CURLOPT_USERPWD,"user:passwd");
                curl_easy_setopt(curl,  CURLOPT_WRITEFUNCTION, my_fwrite);  
                curl_easy_setopt(curl,  CURLOPT_WRITEDATA, &ftpfile); //给相关函数的第四个参数 传递一个结构体的指针  
                 curl_easy_setopt(curl, CURLOPT_VERBOSE, TRUE); //CURLOPT_VERBOSE 这个选项很常用  用来在屏幕上显示对服务器相关操作返回的信息  

                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;  

 /*****************************************************************************

  FTP上传下载功能

  */

  #include <stdlib.h>

  #include <stdio.h>

  #include <curl/curl.h>

  #include <sys/stat.h>

  /* parse headers for Content-Length */

  size_t getcontentlengthfunc(void *ptr, size_t size, size_t nmemb, void *stream) {

  int r;

  long len = 0;

  /* _snscanf() is Win32 specific */

  //r = _snscanf(ptr, size * nmemb, "Content-Length: %ld/n", &len);

  r = sscanf(ptr, "Content-Length: %ld/n", &len);

  if (r) /* Microsoft: we don't read the specs */

  *((long *) stream) = len;

  return size * nmemb;

  }

  /* discard downloaded data */

  size_t discardfunc(void *ptr, size_t size, size_t nmemb, void *stream) {

  return size * nmemb;

  }

  //write data to upload

  size_t writefunc(void *ptr, size_t size, size_t nmemb, void *stream)

  {

  return fwrite(ptr, size, nmemb, stream);

  }

  /* read data to upload */

  size_t readfunc(void *ptr, size_t size, size_t nmemb, void *stream)

  {

  FILE *f = stream;

  size_t n;

  if (ferror(f))

  return CURL_READFUNC_ABORT;

  n = fread(ptr, size, nmemb, f) * size;

  return n;

  }

  int upload(CURL *curlhandle, const char * remotepath, const char * localpath,

  long timeout, long tries)

  {

  FILE *f;

  long uploaded_len = 0;

  CURLcode r = CURLE_GOT_NOTHING;

  int c;

  f = fopen(localpath, "rb");

  if (f == NULL) {

  perror(NULL);

  return 0;

  }

  curl_easy_setopt(curlhandle, CURLOPT_UPLOAD, 1L);

  curl_easy_setopt(curlhandle, CURLOPT_URL, remotepath);

  if (timeout)

  curl_easy_setopt(curlhandle, CURLOPT_FTP_RESPONSE_TIMEOUT, timeout);

  curl_easy_setopt(curlhandle, CURLOPT_HEADERFUNCTION, getcontentlengthfunc);

  curl_easy_setopt(curlhandle, CURLOPT_HEADERDATA, &uploaded_len);

  curl_easy_setopt(curlhandle, CURLOPT_WRITEFUNCTION, discardfunc);

  curl_easy_setopt(curlhandle, CURLOPT_READFUNCTION, readfunc);

  curl_easy_setopt(curlhandle, CURLOPT_READDATA, f);

  curl_easy_setopt(curlhandle, CURLOPT_FTPPORT, "-"); /* disable passive mode */

  curl_easy_setopt(curlhandle, CURLOPT_FTP_CREATE_MISSING_DIRS, 1L);

  curl_easy_setopt(curlhandle, CURLOPT_VERBOSE, 1L);

  for (c = 0; (r != CURLE_OK) && (c < tries); c++) {

  /* are we resuming? */

  if (c) { /* yes */

  /* determine the length of the file already written */

  /*

  * With NOBODY and NOHEADER, libcurl will issue a SIZE

  * command, but the only way to retrieve the result is

  * to parse the returned Content-Length header. Thus,

  * getcontentlengthfunc(). We need discardfunc() above

  * because HEADER will dump the headers to stdout

  * without it.

  */

  curl_easy_setopt(curlhandle, CURLOPT_NOBODY, 1L);

  curl_easy_setopt(curlhandle, CURLOPT_HEADER, 1L);

  r = curl_easy_perform(curlhandle);

  if (r != CURLE_OK)

  continue;

  curl_easy_setopt(curlhandle, CURLOPT_NOBODY, 0L);

  curl_easy_setopt(curlhandle, CURLOPT_HEADER, 0L);

  fseek(f, uploaded_len, SEEK_SET);

  curl_easy_setopt(curlhandle, CURLOPT_APPEND, 1L);

  }

  else { /* no */

  curl_easy_setopt(curlhandle, CURLOPT_APPEND, 0L);

  }

  r = curl_easy_perform(curlhandle);

  }

  fclose(f);

  if (r == CURLE_OK)

  return 1;

  else {

  fprintf(stderr, "%s/n", curl_easy_strerror(r));

  return 0;

  }

  }

  // 下载

  int download(CURL *curlhandle, const char * remotepath, const char * localpath,

  long timeout, long tries)

  {

  FILE *f;

  curl_off_t local_file_len = -1 ;

  long filesize =0 ;

  CURLcode r = CURLE_GOT_NOTHING;

  struct stat file_info;

  int use_resume = 0;

  //获取本地文件大小信息

  if(stat(localpath, &file_info) == 0)

  {

  local_file_len = file_info.st_size;

use_resume = 1;

  }

  //追加方式打开文件,实现断点续传

  f = fopen(localpath, "ab+");

  if (f == NULL) {

  perror(NULL);

  return 0;

  }

  curl_easy_setopt(curlhandle, CURLOPT_URL, remotepath);

  //连接超时设置

  curl_easy_setopt(curlhandle, CURLOPT_CONNECTTIMEOUT, timeout);

  //设置头处理函数

  curl_easy_setopt(curlhandle, CURLOPT_HEADERFUNCTION, getcontentlengthfunc);

  curl_easy_setopt(curlhandle, CURLOPT_HEADERDATA, &filesize);

  // 设置断点续传

  curl_easy_setopt(curlhandle, CURLOPT_RESUME_FROM_LARGE, use_resume?local_file_len:0);

  curl_easy_setopt(curlhandle, CURLOPT_WRITEFUNCTION, writefunc);

  curl_easy_setopt(curlhandle, CURLOPT_WRITEDATA, f);

  curl_easy_setopt(curlhandle, CURLOPT_NOPROGRESS, 1L);

  curl_easy_setopt(curlhandle, CURLOPT_VERBOSE, 1L);

  r = curl_easy_perform(curlhandle);

  fclose(f);

  if (r == CURLE_OK)

  return 1;

  else {

  fprintf(stderr, "%s/n", curl_easy_strerror(r));

  return 0;

  }

  }

  int main(int c, char **argv) {

  CURL *curlhandle = NULL;

  CURL *curldwn = NULL;

  curl_global_init(CURL_GLOBAL_ALL);

  curlhandle = curl_easy_init();

  curldwn = curl_easy_init();

  upload(curlhandle, "ftp://ffcs:ffcs@135.125.97.42/ipmsg.log", "C://ipmsg.log", 1, 3);

  download(curldwn, "ftp://ffcs:ffcs@135.125.97.42/2009.txt", "C://id.log", 1, 3);

  curl_easy_cleanup(curlhandle);

  curl_easy_cleanup(curldwn);

  curl_global_cleanup();

  return 0;

  }

 

 

 

 

相关文章推荐

libcurl 库的ftp上传和下载代码

昨天考虑好了IM传送图片的方式,用的是FTP做缓存来传递图片,以减少聊天服务器的压力,用的是libcurl库,自己下载了一个Server-U 服务器,下面是测试程序,上传和下载都跑通了,剩下的就是在程...

libcurl 实现ftp客户端不支持下载size大小为0文件----半吊子解决方案

一个酱油程序中需要使用libcurl实现ftp客户端,在下载linux系统/proc目录下的文件时,发现每次下载的内容都是空的。原因是libcurl发现文件size是0(/proc下面的文件就是酱紫的...
  • xly931
  • xly931
  • 2013年11月06日 11:04
  • 1119

libcurl 下载ftp服务器全部文件

原:http://blog.csdn.net/mp295345033/article/details/49487347 最近做的一个任务,项目的需求,需要下载ftp上的全部文件,没有上传,只有下载...

libcurl之ftp上传 & 下载 (转)

源地址:http://my.oschina.net/pathenon/blog/66154 由于项目的需要,利用libcurl编写程序与ftp服务器通信,进行文件上传或下载,愈发地感到curl功能的...
  • vc66vcc
  • vc66vcc
  • 2015年10月09日 12:39
  • 315

libcurl 库的ftp上传和下载代码

昨天考虑好了IM传送图片的方式,用的是FTP做缓存来传递图片,以减少聊天服务器的压力,用的是libcurl库,自己下载了一个Server-U 服务器,下面是测试程序,上传和下载都跑通了,剩下的就是在程...

qt5集成libcurl实现tftp和ftp的方法之四:实现ftp文件上传和下载

详细介绍了在Ubuntu中搭建ftp服务的方法,介绍了vsftpd的安装方法;给出了解决vsftpd 530 Permission denied 的方法;给出了基于libcurl实现ftp上传和下载的...

ios中利用libcurl上传文件到ftp服务器

  • 2017年07月02日 18:51
  • 12.71MB
  • 下载

libcurl进行ftp的demo

  • 2015年01月23日 09:30
  • 15.1MB
  • 下载

libcurl使用ftp的一个例子

#include #include "curl.h" //#include #include #include #include #include #include #define ...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:libcurl ftp下载实例
举报原因:
原因补充:

(最多只允许输入30个字)