在ubuntu C ++中使用FTP上传时出现Libcurl错误

时间:2012-10-10 07:43:11

标签: c++ ftp libcurl

在Windows中,此代码可以运行文件,但现在,我想将其转换为ubuntu:

  // callback read function to upload file from local to ftp server
size_t read_callback (void* ptr, size_t size, size_t nmemb, FILE *stream){
    //return fread(ptr,size,nmemb, (FILE*) stream);
    return fread(ptr,size,nmemb,stream);
}

// get file name from a path
string FTPClientConnector::getFileName(string path){
    int length = path.size();
    for(int i = length - 1; i >= 0; i--){
        if(path[i] == '/' || path[i] == '\\'){
            return path.substr(i+1, length-i-1);
        }
    }
}

//function to upload a file to FTP server
int FTPClientConnector::uploadFile(string filePath, string serverPath ){
    CURL *curl;
    CURLcode res;
    FILE *hd_src;
    struct stat file_info;
    curl_off_t fsize;

    char* local_file = new char[filePath.size()+1];
    std::copy(filePath.begin(), filePath.end(), local_file);
    local_file[filePath.size()] = '\0';

    // stat the local file
    if(stat(local_file, &file_info)){
        printf("couldn't open file\n");
        delete local_file;
        return -1;
    }

    // convert URL and username and password to connect to remote server
    string urlPath = this->hostName + serverPath;
    urlPath += getFileName(filePath);
    char *url = new char[urlPath.size() + 1];
    std::copy(urlPath.begin(), urlPath.end(), url);
    url[urlPath.size()] = '\0';

    string userAndPassString = this->userName + ":" + this->password;
    char* usernameAndPassword = new char[userAndPassString.size() +1];
    std::copy(userAndPassString.begin(), userAndPassString.end(), usernameAndPassword);
    usernameAndPassword[userAndPassString.size()] = '\0';

    // get the file to open
    hd_src = fopen(local_file, "rb");

    curl_global_init(CURL_GLOBAL_ALL);
    curl = curl_easy_init();
    if(curl){

        /* specify target */
        curl_easy_setopt(curl,CURLOPT_URL, url);
        curl_easy_setopt(curl, CURLOPT_PORT, this->port);
        curl_easy_setopt(curl, CURLOPT_USERPWD, usernameAndPassword);

          /* we want to use our own read function */
        curl_easy_setopt(curl, CURLOPT_READFUNCTION, read_callback);

        /* enable uploading */
        curl_easy_setopt(curl, CURLOPT_UPLOAD, 1L);

        /* now specify which file to upload */
        curl_easy_setopt(curl, CURLOPT_READDATA, hd_src);

        /* Now run off and do what you've been told! */
        res = curl_easy_perform(curl);

        if(res != CURLE_OK){
            printf("Upload file failed!\n");
            delete local_file;
            delete url;
            delete usernameAndPassword;
            return -1;
        }
        curl_easy_cleanup(curl);
    }
    fclose(hd_src);

    delete local_file;
    delete url;
    delete usernameAndPassword;
    return 0;
}    

这就是我在main.cpp中所说的:

FTPClientConnector connector(host,user,password,port);
connector.uploadFile("xml/kingfisher.xml", "/xml_test_upload");

上面的代码在Ubuntu中不起作用,但有错误:

220 ProFTPD 1.3.4a Server (Debian) [::ffff:10.244.31.244]
500 PUT not understood
500 AUTHORIZATION: not understood
500 HOST: not understood
550 */*: Forbidden command argument
500 TRANSFER-ENCODING: not understood
500 EXPECT: not understood
500 Invalid command: try being more creative
500 2A2 not understood

修改:这是我的Makefile

uploader:
    g++ -o uploader FTPClientConnector.cpp main.cpp -lcurl

2 个答案:

答案 0 :(得分:1)

您的评论中显示您需要使用IPv4。将其添加到setopt调用列表中:

curl_easy_setopt(curl, CURLOPT_IPRESOLVE, CURL_IPRESOLVE_V4);

答案 1 :(得分:1)

输出似乎表明您向FTP服务器说HTTP。确保您的URL正确使用FTP的FTP://前缀,因为没有协议前缀libcurl猜测您需要哪个协议,它默认为HTTP ...