怎样实现使用c++将图片存入mongodb数据库再取出来
2014-08-15 10:30
681 查看
网上有很多使用c#和java对mongdb进行读取的例子,但是很少有使用c++对其进行开发的例子,由于和c#和java的驱动不同,因此具体的实现方法也不一样,最多是大体的意思想通,而且我试了很久也没有找到直接将图片存入mongdb中的方法,只能是借助GridFS来进行存取,下面是我的代码。由于刚写代码,手法还不娴熟,没有加入错误判断,因此可读性较差,但是功能都已经实现了,供大家茶余饭后消遣了。
#include "stdafx.h"
#include<iostream>
#include<string>
#include<fstream>
#include<mongo\client\dbclient.h>
using namespace mongo;
using namespace bson;
using namespace std;
int _tmain(int argc, _TCHAR* argv[])
{
//连接端口;
DBClientConnection c;
c.connect("localhost");
//将图片转化到字符数组;
char*ffile;
string fileName = "F:\\ceshi\\iphoto.jpg";
ifstream infile;
infile.open(fileName,ios::binary);
infile.seekg(0,ios::end);
int len = infile.tellg();
infile.seekg(0,ios::beg);
ffile = new char[len+1];
memset(ffile, '\0', len+1);
infile.read(ffile, len);
infile.close();
//新建GridFS;
GridFS fs(c,"local");
BSONObj fileobj = fs.storeFile(ffile,len,"photo");
//将值插入数据库;
BSONObjBuilder b;
b.append("name", "peter");
b.append("age", 33);
b.append("photo",fileobj);
BSONObj p = b.obj();
c.insert("local.mydb",p);
delete[]ffile;
//读取图片数据;
string filename ="F:\\ceshi\\output.jpg";
ofstream out;
out.open(filename,ios::binary);
GridFS fsread(c,"local");
fsread.findFile("photo").write(out);
out.close();
return 0;
}
#include "stdafx.h"
#include<iostream>
#include<string>
#include<fstream>
#include<mongo\client\dbclient.h>
using namespace mongo;
using namespace bson;
using namespace std;
int _tmain(int argc, _TCHAR* argv[])
{
//连接端口;
DBClientConnection c;
c.connect("localhost");
//将图片转化到字符数组;
char*ffile;
string fileName = "F:\\ceshi\\iphoto.jpg";
ifstream infile;
infile.open(fileName,ios::binary);
infile.seekg(0,ios::end);
int len = infile.tellg();
infile.seekg(0,ios::beg);
ffile = new char[len+1];
memset(ffile, '\0', len+1);
infile.read(ffile, len);
infile.close();
//新建GridFS;
GridFS fs(c,"local");
BSONObj fileobj = fs.storeFile(ffile,len,"photo");
//将值插入数据库;
BSONObjBuilder b;
b.append("name", "peter");
b.append("age", 33);
b.append("photo",fileobj);
BSONObj p = b.obj();
c.insert("local.mydb",p);
delete[]ffile;
//读取图片数据;
string filename ="F:\\ceshi\\output.jpg";
ofstream out;
out.open(filename,ios::binary);
GridFS fsread(c,"local");
fsread.findFile("photo").write(out);
out.close();
return 0;
}
相关文章推荐
- 怎样在vb.net中将图片存入SQL Server 2000并能读出来使用?
- 怎样在vb.net中将图片存入SQL Server 2000并能读出来使用?
- 使用asp.net将图片上传并存入SqlServer中,然后从SqlServer中读取并显示出来
- C++使用ADO实现存取图片的方法
- 不使用FLASH,怎样用用ASP.net在网站上实现图片切换?
- 自定义ImageView实现类似微信朋友圈发图片点击出来阴影效果;同时使用Picasso加载网络图片
- 【C++ STL应用与实现】18: 怎样使用迭代器适配器
- 使用ImageButton怎样实现鼠标移上去和移开更换图片
- 使用JDBC连接Mysql数据库存入二进制图片并实现读取在PHP中打开展示
- php使用ob_start()实现图片存入变量的方法
- C++ budilder 使用ImageOle.dll 实现richedit显示任意格式图片和连接 gif
- 使用JDBC连接Mysql数据库存入二进制图片并实现读取在PHP中打开展示。
- [C/C++]_[初级]_[把一个图片的数据存入到std::string里,使用_wfopen生成二进制文件]
- 使用asp.net将图片上传并存入SqlServer中,然后从SqlServer中读取并显示出来
- Python实现批量读取图片并存入mongodb数据库的方法示例
- js怎样实现图片的懒加载以及jquery.lazyload.js的使用
- vb.net中将图片存入SQL Server 2000并能读出来使用
- php使用ob_start()实现图片存入变量的方法
- 使用asp.net将图片上传并存入SqlServer中,然后从SqlServer中读取并显示出来
- 使用asp.net将图片上传并存入SqlServer中,然后从SqlServer中读取并显示出来