http://www.linuxidc.com/Linux/2015-09/123609.htm?utm_source=tuicool&utm_medium=referral
libcurl库的使用(通过libcurl库下载url图像)
1. 从这里下载libcurl源码,解压缩;
2. 通过CMake(cmake-gui)生成vs2013 x64位 CURL.sln;
3. 打开CURL.sln,编译会生成libcurl.dll动态库;
4. 在CURL.sln基础上,添加一个testlibcurl控制台工程;
5. testlibcurl.cpp:
#include "stdafx.h"
#include <iostream>
#include <curl/curl.h>
size_t callbackfunction(void *ptr, size_t size, size_t nmemb, void* userdata)
{
FILE* stream = (FILE*)userdata;
if (!stream) {
printf("!!! No stream
");
return 0;
}
size_t written = fwrite((FILE*)ptr, size, nmemb, stream);
return written;
}
bool download_jpeg(char* url)
{
FILE* fp = fopen("out.jpg", "wb");
if (!fp) {
printf("!!! Failed to create file on the disk
");
return false;
}
CURL* curlCtx = curl_easy_init();
curl_easy_setopt(curlCtx, CURLOPT_URL, url);
curl_easy_perform(curlCtx);
char* ctbuf = NULL;
if ( curl_easy_getinfo(curlCtx, CURLINFO_CONTENT_TYPE, &ctbuf) == 0 && ctbuf )
{// image/jpeg
if (strcmp(ctbuf,"image/jpeg")==0)
isJpeg = true;
}
curl_easy_setopt(curlCtx, CURLOPT_WRITEDATA, fp);
curl_easy_setopt(curlCtx, CURLOPT_WRITEFUNCTION, callbackfunction);
curl_easy_setopt(curlCtx, CURLOPT_FOLLOWLOCATION, 1);
CURLcode rc = curl_easy_perform(curlCtx);
if (rc) {
printf("!!! Failed to download: %s
", url);
return false;
}
long res_code = 0;
curl_easy_getinfo(curlCtx, CURLINFO_RESPONSE_CODE, &res_code);
if (!((res_code == 200 || res_code == 201) && rc != CURLE_ABORTED_BY_CALLBACK)) {
printf("!!! Response code: %d
", res_code);
return false;
}
curl_easy_cleanup(curlCtx);
fclose(fp);
return true;
}
size_t write_data(void *ptr, size_t size, size_t nmemb, FILE *stream)
{
size_t written = fwrite(ptr, size, nmemb, stream);
return written;
}
bool download_jpeg2(char* url)
{
CURL *curl;
FILE *fp;
CURLcode res;
char* outfilename = "out2.jpg";
bool isJpeg = false;
curl = curl_easy_init();
if (curl) {
curl_easy_setopt(curl, CURLOPT_URL, url);
res = curl_easy_perform(curl);
char* ctbuf = NULL;
if ( curl_easy_getinfo(curl, CURLINFO_CONTENT_TYPE, &ctbuf) == 0 && ctbuf )
{// image/jpeg
if (strcmp(ctbuf,"image/jpeg")==0)
isJpeg = true;
}
fp = fopen(outfilename, "wb");
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_data);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, fp);
res = curl_easy_perform(curl);
/* always cleanup */
curl_easy_cleanup(curl);
fclose(fp);
}
else {
printf("!!!curl init failed
");
return false;
}
return true;
}
int main(int argc, char* argv[])
{
char* url = "http://f.hiphotos.baidu.com/image/pic/item/d043ad4bd11373f0671f5d95a60f4bfbfbed0493.jpg";
#if 1
if (!download_jpeg3(url)) {
printf("!! Failed to download file: %s
", url);
return -1;
}
#else
if (!download_jpeg2(url)) {
printf("!! Failed to download file: %s
", url);
return -1;
}
#endif
std::cout << "ok!" << std::endl;
return 0;
}
// 补充第3种方式: 先将数据写到字符串中,然后再写成图像文件
char* url = "http://t0.tianditu.com/img_c/wmts/wmts?Service=WMTS&Request=GetTile&Version=1.0.0&Style=Default&Format=tiles&serviceMode=KVP&layer=img&TileMatrixSet=c&TileMatrix=1&TileRow=0&TileCol=0";
long writer2(void *data, long size, int nmemb, void* lpVoid)
{
std::string* content = dynamic_cast<std::string*>((std::string*)lpVoid);
long sizes = size * nmemb;
//string temp;
//temp = std::string((char*)data,sizes);
//content += temp;
content->append((char*)data,sizes);
return sizes;
}
bool CGlbGlobe::download_jpeg3(char* url)
{
CURL *curl;
CURLcode res;
std::string content;
bool isJpeg = false;
curl = curl_easy_init();
if (curl) {
curl_easy_setopt(curl, CURLOPT_URL, url);
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, writer2);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, (void *)&content);
res = curl_easy_perform(curl);
char* ctbuf = NULL;
if ( curl_easy_getinfo(curl, CURLINFO_CONTENT_TYPE, &ctbuf) == 0 && ctbuf )
{// image/jpeg
if (strcmp(ctbuf,"image/jpeg")==0)
isJpeg = true;
}
long retcode = 0;
res = curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE , &retcode);
if ( (res == CURLE_OK) && retcode == 200 )
{
double length = 0;
res = curl_easy_getinfo(curl, CURLINFO_CONTENT_LENGTH_DOWNLOAD , &length);
printf("%d",retcode);
FILE * file = fopen("out2.jpg","wb");
fseek(file,0,SEEK_SET);
fwrite(content.c_str(),1,length,file);
fclose(file);
}
/* always cleanup */
curl_easy_cleanup(curl);
}
else {
printf("!!!curl init failed
");
return false;
}
return true;
}