添加项目文件。
parent
13c0535528
commit
486ed28c56
@ -0,0 +1,55 @@
|
|||||||
|
#include <iostream>
|
||||||
|
#include <fstream>
|
||||||
|
#include "../cpp-httplib/httplib.h"
|
||||||
|
#include <direct.h>
|
||||||
|
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;
|
||||||
|
}
|
@ -0,0 +1,27 @@
|
|||||||
|
<?xml version="1.0" encoding="utf-8"?>
|
||||||
|
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
|
||||||
|
<ItemGroup>
|
||||||
|
<Filter Include="源文件">
|
||||||
|
<UniqueIdentifier>{4FC737F1-C7A5-4376-A066-2A32D752A2FF}</UniqueIdentifier>
|
||||||
|
<Extensions>cpp;c;cc;cxx;def;odl;idl;hpj;bat;asm;asmx</Extensions>
|
||||||
|
</Filter>
|
||||||
|
<Filter Include="头文件">
|
||||||
|
<UniqueIdentifier>{93995380-89BD-4b04-88EB-625FBE52EBFB}</UniqueIdentifier>
|
||||||
|
<Extensions>h;hh;hpp;hxx;hm;inl;inc;xsd</Extensions>
|
||||||
|
</Filter>
|
||||||
|
<Filter Include="资源文件">
|
||||||
|
<UniqueIdentifier>{67DA6AB6-F800-4c08-8B7A-83BB121AAD01}</UniqueIdentifier>
|
||||||
|
<Extensions>rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms</Extensions>
|
||||||
|
</Filter>
|
||||||
|
</ItemGroup>
|
||||||
|
<ItemGroup>
|
||||||
|
<ClCompile Include="client.cpp">
|
||||||
|
<Filter>源文件</Filter>
|
||||||
|
</ClCompile>
|
||||||
|
</ItemGroup>
|
||||||
|
<ItemGroup>
|
||||||
|
<ClInclude Include="..\cpp-httplib\httplib.h">
|
||||||
|
<Filter>头文件</Filter>
|
||||||
|
</ClInclude>
|
||||||
|
</ItemGroup>
|
||||||
|
</Project>
|
File diff suppressed because it is too large
Load Diff
@ -0,0 +1,75 @@
|
|||||||
|
#include <iostream>
|
||||||
|
#include <fstream>
|
||||||
|
#include "../cpp-httplib/httplib.h"
|
||||||
|
#include <filesystem>
|
||||||
|
#include <vector>
|
||||||
|
#include <string>
|
||||||
|
#include <direct.h>
|
||||||
|
#include <iostream>
|
||||||
|
|
||||||
|
|
||||||
|
using namespace std;
|
||||||
|
using namespace httplib;
|
||||||
|
|
||||||
|
const string SERVER_FILE_DIR = "/ServerFiles/";
|
||||||
|
|
||||||
|
string GetExeDir()
|
||||||
|
{
|
||||||
|
char buff[_MAX_PATH];
|
||||||
|
_getcwd(buff, _MAX_PATH);
|
||||||
|
string currPath(buff);
|
||||||
|
return string(buff);
|
||||||
|
}
|
||||||
|
|
||||||
|
bool file_exists(const string& path) {
|
||||||
|
ifstream file(path, ios::binary);
|
||||||
|
return file.good();
|
||||||
|
}
|
||||||
|
|
||||||
|
void send_file(const string& file_path, Response& res) {
|
||||||
|
ifstream file(file_path, ios::binary | ios::ate);
|
||||||
|
if (!file) {
|
||||||
|
res.status = 500; // 服务器错误
|
||||||
|
res.set_content("Failed to open file", "text/plain");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
// 获取文件大小
|
||||||
|
streamsize file_size = file.tellg();
|
||||||
|
file.seekg(0, ios::beg);
|
||||||
|
|
||||||
|
res.set_content_provider(
|
||||||
|
file_size, "application/octet-stream",
|
||||||
|
[file_path](size_t offset, size_t length, DataSink& sink) {
|
||||||
|
ifstream file(file_path, ios::binary);
|
||||||
|
if (!file) return false;
|
||||||
|
|
||||||
|
file.seekg(offset, ios::beg);
|
||||||
|
vector<char> buffer(length);
|
||||||
|
file.read(buffer.data(), length);
|
||||||
|
sink.write(buffer.data(), file.gcount()); // 发送数据
|
||||||
|
|
||||||
|
return true; // 继续传输
|
||||||
|
}
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
int main() {
|
||||||
|
Server svr;
|
||||||
|
|
||||||
|
svr.Get(R"(/download/(.+))", [&](const Request& req, Response& res) {
|
||||||
|
string filename = req.matches[1];
|
||||||
|
string file_path = GetExeDir()+SERVER_FILE_DIR + filename;
|
||||||
|
|
||||||
|
if (!file_exists(file_path)) {
|
||||||
|
res.status = 404;
|
||||||
|
res.set_content("File not found", "text/plain");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
send_file(file_path, res);
|
||||||
|
});
|
||||||
|
|
||||||
|
cout << "Server started at http://localhost:1234\n";
|
||||||
|
svr.listen("localhost", 1234);
|
||||||
|
}
|
@ -0,0 +1,27 @@
|
|||||||
|
<?xml version="1.0" encoding="utf-8"?>
|
||||||
|
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
|
||||||
|
<ItemGroup>
|
||||||
|
<Filter Include="源文件">
|
||||||
|
<UniqueIdentifier>{4FC737F1-C7A5-4376-A066-2A32D752A2FF}</UniqueIdentifier>
|
||||||
|
<Extensions>cpp;c;cc;cxx;def;odl;idl;hpj;bat;asm;asmx</Extensions>
|
||||||
|
</Filter>
|
||||||
|
<Filter Include="头文件">
|
||||||
|
<UniqueIdentifier>{93995380-89BD-4b04-88EB-625FBE52EBFB}</UniqueIdentifier>
|
||||||
|
<Extensions>h;hh;hpp;hxx;hm;inl;inc;xsd</Extensions>
|
||||||
|
</Filter>
|
||||||
|
<Filter Include="资源文件">
|
||||||
|
<UniqueIdentifier>{67DA6AB6-F800-4c08-8B7A-83BB121AAD01}</UniqueIdentifier>
|
||||||
|
<Extensions>rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms</Extensions>
|
||||||
|
</Filter>
|
||||||
|
</ItemGroup>
|
||||||
|
<ItemGroup>
|
||||||
|
<ClCompile Include="server.cpp">
|
||||||
|
<Filter>源文件</Filter>
|
||||||
|
</ClCompile>
|
||||||
|
</ItemGroup>
|
||||||
|
<ItemGroup>
|
||||||
|
<ClInclude Include="..\cpp-httplib\httplib.h">
|
||||||
|
<Filter>头文件</Filter>
|
||||||
|
</ClInclude>
|
||||||
|
</ItemGroup>
|
||||||
|
</Project>
|
Loading…
Reference in New Issue