1. 程式人生 > >java mongodb 檔案上傳,下載,刪除方法

java mongodb 檔案上傳,下載,刪除方法

package com.stylefeng.guns.config.mongo;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import org.apache.log4j.Logger;
import org.bson.types.ObjectId;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.stereotype.Service;
import com.mongodb.DB;
import com.mongodb.gridfs.GridFS;
import com.mongodb.gridfs.GridFSDBFile;
import com.mongodb.gridfs.GridFSInputFile;
/**
 * 
 * 類描述:mongodb檔案處理方法類
 * @author lh
 * 2018年1月25日 下午2:59:08
 */

@Service
public class MongoFileService {
private static Logger logger = Logger.getLogger(MongoFileService.class);
@Autowired
private MongoTemplate mongoTemplate;
/**
* 上傳
* @param file 
* @param fileName 檔名
* @param type 表名
* @return mongoid
*/
public String saveFile(File file, String fileName, MongoDbTypeEnum type) {
String id = "";
FileInputStream fileInputStream = null;
try {
GridFS fs = getGridFS(type);
GridFSInputFile mongofile = null;
fileInputStream = new FileInputStream(file);
mongofile = fs.createFile(fileInputStream, fileName);
mongofile.setFilename(fileName);
mongofile.put("lastModified", file.lastModified());
mongofile.save();
id = this.generalDbKey(mongofile.getId().toString(), type);
} catch (Exception e) {
logger.error("saveFile  error=======>" + e);
} finally {
if (!(null == fileInputStream)) {
try {
fileInputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return id;
}

/**
*  下載檔案到指定路徑
* @param type 表名
* @param id 主鍵
* @param Path 下載路徑
* @return
*/
public int downloadFile(MongoDbTypeEnum type, String id, File Path) {
//result=1代表成功,0失敗
int result=1;
try {
// 獲取fs的根節點
GridFS fs = getGridFS(type);
// 解析id
String[] key = parseDbKey(id);
// 取objectId
ObjectId objectId = new ObjectId(key[0]);
// 取表名
// String CollectionsName=key[1];
// 通過id查詢
GridFSDBFile file = fs.find(objectId);
// 通過檔名稱查詢
// GridFSDBFile file = fs.findOne("lvxing");
// 輸出檔案
file.writeTo(Path + File.separator + file.getFilename());
} catch (Exception e) {
result=0;
logger.error("downloadFile  error=======>" + e);
}
return result;
}
// 刪除檔案
public int deleteFile(MongoDbTypeEnum type, String id) {
//result=1代表成功,0失敗
int result=1;
try {
// 獲取fs的根節點
GridFS fs = getGridFS(type);
// 解析id
String[] key = parseDbKey(id);
// 取objectId
ObjectId objectId = new ObjectId(key[0]);
// 取表名
// String CollectionsName=key[1];
// 刪除檔案
fs.remove(objectId);
} catch (Exception e) {
result=0;
logger.error("deleteFile  error=======>" + e);
}
return result;
}
/**
* 獲取資料庫.

* @param type
* @return
*/
private GridFS getGridFS(MongoDbTypeEnum type) {
DB db = mongoTemplate.getDb();
String dbName = db.getName();
GridFS gridFS = new GridFS(db, type.toString());
return gridFS;
}

/**
* 生成標記資料庫的dbkey.

* @param objId
* @param type
* @return
*/
private String generalDbKey(String objId, MongoDbTypeEnum type) {
return objId + "-" + type;
}

/**
* 解析dbkey.

* @param dbKey
* @return
*/
private String[] parseDbKey(String dbKey) {
return dbKey.split("-");
}

}

/**
 * 
 * 類描述:MongoDb資料庫分類列舉.
 * @author lh
 * 2018年1月19日 下午3:11:24
 */
public enum MongoDbTypeEnum {
//測試用的表
test

}

如果你是湖南的 歡迎加入 湖南人在深圳-Java群:557651502