libcurl库的使用(通过libcurl库下载url图像) 【转】
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\n");
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\n");
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\n", 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\n", 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\n");
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\n", url);
return -1;
}
#else
if (!download_jpeg2(url)) {
printf("!! Failed to download file: %s\n", 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\n");
return false;
}
return true;
}
libcurl库的使用(通过libcurl库下载url图像) 【转】的更多相关文章
- 【转载】HTTP/FTP客户端开发库:libwww、libcurl、libfetch
网页抓取和ftp访问是目前很常见的一个应用需要,无论是搜索引擎的爬虫,分析程序,资源获取程序,WebService等等都是需 要的,自己开发抓取库当然是最好了,不过开发需要时间和周期,使用现有的Ope ...
- VC9、VC11、VC14、VC15库 32位 64位 免费下载
VC9.VC11.VC14.VC15库 32位 64位 免费下载 更新版本的PHP是用VC11,VC14或VC15(分别为Visual Studio 2012,2015或2017编译器)构建的,并且包 ...
- 配置自己的OpenGL库,glew、freeglut库编译,库冲突解决(附OpenGL Demo程序)
平台:Windows7,Visual C++ 2010 1. 引言 实验室的一个项目,用到OpenGL进行实时绘制,还用到一些其他的库,一个困扰我很久的问题就是编译时遇到的各种符号未定义,符号重定义之 ...
- 如何实现SP文档库类似百度文档库的效果 (副标题:如何在SP2013文档库的SWF文件用FlexPager显示)
1. 编辑文档库列表显示页面,如下图: 2. 添加内容编辑器,如下图: 3. 添加如下在[内容编辑器中]-[编辑源],添加如下JS代码,如下图: 代码如下: <scrip type=&quo ...
- boost库的安装,使用,介绍,库分类
1)首先去官网下载boost源码安装包:http://www.boost.org/ 选择下载对应的boost源码包.本次下载使用的是 boost_1_60_0.tar.gz (2)解压文件:tar - ...
- [置顶] android利用jni调用第三方库——第三篇——编写库android程序整合第三方库libhello.so到自己的库libhelloword.so
0:前言: 在第二篇中,我们主要介绍了丙方android公司利用乙方C++公司给的动态库,直接调用库中的方法,但是这样方式受限于: 乙方C++公司开发的动态库是否符合jni的规范,如果不规范,则不能直 ...
- python 库安装方法及常用库
python 库安装方法及常用库 python库安装方法: 方法一:setpu.py 1.下载库压缩包,解压,记录下路径:*:/**/……/ 2.运行cmd,切换到*:/**/……/目录下 3.运行s ...
- Android Studio中通过CMake使用NDK并编译自定义库和添加预编译库
Note:这篇文章是基于Android Studio 3.01版本的,NDK是R16. step1:创建一个包含C++的项目 其他默认就可以了. C++ Standard 指定编译库的环境,其中Too ...
- 目前以lib后缀的库有两种,一种为静态链接库(Static Libary,以下简称“静态库”),另一种为动态连接库(DLL,以下简称“动态库”)的导入库(Import Libary,以下简称“导入库”)。静态库是一个或者多个obj文件的打包
前以lib后缀的库有两种,一种为静态链接库(Static Libary,以下简称“静态库”),另一种为动态连接库(DLL,以下简称“动态库”)的导入库(Import Libary,以下简称“导入库”) ...
随机推荐
- WIN8.1优化
用WIN8.1至今,总结的所有优化的办法! 从Win7.Win8.1开始,微软为我们带来完善的系统服务.任务计划程序,这些都一定程度提升了日常使用,实现了“半自动化.半智能化”交互体验.但是对于高级用 ...
- Codeforces Round #433
我会4题,写了两题,还提交错误n次,掉了40rating(哭丧脸),又被学长D飞了. 学长:我很心疼你的成绩啊: 我:第四题忘记加特判了... 学长:暴力还能写挂. 我:...... ———————— ...
- C/C++里的const(2)
对于如下几个语句,哪些定义相同?哪些定义不同?哪些数据可修改?哪些数据不可修改呢? const int a; int const a; const int *a; int *const a; int ...
- camera摄像原理之二:色彩空间【转】
转自:http://blog.csdn.net/ghostyu/article/details/7912854 对于sensor 来说,我们经常接触到的色彩空间的概念,主要是RGB , YUV这两种( ...
- Linux : select()详解 和 实现原理【转】
转自:http://blog.csdn.net/huntinux/article/details/39289317 原文:http://blog.csdn.net/boboiask/article/d ...
- logging模块的使用
# -*- coding: utf-8 -*- import os import time import logging import sys log_dir1=os.path.join(os.pat ...
- mysql索引作用的简单理解
转自:http://blog.csdn.net/pengsidong/article/details/62104703,有添加 索引好比书的目录,好比新华字典的拼音.偏旁部首查字,可以帮助人快速查找到 ...
- Linux下f命令配置
一.工具 f 的配置 使用 ========== ========== ========== ========== ========== ========== ==== 一.配置方法: 首先在lin ...
- jQuery,月历,左右点击事件实现月份的改变
html页面: <div class="recordbriefing-title-top-body"> <span class="record-left ...
- KVM(七)使用 libvirt 做 QEMU/KVM 快照和 Nova 实例的快照
本文将梳理 QEMU/KVM 快照相关的知识,以及在 OpenStack Nova 中使用 libvirt 来对 QEMU/KVM 虚机做快照的过程. 1. QEMU/KVM 快照 1.1 概念 QE ...