我有 C++ 代码,应该从 URL 下载文件并将其保存在某个目录中。我实现了这个。但我不知道服务器端如何实现。下载文件的网址
C++代码
string dwnld_URL = "127.0.0.1/screenchote.png";
string savepath = "D:\\screenchote.png";
URLDownloadToFile(NULL, dwnld_URL.c_str(), savepath.c_str(), 0, NULL);
dwnload_URl = 我们必须下载文件的网址
savepath = 将文件保存在路径中
服务器端如何制作?
这非常有帮助,但需要一些开发和改进:
#include<tchar.h>
#include<urlmon.h>
#pragma comment (lib,"urlmon.lib")
int main ()
{
HRESULT hr=Urldownloadtofile( NULL, _T("your web page"), _T("c:/web_page.html") 0, NULL );
return 0;
}
或者
HRESULT hr = Urldownloadtofile( NULL, L"your web page", L"c:/web_page.html", 0, NULL );
您可以使用 URLDownloadToFile() 函数从互联网下载任何内容,例如 /files 等...
如果您想将 URL 和文件路径作为变量传递,请使用此方法
#include <iostream>
#include<Windows.h>
#include<string>
#pragma comment(lib, "urlmon.lib")
using namespace std;
int main()
{
// URL & FILE PATH IN STRING
string FilePath = "D:\test\image.png";
string URL = "https://cdn.pixabay.com/photo/2015/12/01/20/28/road-1072823__340.jpg";
wstring tempUrl = wstring(URL.begin(), URL.end());
wstring tempPath = wstring(FilePath.begin(), FilePath.end());
// Applying c_str() method on temp
LPCWSTR wideStringUrl = tempUrl.c_str();
LPCWSTR wideStringPath = tempPath.c_str();
// URL must include the HTTPS/HTTP.
if (S_OK == URLDownloadToFile(NULL, wideStringUrl, wideStringPath, 0, NULL)) {
cout << "Downlod: Succses" << endl;
} else {
cout << "Download: Fails" << endl;
}
return 0;
}
// URL 必须包含 HTTPS/HTTP。
如果你想直接传递URL和保存路径:
#include <iostream>
#include<Windows.h>
#include<string>
#pragma comment(lib, "urlmon.lib")
using namespace std;
int main()
{
if (S_OK == URLDownloadToFile(NULL, L"https://cdn.pixabay.com/photo/2015/12/01/20/28/road-1072823__340.jpg", L"D:\test\image.png", 0, NULL)) {
cout << "Downlod: Succses" << endl;
} else {
cout << "Download: Fails" << endl;
}
return 0;
}
文件路径 = 您想要保存内容的文件的路径。
URL=您要下载的网站地址,例如图片或文本文件的url