次のコードを使用して、FTPサーバーからすべてのファイルをダウンロードしました
手順は次のとおりです。1。ファイルのFTPリストを作成する
getFTPList(string sHost, string sUser, string sPass, string sUri)
{
CURL *curl;
CURLcode res;
FILE *ftplister;
string host = "ftp://";
host += sHost;
host += "/sample/";
string furl = host + sUri;
string usrpwd = sUser;
usrpwd += ":";
usrpwd += sPass;
/* local file name to store the file as */
ftplister = fopen("ftp-list", "wb"); /* b is binary, needed on win32 */
curl = curl_easy_init();
if(curl) {
/* Get a file listing from sunet */
curl_easy_setopt(curl, CURLOPT_URL, furl.c_str() );
curl_easy_setopt(curl, CURLOPT_USERPWD, usrpwd.c_str());
curl_easy_setopt(curl, CURLOPT_FTPLISTONLY, TRUE);
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, &write_list);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, ftplister);
res = curl_easy_perform(curl);
/* Check for errors */
if(res != CURLE_OK)
fprintf(stderr, "curl_easy_perform() failed: %s\n",
curl_easy_strerror(res));
/* always cleanup */
curl_easy_cleanup(curl);
}
fclose(ftplister); /* close the local file */
}
このリストを使用して、ダウンロード関数を再帰的に呼び出すファイルをダウンロードします
int main(){ FILE *ftpfile; string line; ftpfile = fopen("ftp-list", "r"); ifstream infile("ftp-list"); while ( getline(infile, line) ) { string url, ofname, surl = "ftp://myhost/uploader/", sfname = "C:\\CNAP\\"; url = surl + line; ofname = sfname +line; cout<<url<<" "<<ofname<<endl; char* theVal ; char* theStr ; theVal = new char [url.size()+1]; theStr = new char [ofname.size()+1]; strcpy(theVal, url.c_str()); strcpy(theStr, ofname.c_str()); downloadFile(theVal, theStr); } return 0; }
今ダウンロード機能:
size_t write_data(void *ptr, size_t size, size_t nmemb, FILE *stream) {
size_t written;
written = fwrite(ptr, size, nmemb, stream);
return written;
}
void downloadFile(const char* url, const char* ofname)
{
CURL *curl;
FILE *fp;
CURLcode res;
curl = curl_easy_init();
if (curl){
fp = fopen(ofname,"wb");
curl_easy_setopt(curl, CURLOPT_URL, url);
curl_easy_setopt(curl, CURLOPT_USERPWD, "user:pass");
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_data);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, fp);
res = curl_easy_perform(curl);
curl_easy_cleanup(curl);
fclose(fp);
}
}
実装するとうまく機能しますが、テキストファイルまたはテキスト付きの一部のファイルをダウンロードする場合にのみ、画像、docx、またはzipまたはrarをダウンロードした場合、またはテキスト以外のファイルをダウンロードした場合、ダウンロード後に開くことができません(無効なファイルを言います)。
何が欠けているのかわかりません。助けていただければ幸いです。
これが非効率的なコーディング方法であることは知っていますが、ダウンロードが正しくなければなりません(任意のファイル)。効率性に取り組むことが私の次の議題です。
PS:ここで使用されているこの方法を使用 しましたC++でlibcurlを使用して複数のファイルをダウンロードします
ありがとうございました