MongoDB操作檔案
package com.kflow.security.ui.file;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.net.UnknownHostException;
import java.text.SimpleDateFormat;
import java.util.Date;
import org.bson.types.ObjectId;
import com.kflow.security.MongodbFileVo;
import com.kflow.util.WebConfig;
import com.mongodb.BasicDBObject;
import com.mongodb.DB;
import com.mongodb.DBCollection;
import com.mongodb.DBObject;
import com.mongodb.Mongo;
import com.mongodb.MongoException;
import com.mongodb.gridfs.GridFS;
import com.mongodb.gridfs.GridFSDBFile;
import com.mongodb.gridfs.GridFSInputFile;
public class MongodbUtil {
private static Mongo connection = null;
private static DB db = null;
private static DBCollection collection = null;
private static GridFS myFS = null;
private static String mongoDBHost = null;
private static Integer mongoDBPort = null;
private static String dbName = null;
private static String collectionName = null;
//test
public static void main(String[] args) throws MongoException, IOException {
String fileName = "F:/asddas.txt";
String name = "asddas.txt";
//把檔案儲存到gridfs中,並生成UUID 5626032608b0846929e36cd1
File file = new File(fileName);
String uuid = MongodbUtil.save(file, name);
System.out.println("生成的UUID: " + uuid);
//MongodbUtil.deleteFileById("5626032608b0846929e36cd1");
//據UUID從gridfs中讀取到檔案
//MongodbUtil.deleteFileById(uuid);
GridFSDBFile gridFSDBFile = MongodbUtil.getMongoDbById(uuid);
if (gridFSDBFile != null) {
System.out.println("filename:" + gridFSDBFile.getFilename());
System.out.println("md5:" + gridFSDBFile.getMD5());
System.out.println("length:" + gridFSDBFile.getLength());
System.out.println("uploadDate:" + gridFSDBFile.getUploadDate());
gridFSDBFile.getInputStream();
System.out.println("--------------------------------------");
gridFSDBFile.writeTo(System.out);
} else {
System.out.println("file not found ");
}
}
/**
* 獲取引數配置
*/
static {
mongoDBHost = WebConfig.getProperty("mongoDBHost");
mongoDBPort = Integer.parseInt(WebConfig.getProperty("mongoDBPort"));
dbName = WebConfig.getProperty("dbName");
collectionName = WebConfig.getProperty("collectionName");
try {
_init();
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 初始化引數配置
*/
private static void _init() throws UnknownHostException, MongoException {
connection = new Mongo(mongoDBHost, mongoDBPort);
db = connection.getDB(dbName);
collection = db.getCollection(collectionName);
myFS = new GridFS(db);
}
/**
* 用給出的id,儲存檔案透明處理已存在的情況
* id
*
* @param in
* @param id
*/
public static String save(File file, Object id) {
if (file != null) {
String uuid = null;
DBObject query = new BasicDBObject("_id", id);
GridFSDBFile gridFSDBFile = myFS.findOne(query);
try {
if (gridFSDBFile != null) {
return null;
} else {
GridFSInputFile gridFSInputFile = myFS.createFile(file);
gridFSInputFile.setFilename(id.toString());
gridFSInputFile.save();
uuid = gridFSInputFile.getId().toString();
}
} catch (IOException e) {
e.printStackTrace();
}
return uuid;
} else {
return null;
}
}
public static String save(byte[] fileByte, Object id) {
if (fileByte != null) {
String uuid = null;
DBObject query = new BasicDBObject("_id", id);
GridFSDBFile gridFSDBFile = myFS.findOne(query);
if (gridFSDBFile != null) {
return null;
} else {
GridFSInputFile gridFSInputFile = myFS.createFile(fileByte);
gridFSInputFile.setFilename(id.toString());
gridFSInputFile.save();
uuid = gridFSInputFile.getId().toString();
}
return uuid;
} else {
return null;
}
}
/**
* 據id返回檔案物件
*
* @param id
* @return
*/
public static MongodbFileVo getVoById(String id) {
MongodbFileVo vo = null;
if (id != null) {
GridFSDBFile gridFSDBFile = (GridFSDBFile) myFS.find(new ObjectId(id));
gridFSDBFile.getUploadDate();
gridFSDBFile.getFilename();
gridFSDBFile.getLength();
vo = new MongodbFileVo(
id,
gridFSDBFile.getFilename(),
formatDate(gridFSDBFile.getUploadDate()),
gridFSDBFile.getLength()
);
return vo;
} else {
return null;
}
}
/**
* 據id返回MongoDb檔案VO物件
*
* @param id
* @return
*/
public static GridFSDBFile getMongoDbById(String id) {
if (id != null) {
GridFSDBFile gridFSDBFile = (GridFSDBFile) myFS.find(new ObjectId(id));
return gridFSDBFile;
} else {
return null;
}
}
/**
* 據id刪除檔案
*
* @param id
* @return
*/
public static void deleteFileById(String id) {
myFS.remove(new ObjectId(id.toString()));
}
/**
* 據id下載檔案
*
* @param id
* @return
*/
public static MongodbFileVo getReadFile(String id) {
if (id != null) {
GridFSDBFile gridFSDBFile = MongodbUtil.getMongoDbById(id);
InputStream is = gridFSDBFile.getInputStream();
MongodbFileVo vo = new MongodbFileVo(gridFSDBFile.getFilename(), gridFSDBFile.getLength(), is);
return vo;
} else {
return null;
}
}
private static String formatDate(Date date) {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
return date == null ? null : sdf.format(date);
}
}