|
@ -30,7 +30,8 @@ import java.sql.Blob;
|
|
import java.util.*;
|
|
import java.util.*;
|
|
|
|
|
|
/**
|
|
/**
|
|
* MongoDB GridFS 操作类
|
|
|
|
|
|
* MongoDB GridFS 操作类
|
|
|
|
*
|
|
* @author HZY
|
|
* @author HZY
|
|
* @vsrsion 1.0
|
|
* @vsrsion 1.0
|
|
* Created at 2016/7/21.
|
|
* Created at 2016/7/21.
|
|
@ -40,19 +41,19 @@ public class GridFSUtil {
|
|
public static final int defaultChunkSize = 1024 * 1024 * 4;
|
|
public static final int defaultChunkSize = 1024 * 1024 * 4;
|
|
|
|
|
|
|
|
|
|
public static ObjectId uploadFile(String dbName, Blob blob,String fileType,Map<String,Object> params) {
|
|
|
|
|
|
public static ObjectId uploadFile(String dbName, Blob blob, String fileType, Map<String, Object> params) {
|
|
//获取mongodb连接
|
|
//获取mongodb连接
|
|
// MongodbHelper mongoOrigin = new MongodbHelper(dbName);
|
|
// MongodbHelper mongoOrigin = new MongodbHelper(dbName);
|
|
//创建一个容器
|
|
//创建一个容器
|
|
MongoDatabase db = MongodbFactory.getDB(dbName);
|
|
MongoDatabase db = MongodbFactory.getDB(dbName);
|
|
GridFSBucket gridFS = GridFSBuckets.create(db);
|
|
GridFSBucket gridFS = GridFSBuckets.create(db);
|
|
String fileName=UUID.randomUUID().toString()+"."+fileType;
|
|
|
|
|
|
String fileName = UUID.randomUUID().toString() + "." + fileType;
|
|
//自定义字段
|
|
//自定义字段
|
|
Document metaDocument= new Document();
|
|
|
|
if ( params!=null && params.size()>0){
|
|
|
|
|
|
Document metaDocument = new Document();
|
|
|
|
if (params != null && params.size() > 0) {
|
|
for (Map.Entry<String, Object> entry : params.entrySet()) {
|
|
for (Map.Entry<String, Object> entry : params.entrySet()) {
|
|
String key=entry.getKey();
|
|
|
|
metaDocument.append(key,entry.getValue());
|
|
|
|
|
|
String key = entry.getKey();
|
|
|
|
metaDocument.append(key, entry.getValue());
|
|
}
|
|
}
|
|
}
|
|
}
|
|
// Create some custom options
|
|
// Create some custom options
|
|
@ -78,13 +79,13 @@ public class GridFSUtil {
|
|
}
|
|
}
|
|
|
|
|
|
/**
|
|
/**
|
|
* 上传文件至Mongodb by GridFS
|
|
|
|
|
|
* 上传文件至Mongodb by GridFS
|
|
*
|
|
*
|
|
* @param dbName 数据库名
|
|
|
|
* @param filePath 文件路径
|
|
|
|
* @param params 自定义保存字段
|
|
|
|
|
|
* @param dbName 数据库名
|
|
|
|
* @param filePath 文件路径
|
|
|
|
* @param params 自定义保存字段
|
|
*/
|
|
*/
|
|
public static boolean uploadFile(String dbName, String filePath,Map<String,Object> params) {
|
|
|
|
|
|
public static boolean uploadFile(String dbName, String filePath, Map<String, Object> params) {
|
|
//获取mongodb连接
|
|
//获取mongodb连接
|
|
// MongodbHelper mongoOrigin = new MongodbHelper(dbName);
|
|
// MongodbHelper mongoOrigin = new MongodbHelper(dbName);
|
|
//创建一个容器
|
|
//创建一个容器
|
|
@ -93,11 +94,11 @@ public class GridFSUtil {
|
|
File readFile = new File(filePath);
|
|
File readFile = new File(filePath);
|
|
|
|
|
|
//自定义字段
|
|
//自定义字段
|
|
Document metaDocument= new Document();
|
|
|
|
if ( params!=null && params.size()>0){
|
|
|
|
|
|
Document metaDocument = new Document();
|
|
|
|
if (params != null && params.size() > 0) {
|
|
for (Map.Entry<String, Object> entry : params.entrySet()) {
|
|
for (Map.Entry<String, Object> entry : params.entrySet()) {
|
|
String key=entry.getKey();
|
|
|
|
metaDocument.append(key,entry.getValue());
|
|
|
|
|
|
String key = entry.getKey();
|
|
|
|
metaDocument.append(key, entry.getValue());
|
|
}
|
|
}
|
|
}
|
|
}
|
|
// Create some custom options
|
|
// Create some custom options
|
|
@ -124,9 +125,10 @@ public class GridFSUtil {
|
|
|
|
|
|
/**
|
|
/**
|
|
* 从 mongodb GridFS 下载文件
|
|
* 从 mongodb GridFS 下载文件
|
|
* @param dbName 数据库名
|
|
|
|
|
|
*
|
|
|
|
* @param dbName 数据库名
|
|
* @param savePath 文件保存路径
|
|
* @param savePath 文件保存路径
|
|
* @param objId GridFS文件保存ObjectId
|
|
|
|
|
|
* @param objId GridFS文件保存ObjectId
|
|
* @return
|
|
* @return
|
|
*/
|
|
*/
|
|
public static String downFile(String dbName, String savePath, ObjectId objId) {
|
|
public static String downFile(String dbName, String savePath, ObjectId objId) {
|
|
@ -159,32 +161,33 @@ public class GridFSUtil {
|
|
}
|
|
}
|
|
|
|
|
|
/**
|
|
/**
|
|
* 批量下载文件保存(根据 fs.files集合)
|
|
|
|
* @param dbName 数据库名
|
|
|
|
* @param savePath 文件保存的路径
|
|
|
|
* @param fsFiles fs.files
|
|
|
|
|
|
* 批量下载文件保存(根据 fs.files集合)
|
|
|
|
*
|
|
|
|
* @param dbName 数据库名
|
|
|
|
* @param savePath 文件保存的路径
|
|
|
|
* @param fsFiles fs.files
|
|
* @return 以“,”分割的文件名
|
|
* @return 以“,”分割的文件名
|
|
*/
|
|
*/
|
|
public static Map<String,StringBuffer> downFileList(String dbName, String savePath, List<GridFSFile> fsFiles) {
|
|
|
|
|
|
public static Map<String, StringBuffer> downFileList(String dbName, String savePath, List<GridFSFile> fsFiles) {
|
|
StringBuffer stringBuffer = new StringBuffer();
|
|
StringBuffer stringBuffer = new StringBuffer();
|
|
Map<String,String> fileNames= new HashMap<>();
|
|
|
|
|
|
Map<String, String> fileNames = new HashMap<>();
|
|
//穿件mongodb连接
|
|
//穿件mongodb连接
|
|
// MongodbHelper mongoOrigin = new MongodbHelper(dbName);
|
|
// MongodbHelper mongoOrigin = new MongodbHelper(dbName);
|
|
//创建一个容器
|
|
//创建一个容器
|
|
MongoDatabase db = MongodbFactory.getDB(dbName);
|
|
MongoDatabase db = MongodbFactory.getDB(dbName);
|
|
GridFSBucket gridFS = GridFSBuckets.create(db);
|
|
GridFSBucket gridFS = GridFSBuckets.create(db);
|
|
ByteArrayOutputStream out = new ByteArrayOutputStream();
|
|
ByteArrayOutputStream out = new ByteArrayOutputStream();
|
|
if (fsFiles!=null && fsFiles.size()>0){
|
|
|
|
for (GridFSFile fsFile:fsFiles){
|
|
|
|
|
|
if (fsFiles != null && fsFiles.size() > 0) {
|
|
|
|
for (GridFSFile fsFile : fsFiles) {
|
|
ObjectId objId = fsFile.getObjectId();
|
|
ObjectId objId = fsFile.getObjectId();
|
|
String fileType= fsFile.getFilename().substring(fsFile.getFilename().lastIndexOf("."));
|
|
|
|
String newName = UUID.randomUUID().toString()+"."+fileType;
|
|
|
|
|
|
String fileType = fsFile.getFilename().substring(fsFile.getFilename().lastIndexOf("."));
|
|
|
|
String newName = UUID.randomUUID().toString() + "." + fileType;
|
|
gridFS.downloadToStream(objId, out);
|
|
gridFS.downloadToStream(objId, out);
|
|
try {
|
|
try {
|
|
boolean succ = FileUtil.writeFile(savePath+"/"+newName, out.toByteArray(), "utf-8");
|
|
|
|
|
|
boolean succ = FileUtil.writeFile(savePath + "/" + newName, out.toByteArray(), "utf-8");
|
|
if (succ) {
|
|
if (succ) {
|
|
String type = getMimeType( out.toByteArray());
|
|
|
|
fileNames.put(newName,type);
|
|
|
|
|
|
String type = getMimeType(out.toByteArray());
|
|
|
|
fileNames.put(newName, type);
|
|
// stringBuffer.append(newName).append(",");
|
|
// stringBuffer.append(newName).append(",");
|
|
continue;
|
|
continue;
|
|
} else {
|
|
} else {
|
|
@ -204,14 +207,14 @@ public class GridFSUtil {
|
|
}
|
|
}
|
|
}
|
|
}
|
|
return groupDataMap(fileNames);
|
|
return groupDataMap(fileNames);
|
|
}else {
|
|
|
|
|
|
} else {
|
|
return null;
|
|
return null;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
public static Map<String,StringBuffer> downFilesByObjectIds(String dbName, String savePath, List<ObjectId> ids) {
|
|
|
|
|
|
public static Map<String, StringBuffer> downFilesByObjectIds(String dbName, String savePath, List<ObjectId> ids) {
|
|
StringBuffer stringBuffer = new StringBuffer();
|
|
StringBuffer stringBuffer = new StringBuffer();
|
|
Map<String,String> fileNames= new HashMap<>();
|
|
|
|
|
|
Map<String, String> fileNames = new HashMap<>();
|
|
//穿件mongodb连接
|
|
//穿件mongodb连接
|
|
// MongodbHelper mongoOrigin = new MongodbHelper(dbName);
|
|
// MongodbHelper mongoOrigin = new MongodbHelper(dbName);
|
|
//创建一个容器
|
|
//创建一个容器
|
|
@ -219,17 +222,17 @@ public class GridFSUtil {
|
|
GridFSBucket gridFS = GridFSBuckets.create(db);
|
|
GridFSBucket gridFS = GridFSBuckets.create(db);
|
|
ByteArrayOutputStream out = new ByteArrayOutputStream();
|
|
ByteArrayOutputStream out = new ByteArrayOutputStream();
|
|
List<GridFSFile> fsFiles = findFsFiles(dbName, ids);
|
|
List<GridFSFile> fsFiles = findFsFiles(dbName, ids);
|
|
if (fsFiles!=null && fsFiles.size()>0){
|
|
|
|
for (GridFSFile fsFile:fsFiles){
|
|
|
|
|
|
if (fsFiles != null && fsFiles.size() > 0) {
|
|
|
|
for (GridFSFile fsFile : fsFiles) {
|
|
ObjectId objId = fsFile.getObjectId();
|
|
ObjectId objId = fsFile.getObjectId();
|
|
String fileType= fsFile.getFilename().substring(fsFile.getFilename().lastIndexOf("."));
|
|
|
|
String newName = UUID.randomUUID().toString()+"."+fileType;
|
|
|
|
|
|
String fileType = fsFile.getFilename().substring(fsFile.getFilename().lastIndexOf("."));
|
|
|
|
String newName = UUID.randomUUID().toString() + "." + fileType;
|
|
gridFS.downloadToStream(objId, out);
|
|
gridFS.downloadToStream(objId, out);
|
|
try {
|
|
try {
|
|
boolean succ = FileUtil.writeFile(savePath+"/"+newName, out.toByteArray(), "utf-8");
|
|
|
|
|
|
boolean succ = FileUtil.writeFile(savePath + "/" + newName, out.toByteArray(), "utf-8");
|
|
if (succ) {
|
|
if (succ) {
|
|
String type = getMimeType( out.toByteArray());
|
|
|
|
fileNames.put(newName,type);
|
|
|
|
|
|
String type = getMimeType(out.toByteArray());
|
|
|
|
fileNames.put(newName, type);
|
|
// stringBuffer.append(newName).append(",");
|
|
// stringBuffer.append(newName).append(",");
|
|
continue;
|
|
continue;
|
|
} else {
|
|
} else {
|
|
@ -249,13 +252,14 @@ public class GridFSUtil {
|
|
}
|
|
}
|
|
}
|
|
}
|
|
return groupDataMap(fileNames);
|
|
return groupDataMap(fileNames);
|
|
}else {
|
|
|
|
|
|
} else {
|
|
return null;
|
|
return null;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
/**
|
|
/**
|
|
* 删除 mongodb-GridFS文件
|
|
|
|
|
|
* 删除 mongodb-GridFS文件
|
|
|
|
*
|
|
* @param dbName
|
|
* @param dbName
|
|
* @param objId
|
|
* @param objId
|
|
*/
|
|
*/
|
|
@ -268,62 +272,64 @@ public class GridFSUtil {
|
|
|
|
|
|
/**
|
|
/**
|
|
* 查询fs.files 数据 in GridFS
|
|
* 查询fs.files 数据 in GridFS
|
|
* @param dbName 数据库名
|
|
|
|
|
|
*
|
|
|
|
* @param dbName 数据库名
|
|
* @param filters 查询条件
|
|
* @param filters 查询条件
|
|
* @return files集合
|
|
* @return files集合
|
|
*/
|
|
*/
|
|
public static List<GridFSFile> findFiles(String dbName,Map<String,Object> filters){
|
|
|
|
|
|
public static List<GridFSFile> findFiles(String dbName, Map<String, Object> filters) {
|
|
//穿件mongodb连接
|
|
//穿件mongodb连接
|
|
List<GridFSFile> list=new ArrayList<>();
|
|
|
|
|
|
List<GridFSFile> list = new ArrayList<>();
|
|
//创建一个容器
|
|
//创建一个容器
|
|
MongoDatabase db = MongodbFactory.getDB(dbName);
|
|
MongoDatabase db = MongodbFactory.getDB(dbName);
|
|
GridFSBucket gridFSBucket = GridFSBuckets.create(db);
|
|
GridFSBucket gridFSBucket = GridFSBuckets.create(db);
|
|
List<Bson> querys=new ArrayList<>();
|
|
|
|
|
|
List<Bson> querys = new ArrayList<>();
|
|
//添加查询条件
|
|
//添加查询条件
|
|
if (filters!=null && filters.size()>0){
|
|
|
|
|
|
if (filters != null && filters.size() > 0) {
|
|
for (Map.Entry<String, Object> entry : filters.entrySet()) {
|
|
for (Map.Entry<String, Object> entry : filters.entrySet()) {
|
|
String key=entry.getKey();
|
|
|
|
|
|
String key = entry.getKey();
|
|
querys.add(Filters.eq(key, entry.getValue()));
|
|
querys.add(Filters.eq(key, entry.getValue()));
|
|
}
|
|
}
|
|
}
|
|
}
|
|
// GridFSFindIterable gsIterable=gridFSBucket.find(Filters.eq("metadata.cda_id", "111"));
|
|
// GridFSFindIterable gsIterable=gridFSBucket.find(Filters.eq("metadata.cda_id", "111"));
|
|
|
|
|
|
GridFSFindIterable gsIterable=gridFSBucket.find(Filters.and(querys));
|
|
|
|
MongoCursor<GridFSFile> it= gsIterable.iterator();
|
|
|
|
while (it.hasNext()){
|
|
|
|
GridFSFile fsFile= it.next();
|
|
|
|
|
|
GridFSFindIterable gsIterable = gridFSBucket.find(Filters.and(querys));
|
|
|
|
MongoCursor<GridFSFile> it = gsIterable.iterator();
|
|
|
|
while (it.hasNext()) {
|
|
|
|
GridFSFile fsFile = it.next();
|
|
list.add(fsFile);
|
|
list.add(fsFile);
|
|
}
|
|
}
|
|
return list;
|
|
return list;
|
|
}
|
|
}
|
|
|
|
|
|
/**
|
|
/**
|
|
* 根据ObjectID集合查询GridFS 文件列表
|
|
|
|
|
|
* 根据ObjectID集合查询GridFS 文件列表
|
|
|
|
*
|
|
* @param dbName 数据库名
|
|
* @param dbName 数据库名
|
|
* @param ids objectId集合
|
|
|
|
|
|
* @param ids objectId集合
|
|
* @return
|
|
* @return
|
|
*/
|
|
*/
|
|
public static List<GridFSFile> findFsFiles(String dbName,List<ObjectId> ids){
|
|
|
|
|
|
public static List<GridFSFile> findFsFiles(String dbName, List<ObjectId> ids) {
|
|
//穿件mongodb连接
|
|
//穿件mongodb连接
|
|
List<GridFSFile> list=new ArrayList<>();
|
|
|
|
|
|
List<GridFSFile> list = new ArrayList<>();
|
|
//创建一个容器
|
|
//创建一个容器
|
|
MongoDatabase db = MongodbFactory.getDB(dbName);
|
|
MongoDatabase db = MongodbFactory.getDB(dbName);
|
|
GridFSBucket gridFSBucket = GridFSBuckets.create(db);
|
|
GridFSBucket gridFSBucket = GridFSBuckets.create(db);
|
|
List<Bson> querys=new ArrayList<>();
|
|
|
|
|
|
List<Bson> querys = new ArrayList<>();
|
|
//添加查询条件
|
|
//添加查询条件
|
|
if (ids!=null && ids.size()>0){
|
|
|
|
|
|
if (ids != null && ids.size() > 0) {
|
|
querys.add(Filters.in("_id", ids));
|
|
querys.add(Filters.in("_id", ids));
|
|
}
|
|
}
|
|
GridFSFindIterable gsIterable=null;
|
|
|
|
if (querys.size()>0){
|
|
|
|
gsIterable=gridFSBucket.find(Filters.and(querys));
|
|
|
|
}else {
|
|
|
|
gsIterable=gridFSBucket.find();
|
|
|
|
|
|
GridFSFindIterable gsIterable = null;
|
|
|
|
if (querys.size() > 0) {
|
|
|
|
gsIterable = gridFSBucket.find(Filters.and(querys));
|
|
|
|
} else {
|
|
|
|
gsIterable = gridFSBucket.find();
|
|
}
|
|
}
|
|
|
|
|
|
MongoCursor<GridFSFile> it= gsIterable.iterator();
|
|
|
|
while (it.hasNext()){
|
|
|
|
GridFSFile fsFile= it.next();
|
|
|
|
|
|
MongoCursor<GridFSFile> it = gsIterable.iterator();
|
|
|
|
while (it.hasNext()) {
|
|
|
|
GridFSFile fsFile = it.next();
|
|
list.add(fsFile);
|
|
list.add(fsFile);
|
|
}
|
|
}
|
|
return list;
|
|
return list;
|
|
@ -332,22 +338,22 @@ public class GridFSUtil {
|
|
public static Map parseJsonToMap(JSONObject condition) {
|
|
public static Map parseJsonToMap(JSONObject condition) {
|
|
ObjectMapper objectMapper = new ObjectMapper();
|
|
ObjectMapper objectMapper = new ObjectMapper();
|
|
HashMap map = new HashMap();
|
|
HashMap map = new HashMap();
|
|
try {
|
|
|
|
Iterator fileNames = (Iterator) condition.names();
|
|
|
|
|
|
try {
|
|
|
|
Iterator fileNames = (Iterator) condition.names();
|
|
|
|
|
|
while(fileNames.hasNext()) {
|
|
|
|
String fieldName = (String)fileNames.next();
|
|
|
|
Object valueNode = condition.get(fieldName);
|
|
|
|
|
|
while (fileNames.hasNext()) {
|
|
|
|
String fieldName = (String) fileNames.next();
|
|
|
|
Object valueNode = condition.get(fieldName);
|
|
|
|
|
|
if(valueNode instanceof Blob) {
|
|
|
|
map.put(fieldName, valueNode);
|
|
|
|
} else {
|
|
|
|
map.put(fieldName, condition.get(fieldName).toString());
|
|
|
|
}
|
|
|
|
|
|
if (valueNode instanceof Blob) {
|
|
|
|
map.put(fieldName, valueNode);
|
|
|
|
} else {
|
|
|
|
map.put(fieldName, condition.get(fieldName).toString());
|
|
}
|
|
}
|
|
} catch (Exception var7) {
|
|
|
|
var7.printStackTrace();
|
|
|
|
}
|
|
}
|
|
|
|
} catch (Exception var7) {
|
|
|
|
var7.printStackTrace();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return map;
|
|
return map;
|
|
@ -355,30 +361,29 @@ public class GridFSUtil {
|
|
|
|
|
|
|
|
|
|
//TODO 测试所用,后删除
|
|
//TODO 测试所用,后删除
|
|
public static void tTestFind(String tableName, String collection, Map<String,Object> params){
|
|
|
|
|
|
public static void tTestFind(String tableName, String collection, Map<String, Object> params) {
|
|
MongodbHelper mongodbHelper = new MongodbHelper(tableName);
|
|
MongodbHelper mongodbHelper = new MongodbHelper(tableName);
|
|
BasicDBObject basicDBObject=new BasicDBObject();
|
|
|
|
|
|
BasicDBObject basicDBObject = new BasicDBObject();
|
|
for (Map.Entry<String, Object> entry : params.entrySet()) {
|
|
for (Map.Entry<String, Object> entry : params.entrySet()) {
|
|
String key=entry.getKey();
|
|
|
|
basicDBObject.append(key,entry.getValue());
|
|
|
|
|
|
String key = entry.getKey();
|
|
|
|
basicDBObject.append(key, entry.getValue());
|
|
}
|
|
}
|
|
FindIterable<Document> documents=mongodbHelper.query(collection,basicDBObject, null, null);
|
|
|
|
MongoCursor<Document> it= documents.iterator();
|
|
|
|
while (it.hasNext()){
|
|
|
|
Document document= it.next();
|
|
|
|
List<Document> fileList= (List<Document>) document.get("files");
|
|
|
|
Document jsonNode=fileList.get(0);
|
|
|
|
List files= (List) jsonNode.get("files_id");
|
|
|
|
|
|
|
|
List<GridFSFile> fs=findFsFiles("mydb",files);
|
|
|
|
|
|
FindIterable<Document> documents = mongodbHelper.query(collection, basicDBObject, null, null);
|
|
|
|
MongoCursor<Document> it = documents.iterator();
|
|
|
|
while (it.hasNext()) {
|
|
|
|
Document document = it.next();
|
|
|
|
List<Document> fileList = (List<Document>) document.get("files");
|
|
|
|
Document jsonNode = fileList.get(0);
|
|
|
|
List files = (List) jsonNode.get("files_id");
|
|
|
|
|
|
|
|
List<GridFSFile> fs = findFsFiles("mydb", files);
|
|
System.out.println(files.toString());
|
|
System.out.println(files.toString());
|
|
String filePath="e:/test/";
|
|
|
|
Map<String,StringBuffer> path=downFileList("mydb",filePath, fs);
|
|
|
|
|
|
String filePath = "e:/test/";
|
|
|
|
Map<String, StringBuffer> path = downFileList("mydb", filePath, fs);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// public static void main(String[] args) {
|
|
// public static void main(String[] args) {
|
|
// try {
|
|
// try {
|
|
// //上传
|
|
// //上传
|
|
@ -418,29 +423,31 @@ public class GridFSUtil {
|
|
/*************************************** MineType 工具类 *********************************/
|
|
/*************************************** MineType 工具类 *********************************/
|
|
/**
|
|
/**
|
|
* 获取文件Mine-Type
|
|
* 获取文件Mine-Type
|
|
|
|
*
|
|
* @param file
|
|
* @param file
|
|
* @return
|
|
* @return
|
|
*/
|
|
*/
|
|
public static String getMimeType(File file) {
|
|
public static String getMimeType(File file) {
|
|
MimeUtil.registerMimeDetector("eu.medsea.mimeutil.detector.MagicMimeMimeDetector");
|
|
MimeUtil.registerMimeDetector("eu.medsea.mimeutil.detector.MagicMimeMimeDetector");
|
|
Collection<?> collection=MimeUtil.getMimeTypes(file);
|
|
|
|
|
|
Collection<?> collection = MimeUtil.getMimeTypes(file);
|
|
return collection.toString();
|
|
return collection.toString();
|
|
}
|
|
}
|
|
|
|
|
|
public static String getMimeType(byte[] bytes) {
|
|
public static String getMimeType(byte[] bytes) {
|
|
MimeUtil.registerMimeDetector("eu.medsea.mimeutil.detector.MagicMimeMimeDetector");
|
|
MimeUtil.registerMimeDetector("eu.medsea.mimeutil.detector.MagicMimeMimeDetector");
|
|
Collection<?> collection=MimeUtil.getMimeTypes(bytes);
|
|
|
|
|
|
Collection<?> collection = MimeUtil.getMimeTypes(bytes);
|
|
return collection.toString();
|
|
return collection.toString();
|
|
}
|
|
}
|
|
|
|
|
|
/**
|
|
/**
|
|
* 非结构化档案--文件类型map生成
|
|
* 非结构化档案--文件类型map生成
|
|
|
|
*
|
|
* @param map
|
|
* @param map
|
|
* @return
|
|
* @return
|
|
*/
|
|
*/
|
|
public static Map<String, StringBuffer> groupDataMap(Map<String, String> map) {
|
|
public static Map<String, StringBuffer> groupDataMap(Map<String, String> map) {
|
|
Map<String, StringBuffer> result = new HashMap<String, StringBuffer>();
|
|
Map<String, StringBuffer> result = new HashMap<String, StringBuffer>();
|
|
Iterator<String> rs=map.keySet().iterator();
|
|
|
|
|
|
Iterator<String> rs = map.keySet().iterator();
|
|
while (rs.hasNext()) {
|
|
while (rs.hasNext()) {
|
|
String key = rs.next();
|
|
String key = rs.next();
|
|
String value = map.get(key);
|
|
String value = map.get(key);
|
|
@ -454,16 +461,17 @@ public class GridFSUtil {
|
|
}
|
|
}
|
|
|
|
|
|
/**
|
|
/**
|
|
* 上传文件至Mongodb by GridFS
|
|
|
|
* @param db 数据库
|
|
|
|
* @param file 文件
|
|
|
|
|
|
* 上传文件至Mongodb by GridFS
|
|
|
|
*
|
|
|
|
* @param db 数据库
|
|
|
|
* @param file 文件
|
|
*/
|
|
*/
|
|
public static String uploadFile(MongoDatabase db, CommonsMultipartFile file) {
|
|
public static String uploadFile(MongoDatabase db, CommonsMultipartFile file) {
|
|
//创建一个容器
|
|
//创建一个容器
|
|
GridFSBucket gridFS = GridFSBuckets.create(db);
|
|
GridFSBucket gridFS = GridFSBuckets.create(db);
|
|
|
|
|
|
//自定义字段
|
|
//自定义字段
|
|
Document metaDocument= new Document();
|
|
|
|
|
|
Document metaDocument = new Document();
|
|
// Create some custom options
|
|
// Create some custom options
|
|
GridFSUploadOptions gridFSUploadOptions = new GridFSUploadOptions()
|
|
GridFSUploadOptions gridFSUploadOptions = new GridFSUploadOptions()
|
|
.chunkSizeBytes(defaultChunkSize).metadata(metaDocument);
|
|
.chunkSizeBytes(defaultChunkSize).metadata(metaDocument);
|
|
@ -485,12 +493,12 @@ public class GridFSUtil {
|
|
}
|
|
}
|
|
|
|
|
|
/**
|
|
/**
|
|
* 上传文件至Mongodb by GridFS
|
|
|
|
|
|
* 上传文件至Mongodb by GridFS
|
|
*
|
|
*
|
|
* @param dbName 数据库名
|
|
|
|
* @param filePath 文件路径
|
|
|
|
|
|
* @param dbName 数据库名
|
|
|
|
* @param filePath 文件路径
|
|
*/
|
|
*/
|
|
public static String uploadFile(String dbName, String filePath,String fileName) {
|
|
|
|
|
|
public static String uploadFile(String dbName, String filePath, String fileName) {
|
|
//获取mongodb连接
|
|
//获取mongodb连接
|
|
// MongodbHelper mongoOrigin = new MongodbHelper(dbName);
|
|
// MongodbHelper mongoOrigin = new MongodbHelper(dbName);
|
|
//创建一个容器
|
|
//创建一个容器
|
|
@ -498,7 +506,7 @@ public class GridFSUtil {
|
|
MongoDatabase db = MongodbFactory.getDB(dbName);
|
|
MongoDatabase db = MongodbFactory.getDB(dbName);
|
|
GridFSBucket gridFS = GridFSBuckets.create(db);
|
|
GridFSBucket gridFS = GridFSBuckets.create(db);
|
|
//自定义字段
|
|
//自定义字段
|
|
Document metaDocument= new Document();
|
|
|
|
|
|
Document metaDocument = new Document();
|
|
// Create some custom options
|
|
// Create some custom options
|
|
GridFSUploadOptions gridFSUploadOptions = new GridFSUploadOptions()
|
|
GridFSUploadOptions gridFSUploadOptions = new GridFSUploadOptions()
|
|
.chunkSizeBytes(defaultChunkSize).metadata(metaDocument);
|
|
.chunkSizeBytes(defaultChunkSize).metadata(metaDocument);
|
|
@ -510,10 +518,9 @@ public class GridFSUtil {
|
|
if (id != null) {
|
|
if (id != null) {
|
|
return fileName;
|
|
return fileName;
|
|
}
|
|
}
|
|
}catch (Exception e){
|
|
|
|
|
|
} catch (Exception e) {
|
|
e.printStackTrace();
|
|
e.printStackTrace();
|
|
}
|
|
|
|
finally {
|
|
|
|
|
|
} finally {
|
|
if (uploadStream != null) {
|
|
if (uploadStream != null) {
|
|
uploadStream.close();
|
|
uploadStream.close();
|
|
}
|
|
}
|
|
@ -523,10 +530,11 @@ public class GridFSUtil {
|
|
|
|
|
|
/**
|
|
/**
|
|
* 据文件名返回文件,只返回第一个
|
|
* 据文件名返回文件,只返回第一个
|
|
|
|
*
|
|
* @param fileName
|
|
* @param fileName
|
|
* @return
|
|
* @return
|
|
*/
|
|
*/
|
|
public static Boolean readFile(MongoDatabase db, OutputStream os, String fileName){
|
|
|
|
|
|
public static boolean readFile(MongoDatabase db, OutputStream os, String fileName) {
|
|
try {
|
|
try {
|
|
GridFSBucket gridFS = GridFSBuckets.create(db);
|
|
GridFSBucket gridFS = GridFSBuckets.create(db);
|
|
gridFS.downloadToStreamByName(fileName, os);
|
|
gridFS.downloadToStreamByName(fileName, os);
|
|
@ -541,9 +549,10 @@ public class GridFSUtil {
|
|
|
|
|
|
/**
|
|
/**
|
|
* 从 mongodb GridFS 下载文件
|
|
* 从 mongodb GridFS 下载文件
|
|
* @param dbName 数据库名
|
|
|
|
|
|
*
|
|
|
|
* @param dbName 数据库名
|
|
* @param savePath 文件保存路径
|
|
* @param savePath 文件保存路径
|
|
* @param fileName GridFS文件名
|
|
|
|
|
|
* @param fileName GridFS文件名
|
|
* @return
|
|
* @return
|
|
*/
|
|
*/
|
|
public static String downFile(String dbName, String savePath, String fileName) {
|
|
public static String downFile(String dbName, String savePath, String fileName) {
|