欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

C++利用libcurl獲取下載文件名稱(chēng)及大小

 更新時(shí)間:2023年03月01日 16:32:37   作者:歐特克_Glodon  
這篇文章主要為大家詳細(xì)介紹了C++如何利用libcurl獲取下載文件名稱(chēng)及大小的功能,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以了解一下

顯示效果

客戶(hù)端利用libcurl庫(kù)下載文件,一般需要預(yù)先知道文件名稱(chēng)及大小以及下載進(jìn)度、下載速度等,以便用戶(hù)通過(guò)界面顯示實(shí)時(shí)下載狀態(tài)及信息。

1、從下載url鏈接獲取文件名字

如何通過(guò)一個(gè)文件的下載鏈接(url)獲取到需要下載的文件名稱(chēng)是我們經(jīng)常要遇到的一個(gè)問(wèn)題。通常是通過(guò)獲取header中的Content-Disposition字段來(lái)進(jìn)行解析獲取。

Content-disposition 是 MIME 協(xié)議的擴(kuò)展,MIME 協(xié)議指示 MIME 用戶(hù)代理如何顯示附加的文件。Content-disposition其實(shí)可以控制用戶(hù)請(qǐng)求所得的內(nèi)容存為一個(gè)文件的時(shí)候提供一個(gè)默認(rèn)的文件名,文件直接在瀏覽器上顯示或者在訪問(wèn)時(shí)彈出文件下載對(duì)話框。

該字段包含了兩種返回值:

1、inline:將文件內(nèi)容直接顯示在頁(yè)面

Content-Disposition: inline;filename=hello.jpg

2、attachment:彈出對(duì)話框讓用戶(hù)下載

Content-Disposition: attachment;filename=hello.jpg

可以看出無(wú)論是哪種返回值,其中都包括了filename,“=”后面就是我們需要的文件名稱(chēng)。但是有時(shí)會(huì)出現(xiàn)獲取到的文件名稱(chēng)存在中文亂碼問(wèn)題(可能包含非 ASCII 字符)。

2、LibCurl官方示例

Example

static size_t header_callback(char *buffer, size_t size,
                              size_t nitems, void *userdata)
{
  /* received header is nitems * size long in 'buffer' NOT ZERO TERMINATED */
  /* 'userdata' is set with CURLOPT_HEADERDATA */
  return nitems * size;
}
 
CURL *curl = curl_easy_init();
if(curl) 
{
  curl_easy_setopt(curl, CURLOPT_URL, "https://example.com");
 
  curl_easy_setopt(curl, CURLOPT_HEADERFUNCTION, header_callback);
 
  curl_easy_perform(curl);
}

3、實(shí)例

// Get the file size and name on the server

size_t header_callback(void *buffer, size_t size, size_t nitems, void *userdata)
{
	string sHeaderData = (char*)buffer;
	string sKey = "Content-Disposition";
	std::size_t nFound = sHeaderData.find(sKey);
	if (nFound != std::string::npos)
	{
		printf("Header data: %s \n", (char*)buffer);
		stringstream ss(sHeaderData);     
		char cSplit = ';'; // 設(shè)定好分隔符號(hào)
		sKey = "filename=";
		vector<string> results; // 用來(lái)存儲(chǔ)結(jié)果
		string strResult;
		while (getline(ss, strResult, cSplit))
		{
			nFound = strResult.find(sKey);
			if (nFound != std::string::npos)
			{
				string sFileName = strResult.substr(nFound + sKey.length());
				sKey = '"';
				nFound = sFileName.find(sKey);
				if (nFound != std::string::npos)
				{
					sFileName = sFileName.substr(1, sFileName.length()-2);
				}
				m_sFileName = sFileName;
				printf("sFileName: %s \n", m_sFileName.c_str());
				break;
			}
		}
	}

	return nitems * size;
}

double getDownloadFileInfo(const string url)
{
    CURL *easy_handle = NULL;
	int ret = CURLE_OK;
	double size = -1;

	do
	{
		easy_handle = curl_easy_init();
		if (!easy_handle)
		{
            ZLOG("curl_easy_init error");
			break;
		}

		// Only get the header data
		curl_easy_setopt(easy_handle, CURLOPT_URL, url.c_str());
		curl_easy_setopt(easy_handle, CURLOPT_CUSTOMREQUEST, "GET");    //使用CURLOPT_CUSTOMREQUEST
		//ret |= curl_easy_setopt(easy_handle, CURLOPT_HEADER, 1L);
		curl_easy_setopt(easy_handle, CURLOPT_NOBODY, 1L);
        //ret |= curl_easy_setopt(easy_handle, CURLOPT_WRITEFUNCTION, nousecb);	

		curl_easy_setopt(easy_handle, CURLOPT_HEADERFUNCTION, header_callback);

		ret = curl_easy_perform(easy_handle);
		if (ret != CURLE_OK)
		{
			char s[100] = {0};
			sprintf_s(s, sizeof(s), "error:%d:%s", ret, curl_easy_strerror(static_cast<CURLcode>(ret)));

            ZLOG(s);
			break;
		}

		// size = -1 if no Content-Length return or Content-Length=0
		ret = curl_easy_getinfo(easy_handle, CURLINFO_CONTENT_LENGTH_DOWNLOAD, &size);

		if (ret != CURLE_OK)
		{
            ZLOG("curl_easy_getinfo error");
			break;
		}

	} while (0);

	curl_easy_cleanup(easy_handle);

	if (size > 0.0)
	{
		downloadFileLength = size;
	}

	return size;
}

到此這篇關(guān)于C++利用libcurl獲取下載文件名稱(chēng)及大小的文章就介紹到這了,更多相關(guān)C++ libcurl獲取文件名稱(chēng)大小內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評(píng)論