MongoDB客户端工具类


import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.net.UnknownHostException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Map;


import net.sf.json.JSONArray;
import net.sf.json.JSONObject;


import org.apache.log4j.Logger;
import org.bson.types.ObjectId;


import com.ai.paas.util.CiperTools;
import com.mongodb.BasicDBObject;
import com.mongodb.DBObject;
import com.mongodb.MongoClient;
import com.mongodb.MongoCredential;
import com.mongodb.ServerAddress;
import com.mongodb.gridfs.GridFS;
import com.mongodb.gridfs.GridFSDBFile;
import com.mongodb.gridfs.GridFSInputFile;
import com.mongodb.util.JSON;


public class MongoDBClient {
private static final Logger log = Logger.getLogger(MongoDBClient.class);


private static final String IP_KEY = "ip";
private static final String PORT_KEY = "port";
private static final String FILE_NAME = "filename";


private MongoClient mongo = null;


public MongoDBClient(String addr) {
try {
JSONArray array = JSONArray.fromObject(addr);
if (array != null && array.size() > 0) {
int size = array.size();
JSONObject json = null;
ArrayList<ServerAddress> sa = new ArrayList<ServerAddress>();
for (int i = 0; i < size; i++) {
json = (JSONObject) array.get(i);
sa.add(new ServerAddress(json.getString(IP_KEY), json
.getInt(PORT_KEY)));
}
mongo = new MongoClient(sa);


}
} catch (UnknownHostException e) {
log.error("", e);
}
}


public MongoDBClient(String addr, String database, String userName,
String password) {
try {
JSONArray array = JSONArray.fromObject(addr);
if (array != null && array.size() > 0) {
int size = array.size();
JSONObject json = null;
ArrayList<ServerAddress> sa = new ArrayList<ServerAddress>();
for (int i = 0; i < size; i++) {
json = (JSONObject) array.get(i);
sa.add(new ServerAddress(json.getString(IP_KEY), json
.getInt(PORT_KEY)));
}
String orignPwd = CiperTools.decrypt(password);
MongoCredential credential = MongoCredential
.createMongoCRCredential(userName, database,
orignPwd.toCharArray());
mongo = new MongoClient(sa, Arrays.asList(credential));


}
} catch (UnknownHostException e) {
log.error("", e);
}
}


public void insert(String dbName, String collectionName, String content) {
BasicDBObject doc = new BasicDBObject();
doc.put("content", content);
mongo.getDB(dbName).getCollection(collectionName).insert(doc);
}


public void insert(String dbName, String collectionName, JSONObject doc) {
DBObject dbObj = (DBObject) JSON.parse(doc.toString());
mongo.getDB(dbName).getCollection(collectionName).insert(dbObj);
}


public void insertJSON(String dbName, String collectionName, String doc) {
DBObject dbObj = (DBObject) JSON.parse(doc);
mongo.getDB(dbName).getCollection(collectionName).insert(dbObj);
}


public void insertJSON(String dbName, String collectionName, JSONObject doc) {
DBObject dbObj = (DBObject) JSON.parse(doc.toString());
mongo.getDB(dbName).getCollection(collectionName).insert(dbObj);
}


@SuppressWarnings("rawtypes")
public void insert(String dbName, String collectionName, Map docMap) {
DBObject dbObj = new BasicDBObject(docMap);
mongo.getDB(dbName).getCollection(collectionName).insert(dbObj);
}


public String saveFile(String dbName, byte[] byteFile, String fileName,
String fileType) {
GridFS fs = new GridFS(mongo.getDB(dbName));
GridFSInputFile dbFile = null;
try {
dbFile = fs.createFile(byteFile);
} catch (Exception e) {
e.printStackTrace();
}
dbFile.setContentType(fileType);
dbFile.setFilename(fileName);
dbFile.put("fileName", fileName);
dbFile.save();
return dbFile.getId().toString();
}


public String saveFile(String dbName, String fileName, String fileType) {
if (fileName == null) {
return null;
}
String name = fileName.substring(fileName.lastIndexOf("/") + 1);
GridFS fs = new GridFS(mongo.getDB(dbName));
File file = new File(fileName);
GridFSInputFile dbFile = null;
try {
dbFile = fs.createFile(file);
} catch (IOException e) {
e.printStackTrace();
}
dbFile.setContentType(fileType);
dbFile.setFilename(name);
dbFile.save();
return dbFile.getId().toString();
}


public void deleteFile(String dbName, String fileId) {
if (fileId == null) {
return;
}
GridFS fs = new GridFS(mongo.getDB(dbName));
GridFSDBFile dbFile = fs.findOne(new ObjectId(fileId));
if (dbFile == null) {
return;
}
fs.remove(dbFile);
}


public void deleteFileByName(String dbName, String fileName) {
if (fileName == null) {
return;
}
GridFS fs = new GridFS(mongo.getDB(dbName));
GridFSDBFile dbFile = fs
.findOne(new BasicDBObject(FILE_NAME, fileName));
if (dbFile == null) {
return;
}
fs.remove(dbFile);
}


public byte[] readFile(String dbName, String fileId) {
if (fileId == null) {
return null;
}
GridFS fs = new GridFS(mongo.getDB(dbName));
GridFSDBFile dbFile = fs.findOne(new ObjectId(fileId));
if (dbFile == null) {
return null;
}
InputStream is = null;
try {
int len = (int) dbFile.getLength();
byte[] ret = new byte[len];
is = dbFile.getInputStream();
int tmp = 0;
int i = 0;
while ((tmp = is.read()) != -1 && i < len) {
ret[i] = (byte) tmp;
i++;
}
return ret;
} catch (IOException e) {
e.printStackTrace();
return null;
} finally {
if (is != null) {
try {
is.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}


}


public byte[] readFileByName(String dbName, String fileName) {
if (fileName == null) {
return null;
}
GridFS fs = new GridFS(mongo.getDB(dbName));
GridFSDBFile dbFile = fs
.findOne(new BasicDBObject(FILE_NAME, fileName));
if (dbFile == null) {
return null;
}
InputStream is = null;
try {
int len = (int) dbFile.getLength();
byte[] ret = new byte[len];
is = dbFile.getInputStream();
int tmp = 0;
int i = 0;
while ((tmp = is.read()) != -1 && i < len) {
ret[i] = (byte) tmp;
i++;
}
return ret;
} catch (IOException e) {
e.printStackTrace();
return null;
} finally {
if (is != null) {
try {
is.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}


}


public void readFile(String dbName, String fileId, String localFileName) {
if (fileId == null) {
return;
}
GridFS fs = new GridFS(mongo.getDB(dbName));
GridFSDBFile dbFile = fs.findOne(new ObjectId(fileId));
if (dbFile == null) {
return;
}
File file = new File(localFileName);
try {
dbFile.writeTo(file);
} catch (IOException e) {
e.printStackTrace();
}


}


public void readFileByName(String dbName, String fileName,
String localFileName) {
if (fileName == null) {
return;
}
GridFS fs = new GridFS(mongo.getDB(dbName));
GridFSDBFile dbFile = fs
.findOne(new BasicDBObject(FILE_NAME, fileName));
if (dbFile == null) {
return;
}
File file = new File(localFileName);
try {
dbFile.writeTo(file);
} catch (IOException e) {
log.error("", e);
}


}


public void destroyMongoDB() {
if (null != mongo) {
mongo.close();
mongo = null;
}
}
public String getFileName(String dbName,String fileId) {
if (fileId == null) {
return null;
}
GridFS fs = new GridFS(mongo.getDB(dbName));
GridFSDBFile dbFile = fs.findOne(new ObjectId(fileId));
if (dbFile == null) {
return null;
}
return dbFile.getFilename();
}
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值