编译环境:Win7 VS2010
Mongodb版本:2.4.6
C++ Driver版本:2.4.6
Java Driver版本:2.4
在C++客户端写入Mongodb记录后,Java作为展示端,读取mongodb中的数据。当记录为英文时,没有任何问题;当记录出现中文时,C++读取正常,Java端读取为乱码。
因为mongodb中的编码时utf-8,所以尝试在Java端,将utf-8转换为GBK,结果没有成功。后来尝试在C++写入时,将unicode转换为utf-8,再写入mongodb,则Java读取正常。C++端读取的时候,需要将utf-8转换回unicode即可。
下面是一个demo:
#include "dbclient.h" // the mongo c++ driver #include <fstream> #include <iostream> #include <clocale> using namespace std; using namespace mongo; using namespace bson; using std::wcout; void UnicodeToUTF8(char *utf, wchar_t* wszString) { //预转换,得到所需空间的大小 int u8Len = ::WideCharToMultiByte(CP_UTF8, NULL, wszString, wcslen(wszString), NULL, 0, NULL, NULL); //同上,分配空间要给'\0'留个空间 //UTF8虽然是Unicode的压缩形式,但也是多字节字符串,所以可以以char的形式保存 //转换 //unicode版对应的strlen是wcslen ::WideCharToMultiByte(CP_UTF8, NULL, wszString, wcslen(wszString), utf, u8Len, NULL, NULL); //最后加上'\0' utf[u8Len] = '\0'; } void UTF8ToUnicode(wchar_t *unicode, char *utf) { //utf8-unicode //预转换,得到所需空间的大小 int wcsLen = ::MultiByteToWideChar(CP_UTF8, NULL, utf, strlen(utf), NULL, 0); //分配空间要给'\0'留个空间,MultiByteToWideChar不会给'\0'空间 //转换 ::MultiByteToWideChar(CP_UTF8, NULL, utf, strlen(utf), unicode, wcsLen); //最后加上'\0' unicode[wcsLen] = '\0'; //输出测试一下 locale::global(locale("")); //init 放在main函数第一行最好 wcout.imbue(locale("")); wcout << unicode; wcout.clear(); wcout<<endl; //注意千万不要写成wcout << wszString << endl;否则中文无法输出 } int main() { cout << "connecting to localhost..." << endl; DBClientConnection c; c.connect("localhost"); //100.121.25.23:27400 cout << "connected ok" << endl; //存入文件 string fileName = "C:\\WCSFiles\\1.jpg"; //"D:\\DownLoads\\P6.rar";// std::ifstream file; file.open(fileName, std::ios::binary); file.seekg(0,std::ios::end); //设置文件指针位置到文件尾 int len = file.tellg(); //读取文件指针的位置,获取文件长度 file.seekg(0, std::ios::beg); char *file_buf = new char[len+1]; memset(file_buf, 0, len+1); file.read(file_buf, len); file.close(); GridFS fs (c, "test"); BSONObj fileObj = fs.storeFile(file_buf, len, fileName); //保存文件 //关联文件属性 char well_name[128] = {0}; wchar_t* wszString = L"ss测试井111"; UnicodeToUTF8(well_name, wszString);//unicode to UTF8 // string well_name = "天津-123455"; mongo::BSONObjBuilder builder; builder.append( "well_name" , well_name); builder.append( "wellborn_name" , "test_022" ); builder.append( "num" , 30 ); builder.append("file", fileObj); c.insert( "test.well001" , builder.obj() ); //读取井名 auto_ptr<DBClientCursor> cursor = c.query("test.well001", BSONObj()); while( cursor->more() ) { //utf8-unicode char szU8[1024] = {0}; strcpy_s(szU8, (cursor->next())["well_name"].str().c_str()); wchar_t wszString[2048]; UTF8ToUnicode(wszString, szU8); } return 0; }