#include #include #include "../cpp-httplib/httplib.h" #include using namespace std; using namespace httplib; string GetExeDir() { char buff[_MAX_PATH]; _getcwd(buff, _MAX_PATH); string currPath(buff); return string(buff); } const string CLIENT_FILE_DIR = "/ClientFiles/"; void download_file(const string& file_name) { httplib::Client cli("http://localhost:1234"); auto res = cli.Get(("/download/" + file_name).c_str()); if (res && res->status == 200) { string save_path = GetExeDir()+CLIENT_FILE_DIR + file_name; // 打开文件进行写入 ofstream file(save_path, ios::binary); if (!file) { cerr << "Failed to save file: " << save_path << endl; return; } file.write(res->body.c_str(), res->body.size()); file.close(); cout << "File downloaded and saved at: " << save_path << endl; } else { cerr << "Failed to download file. Status code: " << (res ? to_string(res->status) : "No response") << endl; } } int main() { string file_name; while (file_name!="q") { cout << "Enter the file name to download (q to Exit): "; cin >> file_name; download_file(file_name); } system("pause"); return 0; }