曲径通幽论坛

 找回密码
 立即注册
搜索
查看: 5404|回复: 0
打印 上一主题 下一主题

[Curl] FTP 下载

[复制链接]

4918

主题

5880

帖子

3万

积分

GROAD

曲径通幽,安觅芳踪。

Rank: 6Rank: 6

积分
34395
跳转到指定楼层
楼主
发表于 2011-7-3 19:41:27 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
下面代码演示从 FTP 下载一个文件。FTP 地址,登录用户,密码以及文件的路径信息均再命令行中给出。

实际上,对于 curl 来说,用户很多使用无需关心什么什么协议,如 HTTP,FTP 等都由 Curl 库来识别,我们更多要做的是用好那些设置参数。

代码中,主要用到两个参数,CURLOPT_WRITEFUNCTIONCURLOPT_WRITEDATA 。参数中的 "WRITE" 是接收远程文件,写到本地。

为了防止远程服务器上文件不可用而仍然创建空的保存文件,所以在下面的代码中,我们在回调函数中才会实际的创建文件:

测试代码
[C++] 纯文本查看 复制代码
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <curl/curl.h>
#include <curl/easy.h>

struct FTPFile {
    const char *filename;
    FILE *fp;
};
static size_t save_file(void *ptr, size_t size, size_t nmemb, void *userdata)
{
    struct FTPFile *out = (struct FTPFile *)userdata;
    
    if (out && !out->fp) {
        out->fp = fopen(out->filename, "w");
        if (!out->fp)
            return -1;
    }
    return fwrite(ptr, size, nmemb, out->fp);
}

int main(int argc, char **argv)
{
    CURL *curl;
    CURLcode res;
    char *p;

    if (argc != 2 || !!strncmp(argv[1], "ftp://", 6)){
        printf ("Usage: %s [url]ftp://path/your_file[/url]\n", argv[0]);
        exit (EXIT_FAILURE);
    }

    p = strrchr(argv[1], '/');
    p += 1;
    struct FTPFile ftpfile = {
        p,
        NULL
    };

    curl_global_init(CURL_GLOBAL_ALL);

    curl = curl_easy_init();

    curl_easy_setopt(curl, CURLOPT_URL, argv[1]);
        
    curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, save_file);
    curl_easy_setopt(curl, CURLOPT_WRITEDATA, &ftpfile);

    curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L);

    res = curl_easy_perform(curl);

    if (res != CURLE_OK) {
        fprintf (stderr, "Error Ocurred\n");
    }
    
    curl_easy_cleanup(curl);
    curl_global_cleanup();

    fclose(ftpfile.fp);

    return 0;
}

在程序中还使用了 CURLOPT_VERBOSE 参数,这个参数会打印出网络通讯中的详细信息,非常有利于帮助调试。如果程序最后是要发布出到产品的,该选项就无需使用。
运行输出
beyes@debian:~/C/curl$ ./getftpfile ftp://vh491563:2tz92d23@121.15.245.187/www/bbs/favicon.ico
* About to connect() to 121.15.245.187 port 21 (#0)
*   Trying 121.15.245.187... * connected
* Connected to 121.15.245.187 (121.15.245.187) port 21 (#0)
< 220---------- Welcome to Pure-FTPd [privsep] [TLS] ----------
< 220-You are user number 1 of 50 allowed.
< 220-Local time is now 17:26. Server port: 21.
< 220-IPv6 connections are also welcome on this server.
< 220 You will be disconnected after 15 minutes of inactivity.
> USER vh491563
< 331 User vh492363 OK. Password required
> PASS 2tz92d23
< 230-User vh491563 has group access to:  vh491563
< 230 OK. Current restricted directory is /
> PWD
< 257 "/" is your current location
* Entry path is '/'
> CWD www
< 250 OK. Current directory is /www
> CWD bbs
< 250 OK. Current directory is /www/bbs
> EPSV
* Connect data stream passively
< 229 Extended Passive mode OK (|||59625|)
*   Trying 121.15.245.187... * connected
* Connecting to 121.15.245.187 (121.15.245.187) port 59625
> TYPE I
< 200 TYPE is now 8-bit binary
> SIZE favicon.ico
< 213 1758
> RETR favicon.ico
< 150 Accepted data connection
* Maxdownload = -1
* Getting file with size: 1758
* Remembering we are in dir "www/bbs/"
< 226-File successfully transferred
< 226 0.000 seconds (measured here), 39.07 Mbytes per second
* Connection #0 to host 121.15.245.187 left intact
> QUIT
< 221-Goodbye. You uploaded 0 and downloaded 2 kbytes.
< 221 Logout.
* Closing connection #0
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|曲径通幽 ( 琼ICP备11001422号-1|公安备案:46900502000207 )

GMT+8, 2025-5-4 16:44 , Processed in 0.112833 second(s), 23 queries .

Powered by Discuz! X3.2

© 2001-2013 Comsenz Inc.

快速回复 返回顶部 返回列表