卷发上传卡住

发布于 2025-01-31 10:07:57 字数 5368 浏览 4 评论 0 原文

我正在使用Microsoft Visual Studio 2013使用Curl-7.65.0(静态)。 我有以下方法将文件上传到FTP服务器。

bool uploadFile(string sourcePath, string url, string & error)
{
    FILE * file;
    file = fopen(sourcePath.c_str(), "rb");
    if (!file)
    {
        error = "Can not open source file";
        return false;
    }

    struct stat fileInformation;
    if (fstat(fileno(file), &fileInformation) != 0)
    {
        error = "Can not get source file information";
        return false;
    }

    CURL * curl;
    auto curlResultCode = CURLE_FAILED_INIT;
    curl = curl_easy_init();
    if (curl)
    {
        curl_easy_setopt(curl, CURLOPT_URL, url.c_str());
        curl_easy_setopt(curl, CURLOPT_UPLOAD, 1L);
        curl_easy_setopt(curl, CURLOPT_READDATA, file);
        curl_easy_setopt(curl, CURLOPT_INFILESIZE_LARGE, curl_off_t(fileInformation.st_size));
        curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L);
        curl_easy_setopt(curl, CURLOPT_FTP_CREATE_MISSING_DIRS, CURLFTP_CREATE_DIR_RETRY);
        curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 0L);
        curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, 0L);
        curl_easy_setopt(curl, CURLOPT_TIMEOUT, 3600L);
        curlResultCode = curl_easy_perform(curl);
    }

    curl_easy_cleanup(curl);
    fclose(file);
    error = curl_easy_strerror(curlResultCode);
    return curlResultCode != CURLE_OK ? false : true;
}

当我调用 uploadfile 带有以下参数时:

sourcePath:c:/file.zip

url:ftp:// user:[email protected]:21/files/2022/05/20/myfile.zip

I see the directories are created in the FTP server but the file is not uploading最终,我得到 curle_operation_timedout 错误。

这是来自FTP服务器(Xlight FTP服务器)的日志,

05/21/2022 17:26:29 (not login 127.0.0.1<--127.0.0.1:21) "220 Xlight FTP Server 3.5 ready..."
05/21/2022 17:26:29 (not login 127.0.0.1-->127.0.0.1:21) "USER user"
05/21/2022 17:26:29 (not login 127.0.0.1<--127.0.0.1:21) "331 Password required for user"
05/21/2022 17:26:30 (not login 127.0.0.1-->127.0.0.1:21) "PASS *****"
05/21/2022 17:26:30 (user 127.0.0.1<--127.0.0.1:21) "230 Login OK"
05/21/2022 17:26:30 (user 127.0.0.1-->127.0.0.1:21) "PWD"
05/21/2022 17:26:30 (user 127.0.0.1<--127.0.0.1:21) "257 "/""
05/21/2022 17:26:30 (user 127.0.0.1-->127.0.0.1:21) "CWD files"
05/21/2022 17:26:30 (user 127.0.0.1<--127.0.0.1:21) "550 Can't change directory to "/files"."
05/21/2022 17:26:30 (user 127.0.0.1-->127.0.0.1:21) "MKD files"
05/21/2022 17:26:30 (user 127.0.0.1<--127.0.0.1:21) "257 "/files/" directory created."
05/21/2022 17:26:30 (user 127.0.0.1-->127.0.0.1:21) "CWD files"
05/21/2022 17:26:30 (user 127.0.0.1<--127.0.0.1:21) "250 Directory successfully changed"
05/21/2022 17:26:30 (user 127.0.0.1-->127.0.0.1:21) "CWD 2022"
05/21/2022 17:26:30 (user 127.0.0.1<--127.0.0.1:21) "550 Can't change directory to "/files/2022"."
05/21/2022 17:26:30 (user 127.0.0.1-->127.0.0.1:21) "MKD 2022"
05/21/2022 17:26:30 (user 127.0.0.1<--127.0.0.1:21) "257 "/files/2022/" directory created."
05/21/2022 17:26:30 (user 127.0.0.1-->127.0.0.1:21) "CWD 2022"
05/21/2022 17:26:30 (user 127.0.0.1<--127.0.0.1:21) "250 Directory successfully changed"
05/21/2022 17:26:31 (user 127.0.0.1-->127.0.0.1:21) "CWD 05"
05/21/2022 17:26:31 (user 127.0.0.1<--127.0.0.1:21) "550 Can't change directory to "/files/2022/05"."
05/21/2022 17:26:31 (user 127.0.0.1-->127.0.0.1:21) "MKD 05"
05/21/2022 17:26:31 (user 127.0.0.1<--127.0.0.1:21) "257 "/files/2022/05/" directory created."
05/21/2022 17:26:31 (user 127.0.0.1-->127.0.0.1:21) "CWD 05"
05/21/2022 17:26:31 (user 127.0.0.1<--127.0.0.1:21) "250 Directory successfully changed"
05/21/2022 17:26:31 (user 127.0.0.1-->127.0.0.1:21) "CWD 20"
05/21/2022 17:26:31 (user 127.0.0.1<--127.0.0.1:21) "550 Can't change directory to "/files/2022/05/20"."
05/21/2022 17:26:31 (user 127.0.0.1-->127.0.0.1:21) "MKD 20"
05/21/2022 17:26:31 (user 127.0.0.1<--127.0.0.1:21) "257 "/files/2022/05/20/" directory created."
05/21/2022 17:26:31 (user 127.0.0.1-->127.0.0.1:21) "CWD 20"
05/21/2022 17:26:31 (user 127.0.0.1<--127.0.0.1:21) "250 Directory successfully changed"
05/21/2022 17:26:31 (user 127.0.0.1-->127.0.0.1:21) "EPSV"
05/21/2022 17:26:31 (user 127.0.0.1<--127.0.0.1:21) "229 Entering Passive Mode (|||53237|)"
05/21/2022 17:26:33 (user 127.0.0.1-->127.0.0.1:21) "TYPE I"
05/21/2022 17:26:33 (user 127.0.0.1<--127.0.0.1:21) "200 Type set to I."

您可以告诉我这里有什么问题吗?

更新1: 我还添加了读取回调功能,但结果仍然相同。

size_t read_callback(char *ptr, size_t size, size_t nmemb, void *userdata)
{
    FILE *readhere = (FILE *)userdata;
    curl_off_t nread;

    /* copy as much data as possible into the 'ptr' buffer, but no more than
    'size' * 'nmemb' bytes! */
    size_t retcode = fread(ptr, size, nmemb, readhere);

    nread = (curl_off_t)retcode;

    fprintf(stderr, "*** We read %" CURL_FORMAT_CURL_OFF_T
        " bytes from file\n", nread);
    return retcode;
}

curl_easy_setopt(curl, CURLOPT_READFUNCTION, read_callback);
curl_easy_setopt(curl, CURLOPT_READDATA, (void *)file);

提前致谢

I'm using curl-7.65.0 (Static) with Microsoft Visual Studio 2013.
I have the following method to upload files to the FTP server.

bool uploadFile(string sourcePath, string url, string & error)
{
    FILE * file;
    file = fopen(sourcePath.c_str(), "rb");
    if (!file)
    {
        error = "Can not open source file";
        return false;
    }

    struct stat fileInformation;
    if (fstat(fileno(file), &fileInformation) != 0)
    {
        error = "Can not get source file information";
        return false;
    }

    CURL * curl;
    auto curlResultCode = CURLE_FAILED_INIT;
    curl = curl_easy_init();
    if (curl)
    {
        curl_easy_setopt(curl, CURLOPT_URL, url.c_str());
        curl_easy_setopt(curl, CURLOPT_UPLOAD, 1L);
        curl_easy_setopt(curl, CURLOPT_READDATA, file);
        curl_easy_setopt(curl, CURLOPT_INFILESIZE_LARGE, curl_off_t(fileInformation.st_size));
        curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L);
        curl_easy_setopt(curl, CURLOPT_FTP_CREATE_MISSING_DIRS, CURLFTP_CREATE_DIR_RETRY);
        curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 0L);
        curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, 0L);
        curl_easy_setopt(curl, CURLOPT_TIMEOUT, 3600L);
        curlResultCode = curl_easy_perform(curl);
    }

    curl_easy_cleanup(curl);
    fclose(file);
    error = curl_easy_strerror(curlResultCode);
    return curlResultCode != CURLE_OK ? false : true;
}

When i call the uploadFile with following parameters:

sourcePath: c:/file.zip

url: ftp://user:[email protected]:21/files/2022/05/20/myfile.zip

I see the directories are created in the FTP server but the file is not uploading and eventually, I get the CURLE_OPERATION_TIMEDOUT error.

Here is the log from the FTP server (XLight FTP Server)

05/21/2022 17:26:29 (not login 127.0.0.1<--127.0.0.1:21) "220 Xlight FTP Server 3.5 ready..."
05/21/2022 17:26:29 (not login 127.0.0.1-->127.0.0.1:21) "USER user"
05/21/2022 17:26:29 (not login 127.0.0.1<--127.0.0.1:21) "331 Password required for user"
05/21/2022 17:26:30 (not login 127.0.0.1-->127.0.0.1:21) "PASS *****"
05/21/2022 17:26:30 (user 127.0.0.1<--127.0.0.1:21) "230 Login OK"
05/21/2022 17:26:30 (user 127.0.0.1-->127.0.0.1:21) "PWD"
05/21/2022 17:26:30 (user 127.0.0.1<--127.0.0.1:21) "257 "/""
05/21/2022 17:26:30 (user 127.0.0.1-->127.0.0.1:21) "CWD files"
05/21/2022 17:26:30 (user 127.0.0.1<--127.0.0.1:21) "550 Can't change directory to "/files"."
05/21/2022 17:26:30 (user 127.0.0.1-->127.0.0.1:21) "MKD files"
05/21/2022 17:26:30 (user 127.0.0.1<--127.0.0.1:21) "257 "/files/" directory created."
05/21/2022 17:26:30 (user 127.0.0.1-->127.0.0.1:21) "CWD files"
05/21/2022 17:26:30 (user 127.0.0.1<--127.0.0.1:21) "250 Directory successfully changed"
05/21/2022 17:26:30 (user 127.0.0.1-->127.0.0.1:21) "CWD 2022"
05/21/2022 17:26:30 (user 127.0.0.1<--127.0.0.1:21) "550 Can't change directory to "/files/2022"."
05/21/2022 17:26:30 (user 127.0.0.1-->127.0.0.1:21) "MKD 2022"
05/21/2022 17:26:30 (user 127.0.0.1<--127.0.0.1:21) "257 "/files/2022/" directory created."
05/21/2022 17:26:30 (user 127.0.0.1-->127.0.0.1:21) "CWD 2022"
05/21/2022 17:26:30 (user 127.0.0.1<--127.0.0.1:21) "250 Directory successfully changed"
05/21/2022 17:26:31 (user 127.0.0.1-->127.0.0.1:21) "CWD 05"
05/21/2022 17:26:31 (user 127.0.0.1<--127.0.0.1:21) "550 Can't change directory to "/files/2022/05"."
05/21/2022 17:26:31 (user 127.0.0.1-->127.0.0.1:21) "MKD 05"
05/21/2022 17:26:31 (user 127.0.0.1<--127.0.0.1:21) "257 "/files/2022/05/" directory created."
05/21/2022 17:26:31 (user 127.0.0.1-->127.0.0.1:21) "CWD 05"
05/21/2022 17:26:31 (user 127.0.0.1<--127.0.0.1:21) "250 Directory successfully changed"
05/21/2022 17:26:31 (user 127.0.0.1-->127.0.0.1:21) "CWD 20"
05/21/2022 17:26:31 (user 127.0.0.1<--127.0.0.1:21) "550 Can't change directory to "/files/2022/05/20"."
05/21/2022 17:26:31 (user 127.0.0.1-->127.0.0.1:21) "MKD 20"
05/21/2022 17:26:31 (user 127.0.0.1<--127.0.0.1:21) "257 "/files/2022/05/20/" directory created."
05/21/2022 17:26:31 (user 127.0.0.1-->127.0.0.1:21) "CWD 20"
05/21/2022 17:26:31 (user 127.0.0.1<--127.0.0.1:21) "250 Directory successfully changed"
05/21/2022 17:26:31 (user 127.0.0.1-->127.0.0.1:21) "EPSV"
05/21/2022 17:26:31 (user 127.0.0.1<--127.0.0.1:21) "229 Entering Passive Mode (|||53237|)"
05/21/2022 17:26:33 (user 127.0.0.1-->127.0.0.1:21) "TYPE I"
05/21/2022 17:26:33 (user 127.0.0.1<--127.0.0.1:21) "200 Type set to I."

Can you tell me what's wrong here?

Update 1:
I also added the read callback function but still same result.

size_t read_callback(char *ptr, size_t size, size_t nmemb, void *userdata)
{
    FILE *readhere = (FILE *)userdata;
    curl_off_t nread;

    /* copy as much data as possible into the 'ptr' buffer, but no more than
    'size' * 'nmemb' bytes! */
    size_t retcode = fread(ptr, size, nmemb, readhere);

    nread = (curl_off_t)retcode;

    fprintf(stderr, "*** We read %" CURL_FORMAT_CURL_OFF_T
        " bytes from file\n", nread);
    return retcode;
}

curl_easy_setopt(curl, CURLOPT_READFUNCTION, read_callback);
curl_easy_setopt(curl, CURLOPT_READDATA, (void *)file);

Thanks in advance

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文