c++ - 如何使用 libcurl 获取文件名?
问题描述
我libcurl
用来下载文件,url可能是下面这样
url = "https://***.com/**/***/abc"
url = "http://***:8000/**/test.txt?***2484d197c16b2e"
url = "http://***:8000/**/test.txt"
获取它的文件名很麻烦,那么有没有办法libcurl
在下载时使用它来获取文件名?我的下载代码如下。
size_t writeData(void *ptr, size_t size, size_t nmemb, FILE *stream) {
size_t written = fwrite(ptr, size, nmemb, stream);
return written;
}
int downloadFile(const char *url, const char *saveFile)
{
CURL *curl;
FILE *saveFd;
CURLcode res;
curl = curl_easy_init();
if (curl) {
saveFd = fopen(saveFile,"wb");
curl_easy_setopt(curl, CURLOPT_URL, url);
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, writeData);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, saveFd);
std::string strurl = string(url);
if (strurl.find("https") != string::npos) {
curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, false);
curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, false);
}
res = curl_easy_perform(curl);
curl_easy_cleanup(curl);
fclose(saveFd);
}
return 0;
}
解决方案
用于CURLOPT_HEADERFUNCTION
获取每个 HTTP 标头的回调。您正在寻找名为Content-Disposition
.
推荐阅读
- server - 存在反向 DNS (PTR) 问题
- c - 使用结构时出现 C 分段错误
- linux - 这个命令有什么作用?ls /bin | 排序 | 三通 /tmp/lista | wc -l
- optimization - 是全局 MINLP 求解器的结果,全局最优
- php - (PHP)为什么这个生成器脚本将起始值+步值添加到起始值?
- javascript - 结合两个特殊的 jQuery 函数
- c++ - 为什么 list.end()-- 不减少迭代器?
- android - 如何在颤动中制作坐标网格?
- swift - [CustomStruct()] 和 [CustomStruct]() 有什么区别?
- python - Python将字典中的负值更改为正值