所以我想出了如何使用以下代码从互联网上获取远程文件的文件大小:
size_t readHeader(char* header, size_t size, size_t nitems, void *userdata) {
// this works I get the file size when Content-length is available
if (startsWith(header, "Content-Length:")) {
std::string header_in_string = replaceAll(header, "Content-Length:", "");
long size = atol(header_in_string.c_str());
*file_size = size;
}
else { // do something else }
return size * nitems;
}
// main function
if (curl) {
fp = fopen(temp_file_name, "wb");
curl_easy_setopt(curl, CURLOPT_URL, url);
curl_easy_setopt(curl, CURLOPT_CAINFO, "./ca-bundle.crt");
curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, false);
curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, false);
curl_easy_setopt(curl, CURLOPT_HEADERDATA, this);
curl_easy_setopt(curl, CURLOPT_HEADERFUNCTION, readHeader);
// writing the downloading content to file
curl_easy_setopt(curl, CURLOPT_WRITEDATA, fp);
res = curl_easy_perform(curl);
curl_easy_cleanup(curl);
fclose(fp);
}
这通过GET提供了C#中的解决方案 Getting remote file size without content-length,这也提供了PHP https://www.perlmonks.org/?node_id=641735中的解决方案 但我想知道C ++或Java或python中的解决方案,最好使用libcurl。
使用此链接,我得到了真实的文件大小https://stackoverflow.com/questions/ask?wizard=1 但http://ag.teamdna.de/files/advancedgenetics-1.4.3-1.6.jar提供0或-1