libcurl 库的ftp上传下载代码

  1. //ftp上传实例  
  2. // 服务器地址:192.168.0.185 账号:spider 密码:spider  
  3. // 在服务器路径 a上建立一个a.txt ,本地文件是test.txt  
  4. // 命令行参数192.168.0.185 spider spider a a.txt D:/test.txt  
  5. //#include     
  6. //#include     
  7. //#include     
  8. //#include     
  9. //  
  10. //int debugFun(CURL* curl, curl_infotype type, char* str, size_t len, void* stream)    
  11. //{    
  12. //    //只打印CURLINFO_TEXT类型的信息   
  13. //    if(type == CURLINFO_TEXT)    
  14. //    {    
  15. //        fwrite(str, 1, len, (FILE*)stream);    
  16. //    }    
  17. //    return 0;  
  18. //}    
  19. //  
  20. //int main(int argc, char** argv)    
  21. //{    
  22. //    CURL* curl;    
  23. //    CURLcode res;    
  24. //    char errorBuf[CURL_ERROR_SIZE];    
  25. //    FILE *sendFile, *debugFile;    
  26. //    char ftpurl[256 + 1];    
  27. //    char usrpasswd[64 + 1];    
  28. //  
  29. //    curl_slist *slist=NULL;    
  30. //  
  31. //    if(argc != 7)    
  32. //    {    
  33. //        printf("Usage:\n\t./ftp ip username password ftpPath destFileName srcFile");    
  34. //        return -1;    
  35. //    }    
  36. //  
  37. //    //将相关的调试信息打印到dubugFile.txt中   
  38. //    if(NULL == (debugFile = fopen("debugFile.txt", "a+")))    
  39. //        return -1;    
  40. //  
  41. //    //打开ftp上传的源文件   
  42. //    if(NULL == (sendFile = fopen(argv[6], "r")))    
  43. //    {    
  44. //        fclose(debugFile);    
  45. //        return -1;    
  46. //    }    
  47. //  
  48. //    //获取需要发送文件的大小   
  49. //    fseek(sendFile, 0, SEEK_END);    
  50. //    int sendSize = ftell(sendFile);    
  51. //    if(sendSize < 0)    
  52. //    {    
  53. //        fclose(debugFile);    
  54. //        fclose(sendFile);    
  55. //        return -1;    
  56. //    }    
  57. //    fseek(sendFile, 0L, SEEK_SET);    
  58. //  
  59. //    if((res = curl_global_init(CURL_GLOBAL_ALL)) != 0)    
  60. //    {    
  61. //        fclose(debugFile);    
  62. //        fclose(sendFile);    
  63. //        return -1;    
  64. //    }    
  65. //    if((curl = curl_easy_init()) == NULL)    
  66. //    {    
  67. //        fclose(debugFile);    
  68. //        fclose(sendFile);    
  69. //        curl_global_cleanup();    
  70. //        return -1;    
  71. //    }    
  72. //  
  73. //    if(argv[4][strlen(argv[4]) - 1] != '/')    
  74. //        sprintf(ftpurl, "ftp://%s/%s/%s", argv[1], argv[4], argv[5]);    
  75. //    else   
  76. //        sprintf(ftpurl, "ftp://%s/%s%s", argv[1], argv[4], argv[5]);    
  77. //    curl_easy_setopt(curl, CURLOPT_URL, ftpurl);    
  78. //    //设置ftp上传url,组成如下的URL    
  79. //    //ftp://192.168.31.145//root/subdir/curl/testftp.txt    
  80. //  
  81. //    sprintf(usrpasswd, "%s:%s", argv[2], argv[3]);    
  82. //    curl_easy_setopt(curl, CURLOPT_USERPWD, usrpasswd);    
  83. //  
  84. //    curl_easy_setopt(curl, CURLOPT_VERBOSE, 1);    
  85. //    curl_easy_setopt(curl, CURLOPT_DEBUGDATA, debugFile);    
  86. //  
  87. //    curl_easy_setopt(curl, CURLOPT_READDATA, sendFile);    
  88. //    curl_easy_setopt(curl, CURLOPT_UPLOAD, 1);    
  89. //    curl_easy_setopt(curl, CURLOPT_INFILESIZE, sendSize);    
  90. //    curl_easy_setopt(curl, CURLOPT_FTP_CREATE_MISSING_DIRS, 1);    
  91. //  
  92. //    curl_easy_setopt(curl, CURLOPT_DEBUGFUNCTION, debugFun);    
  93. //  
  94. //    res = curl_easy_perform(curl);    
  95. //    if(0 != res)    
  96. //    {    
  97. //        fclose(sendFile);    
  98. //        fclose(debugFile);    
  99. //        curl_easy_cleanup(curl);    
  100. //        curl_global_cleanup();    
  101. //        return -1;    
  102. //    }    
  103. //  
  104. //    curl_easy_cleanup(curl);    
  105. //    fclose(sendFile);    
  106. //    fclose(debugFile);      
  107. //    curl_global_cleanup();    
  108. //    getchar();  
  109. //    return 0;       
  110. //}   

下载代码:

  1. //ftp下载实例  
  2. #include ;   
  3. #include ;   
  4. #include ;   
  5. #include ;   
  6.    
  7. struct FtpFile   //定义一个结构为了传递给my_fwrite函数.可用curl_easy_setopt的CURLOPT_WRITEDATA选项传递  
  8. {   
  9.         char *filename;   
  10.         FILE *stream;   
  11. };   
  12.    
  13. int my_fwrite(void *buffer, size_t size, size_t nmemb, void *stream)   
  14. {   
  15.         struct FtpFile *out=(struct FtpFile *)stream; // stream指针其实就是指向struct FtpFile ftpfile的  
  16.         if(out && !out->stream)   
  17.         {   
  18.                 out->stream=fopen(out->filename, "wb"); //没有这个流的话就创建一个名字是out->filename.   
  19.                 if(!out->stream)   
  20.                 return -1;   
  21.         }   
  22.         return fwrite(buffer, size, nmemb, out->stream);   
  23. }   
  24.    
  25. int main(int argc, char *argv[])   
  26. {   
  27.         CURL *curl;   
  28.         CURLcode res;    
  29.         struct FtpFile ftpfile={"D:/Success.txt",NULL}; //初始化一个FtpFile结构   
  30.         curl_global_init(CURL_GLOBAL_DEFAULT);   
  31.    
  32.         curl = curl_easy_init();   
  33.         if(curl)   
  34.         {   
  35.                 curl_easy_setopt(curl, CURLOPT_URL,"ftp://192.168.0.185/a/a.txt");    
  36.    
  37.                 curl_easy_setopt(curl, CURLOPT_USERPWD,"spider:spider");  
  38.                 curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, my_fwrite);   
  39.                 curl_easy_setopt(curl, CURLOPT_WRITEDATA, &ftpfile); //给相关函数的第四个参数传递一个结构体的指针  
  40.                 curl_easy_setopt(curl, CURLOPT_VERBOSE, TRUE);//CURLOPT_VERBOSE 这个选项很常用用来在屏幕上显示对服务器相关操作返回的信息  
  41.    
  42.                 res = curl_easy_perform(curl);   
  43.                 curl_easy_cleanup(curl);   
  44.    
  45.                 if(CURLE_OK != res)   
  46.                         fprintf(stderr, "curl told us %d\n", res);   
  47.         }   
  48.         if(ftpfile.stream)   
  49.         fclose(ftpfile.stream);   
  50.         curl_global_cleanup();   
  51.    
  52.         return 0;   
  53. }  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值