怎样实现使用c++将图片存入mongodb数据库再
发布网友
发布时间:2022-05-02 03:02
我来回答
共2个回答
懂视网
时间:2022-05-02 07:23
mongodb存取图片文件功能
标签:支持 面向对象 tac efi eve targe template 关系 bsp
热心网友
时间:2022-05-02 04:31
#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;
}