sftpプロトコルlibcurlライブラリを呼び出すことでファイルのアップロードを実現

1573 ワード

#include 
#include 
#include 

#undef DISABLE_SSH_AGENT

size_t read_callback(void *ptr, size_t size, size_t nmemb, void *stream) //    
{
    curl_off_t nread;
    size_t retcode = fread(ptr, size, nmemb, (FILE*)(stream));
    nread = (curl_off_t)retcode;
    return retcode;
}

int main(void)
{
    CURL *curl;
    CURLcode res;
    const char* urlkey = "   :  "; //        
    FILE* pSendFile = fopen("      ", "rb");
    fseek(pSendFile, 0L, SEEK_END);
    size_t iFileSize = ftell(pSendFile);
    fseek(pSendFile, 0L, SEEK_SET);

    curl_global_init(CURL_GLOBAL_DEFAULT);

    curl = curl_easy_init();
    if(curl) {
    curl_easy_setopt(curl, CURLOPT_URL,
    "sftp://   IP  /         ");
    curl_easy_setopt(curl, CURLOPT_USERPWD,urlkey);  
    curl_easy_setopt(curl, CURLOPT_READFUNCTION, read_callback);
    curl_easy_setopt(curl, CURLOPT_READDATA, pSendFile);
    curl_easy_setopt(curl, CURLOPT_FTP_CREATE_MISSING_DIRS, 0);
    curl_easy_setopt(curl, CURLOPT_UPLOAD, 1);
    curl_easy_setopt(curl, CURLOPT_INFILESIZE, iFileSize);

    #ifndef DISABLE_SSH_AGENT
        curl_easy_setopt(curl, CURLOPT_SSH_AUTH_TYPES, CURLSSH_AUTH_PASSWORD);
    #endif
        curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L);
    res = curl_easy_perform(curl);

    curl_easy_cleanup(curl);

    if(CURLE_OK != res) 
    {

        fprintf(stderr, "curl told us %d
", res); } } fclose(pSendFile); curl_global_cleanup(); return 0; }