ubuntu+codeblocks+libcurl FTP上传文件

以前没接触过curl,但是现在要在linux下上传到FTP文件,查了查都说curl比较好,那就研究研究

主要参考的这几篇文章:

http://blog.csdn.net/jgood/article/details/4787670

http://blog.csdn.net/sever2012/article/details/7076248

确实提供了很大的帮助,也有一点点自己摸索的地方,在此记录一下:


ubuntu版本13.10,codeblocks版本12.11,libcurl版本7.32.0


首先需要在ubuntu上安装libcurl,sudo apt-get install libcurl4-gnutls-dev

其实叫libcurl4的包有三个,libcurl4-nss-dev libcurl4-gnutls-dev libcurl4-openssl-dev

我才不会说我安装的这个是我随便挑的一个呢。。


安装完之后在codeblocks里新建一个工程,为什么用codeblocks,省事呗。。

打开菜单栏里的Project->Build options

点你的工程名,不要在debug或release上,省的要改两遍

Compiler Flags->Other options加上`curl-config --cflags`

Linker settings->Other linker options加上`curl-config --libs`

Search directories->Compiler加上个/usr/include(这个因人而异)

Search directories->Linker加上个/usr/lib(这个因人而异)

大功告成,确定


下面直接贴代码好了

#include <iostream>
#include <curl/curl.h>

using namespace std;

size_t read_data(void *buffer, size_t size, size_t nmemb, void *user_p)
{
    return fread(buffer, size, nmemb, (FILE *)user_p);
}

int main()
{
    //初始化libcurl
    CURLcode code;
    code = curl_global_init(CURL_GLOBAL_ALL);
    if (CURLE_OK != code)
    {
        cerr<<"init libcurl failed"<<endl;
        return -1;
    }

    FILE *fp = fopen("要上传的文件路径", "rb");
    if (NULL == fp)
    {
        cout<<"can not open file"<<endl;
        curl_global_cleanup();
        return -1;
    }

    //获取文件大小
    fseek(fp, 0, 2);
    int file_size = ftell(fp);
    rewind(fp);

    //获取easy handle
    CURL *easy_handle = curl_easy_init();
    if (NULL == easy_handle)
    {
        cerr<<"get a easy handle failed"<<endl;
        fclose(fp);
        curl_global_cleanup();
        return -1;
    }

    //设置easy handle属性
    curl_easy_setopt(easy_handle, CURLOPT_URL, "ftp://IP/文件夹名/文件名");
    curl_easy_setopt(easy_handle, CURLOPT_UPLOAD, 1);
    curl_easy_setopt(easy_handle, CURLOPT_USERPWD, "用户名:密码");
    //curl_easy_setopt(easy_handle, CURLOPT_VERBOSE, 1);//打印debug信息
    curl_easy_setopt(easy_handle, CURLOPT_FTP_CREATE_MISSING_DIRS, 1);
    curl_easy_setopt(easy_handle, CURLOPT_READFUNCTION, &read_data);
    curl_easy_setopt(easy_handle, CURLOPT_READDATA, fp);
    curl_easy_setopt(easy_handle, CURLOPT_INFILESIZE, file_size);

    //执行数据请求
    code = curl_easy_perform(easy_handle);
    if (code == CURLE_OK)
    {
        cout<<"upload successfully"<<endl;
    }
    else
    {
        cout<<code<<endl;
        cout<<curl_easy_strerror(code)<<endl;
    }

    //释放资源
    fclose(fp);
    curl_easy_cleanup(easy_handle);
    curl_global_cleanup();

    return 0;
}

在这里说一说我遇到的小问题吧

代码里最后一个curl_easy_setopt函数里,之前的大神博客里给的参数是CURLOPT_INFILELSIZE_LARGE,可能是我上传的文件比较小的原因吧,老是报CURLE_PARTIAL_FILE,说我Transferred a partial file,冥思苦想不得其解。。把代码里的打印debug信息的那一行注释回来发现文件块的大小不符,自作主张把LARGE干掉,一切正常。。


其他也没什么好说了,就是libcurl的相关文章不太好找,希望大家多多分享吧

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值