您好,登錄后才能下訂單哦!
場景一 保存成圖片
代碼
?static size_t WriteFile(void *ptr, size_t size, size_t nmemb, void *stream)
?{
? std::ofstream* ofs = (std::ofstream*)stream;
? size_t nLen = size * nmemb;
? ofs->write((char*)ptr, nLen);
? return nLen;
?}
?static void TestStorePhotoFileFromUrl()
?{
? std::ofstream ofs;
? ofs.open("aa3ab705-a7d5-4892-b63d-2ccdb54db9a21561977015816.jpg", std::ios::out | std::ios::binary);
? std::string strUrl = "ftp://192.168.11.161:9010/successImage/aa3ab705-a7d5-4892-b63d-2ccdb54db9a21561977015816.jpg";
? std::string strPhotoBuffer;
? CURL *pCurlHandle;
? pCurlHandle = curl_easy_init();
? curl_easy_setopt(pCurlHandle, CURLOPT_URL, strUrl.c_str());
? curl_easy_setopt(pCurlHandle, CURLOPT_WRITEDATA, &ofs);
? curl_easy_setopt(pCurlHandle, CURLOPT_WRITEFUNCTION, WriteFile);
? CURLcode nCurlRet = curl_easy_perform(pCurlHandle);
? if ((nCurlRet != CURLE_OK) && (nCurlRet != CURLE_WRITE_ERROR))
? {
?? std::cout << "通過LibCurl獲取:" << strUrl << "圖片失敗,錯誤碼是:" << nCurlRet;
? }
? ofs.close();
? curl_easy_cleanup(pCurlHandle);
?}
場景二 保存到緩存
代碼
?static size_t WriteBuffer(void *ptr, size_t size, size_t nmemb, void *stream)
?{
? std::string* pStrBuffer = (std::string*)stream;
? size_t nLen = size * nmemb;
? pStrBuffer->append((char*)ptr, nLen);
? return nLen;
?}
?static void TestStoreBufferFromUrl()
?{
? std::string strUrl = "ftp://192.168.11.161:9010/successImage/aa3ab705-a7d5-4892-b63d-2ccdb54db9a21561977015816.jpg";
? std::string strPhotoBuffer;
? CURL *pCurlHandle;
? pCurlHandle = curl_easy_init();
? curl_easy_setopt(pCurlHandle, CURLOPT_URL, strUrl.c_str());
? curl_easy_setopt(pCurlHandle, CURLOPT_WRITEDATA, &strPhotoBuffer);
? curl_easy_setopt(pCurlHandle, CURLOPT_WRITEFUNCTION, WriteBuffer);
? CURLcode nCurlRet = curl_easy_perform(pCurlHandle);
? if ((nCurlRet != CURLE_OK) && (nCurlRet != CURLE_WRITE_ERROR))
? {
?? std::cout << "通過LibCurl獲取:" << strUrl << "圖片失敗,錯誤碼是:" << nCurlRet;
? }
? else
? {
?? std::ofstream ofs;
?? ofs.open("aa3ab705-a7d5-4892-b63d-2ccdb54db9a21561977015816.jpg", std::ios::out | std::ios::binary);
?? ofs << strPhotoBuffer;
?? ofs.close();
? }
? curl_easy_cleanup(pCurlHandle);
?}
注意:
std::ios::binary必須攜帶,否則圖片寫文件有丟失
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。