|
@ -3,8 +3,6 @@ package com.yihu.hos.web.framework.util;
|
|
|
import com.mongodb.client.MongoDatabase;
|
|
|
import com.mongodb.client.gridfs.GridFSBucket;
|
|
|
import com.mongodb.client.gridfs.GridFSBuckets;
|
|
|
import com.mongodb.client.gridfs.GridFSUploadStream;
|
|
|
import com.mongodb.client.gridfs.model.GridFSFile;
|
|
|
import com.mongodb.client.gridfs.model.GridFSUploadOptions;
|
|
|
import com.mongodb.gridfs.GridFSDBFile;
|
|
|
import com.yihu.hos.core.file.FileUtil;
|
|
@ -16,7 +14,6 @@ import org.springframework.data.mongodb.core.query.Query;
|
|
|
import org.springframework.data.mongodb.gridfs.GridFsCriteria;
|
|
|
import org.springframework.data.mongodb.gridfs.GridFsOperations;
|
|
|
import org.springframework.stereotype.Component;
|
|
|
import org.springframework.web.multipart.commons.CommonsMultipartFile;
|
|
|
|
|
|
import javax.annotation.PostConstruct;
|
|
|
import java.io.*;
|
|
@ -46,7 +43,7 @@ public class GridFSUtil {
|
|
|
gridFsOperations = this.operations;
|
|
|
}
|
|
|
|
|
|
public static ObjectId uploadFile(String dbName, Blob blob, String fileType, Map<String, Object> params) {
|
|
|
public static ObjectId uploadFile( Blob blob, String fileType, Map<String, Object> params) {
|
|
|
// String fileName = UUID.randomUUID().toString() + "." + fileType;
|
|
|
//自定义字段
|
|
|
Document metaDocument = new Document();
|
|
@ -74,11 +71,11 @@ public class GridFSUtil {
|
|
|
/**
|
|
|
* 上传文件至Mongodb by GridFS
|
|
|
*
|
|
|
* @param dbName 数据库名
|
|
|
* @param filePath 文件路径
|
|
|
* @param params 自定义保存字段
|
|
|
* @param filePath 上传的文件路径
|
|
|
* @param saveFileName 保存到mongo的文件名
|
|
|
* @param params 自定义保存字段
|
|
|
*/
|
|
|
public static boolean uploadFile(String dbName, String filePath, Map<String, Object> params) {
|
|
|
public static String uploadFile( String filePath,String saveFileName, Map<String, Object> params) {
|
|
|
//自定义字段
|
|
|
Document metaDocument = new Document();
|
|
|
if (params != null && params.size() > 0) {
|
|
@ -88,15 +85,13 @@ public class GridFSUtil {
|
|
|
}
|
|
|
}
|
|
|
// Create some custom options
|
|
|
GridFSUploadOptions gridFSUploadOptions = new GridFSUploadOptions()
|
|
|
.chunkSizeBytes(defaultChunkSize).metadata(metaDocument);
|
|
|
FileInputStream fileInputStream = null;
|
|
|
try {
|
|
|
File readFile = new File(filePath);
|
|
|
fileInputStream = new FileInputStream(readFile);
|
|
|
com.mongodb.gridfs.GridFSFile gridFSFile = gridFsOperations.store(fileInputStream, gridFSUploadOptions);
|
|
|
com.mongodb.gridfs.GridFSFile gridFSFile = gridFsOperations.store(fileInputStream,saveFileName,"",metaDocument);
|
|
|
if (gridFSFile != null) {
|
|
|
return true;
|
|
|
return saveFileName;
|
|
|
}
|
|
|
} catch (IOException e) {
|
|
|
e.printStackTrace();
|
|
@ -109,7 +104,7 @@ public class GridFSUtil {
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
return false;
|
|
|
return null;
|
|
|
}
|
|
|
|
|
|
/**
|
|
@ -151,13 +146,13 @@ public class GridFSUtil {
|
|
|
* @param fsFiles fs.files
|
|
|
* @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<GridFSDBFile> fsFiles) {
|
|
|
StringBuffer stringBuffer = new StringBuffer();
|
|
|
Map<String, String> fileNames = new HashMap<>();
|
|
|
ByteArrayOutputStream out = new ByteArrayOutputStream();
|
|
|
if (fsFiles != null && fsFiles.size() > 0) {
|
|
|
for (GridFSFile fsFile : fsFiles) {
|
|
|
ObjectId objectId = fsFile.getObjectId();
|
|
|
for (GridFSDBFile fsFile : fsFiles) {
|
|
|
Object objectId = fsFile.getId();
|
|
|
String fileType = fsFile.getFilename().substring(fsFile.getFilename().lastIndexOf("."));
|
|
|
String newName = UUID.randomUUID().toString() + "." + fileType;
|
|
|
GridFSDBFile gridFSDBFile = gridFsOperations.findOne(Query.query(GridFsCriteria.where("_id").is(objectId)));
|
|
@ -166,7 +161,6 @@ public class GridFSUtil {
|
|
|
boolean success = FileUtil.writeFile(savePath + "/" + newName, out.toByteArray(), "utf-8");
|
|
|
if (!success) {
|
|
|
break;
|
|
|
|
|
|
}
|
|
|
|
|
|
String type = getMimeType(out.toByteArray());
|
|
@ -190,45 +184,6 @@ public class GridFSUtil {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
public static Map<String, StringBuffer> downFilesByObjectIds(String dbName, String savePath, List<ObjectId> ids) {
|
|
|
StringBuffer stringBuffer = new StringBuffer();
|
|
|
Map<String, String> fileNames = new HashMap<>();
|
|
|
ByteArrayOutputStream out = new ByteArrayOutputStream();
|
|
|
List<GridFSDBFile> fsFiles = findFsFiles(dbName, ids);
|
|
|
if (fsFiles != null && fsFiles.size() > 0) {
|
|
|
for (GridFSDBFile fsFile : fsFiles) {
|
|
|
ObjectId objectId = (ObjectId) fsFile.getId();
|
|
|
String fileType = fsFile.getFilename().substring(fsFile.getFilename().lastIndexOf("."));
|
|
|
String newName = UUID.randomUUID().toString() + "." + fileType;
|
|
|
GridFSDBFile gridFSDBFile = gridFsOperations.findOne(Query.query(GridFsCriteria.where("_id").is(objectId)));
|
|
|
try {
|
|
|
gridFSDBFile.writeTo(out);
|
|
|
boolean success = FileUtil.writeFile(savePath + "/" + newName, out.toByteArray(), "utf-8");
|
|
|
if (!success) {
|
|
|
|
|
|
break;
|
|
|
}
|
|
|
String type = getMimeType(out.toByteArray());
|
|
|
fileNames.put(newName, type);
|
|
|
|
|
|
} catch (IOException e) {
|
|
|
e.printStackTrace();
|
|
|
return null;
|
|
|
} finally {
|
|
|
if (out != null) {
|
|
|
try {
|
|
|
out.close();
|
|
|
} catch (IOException e) {
|
|
|
e.printStackTrace();
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
return groupDataMap(fileNames);
|
|
|
} else {
|
|
|
return null;
|
|
|
}
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 删除 mongodb-GridFS文件
|
|
@ -243,11 +198,10 @@ public class GridFSUtil {
|
|
|
/**
|
|
|
* 查询fs.files 数据 in GridFS
|
|
|
*
|
|
|
* @param dbName 数据库名
|
|
|
* @param filters 查询条件
|
|
|
* @return files集合
|
|
|
*/
|
|
|
public static List<GridFSDBFile> findFiles(String dbName, Map<String, Object> filters) {
|
|
|
public static List<GridFSDBFile> findFiles( Map<String, Object> filters) {
|
|
|
Query query = new Query();
|
|
|
if (filters != null) {
|
|
|
filters.forEach((key, value) -> query.addCriteria(GridFsCriteria.where(key).is(value)));
|
|
@ -314,89 +268,39 @@ public class GridFSUtil {
|
|
|
return result;
|
|
|
}
|
|
|
|
|
|
|
|
|
/**
|
|
|
* 上传文件至Mongodb by GridFS
|
|
|
*
|
|
|
* @param db 数据库
|
|
|
* @param file 文件
|
|
|
* 上传文件至Mongodb by GridFS
|
|
|
* @param saveFileName 保存到mongo的文件名
|
|
|
* @param inputStream 文件流
|
|
|
* @param params metaData数据
|
|
|
* @return
|
|
|
*/
|
|
|
public static String uploadFile(MongoDatabase db, CommonsMultipartFile file) {
|
|
|
//创建一个容器
|
|
|
GridFSBucket gridFS = GridFSBuckets.create(db);
|
|
|
|
|
|
//自定义字段
|
|
|
public static String uploadFile( InputStream inputStream,String saveFileName,Map<String ,Object> params) {
|
|
|
//metaData参数
|
|
|
Document metaDocument = new Document();
|
|
|
// Create some custom options
|
|
|
GridFSUploadOptions gridFSUploadOptions = new GridFSUploadOptions()
|
|
|
.chunkSizeBytes(defaultChunkSize).metadata(metaDocument);
|
|
|
String fileName = UUID.randomUUID() + file.getFileItem().getName();
|
|
|
GridFSUploadStream uploadStream = gridFS.openUploadStream(fileName, gridFSUploadOptions);
|
|
|
try {
|
|
|
byte[] data = file.getBytes();
|
|
|
uploadStream.write(data);
|
|
|
ObjectId id = uploadStream.getFileId();
|
|
|
if (id != null) {
|
|
|
return fileName;
|
|
|
}
|
|
|
} finally {
|
|
|
if (uploadStream != null) {
|
|
|
uploadStream.close();
|
|
|
if (params != null && params.size() > 0) {
|
|
|
for (Map.Entry<String, Object> entry : params.entrySet()) {
|
|
|
String key = entry.getKey();
|
|
|
metaDocument.append(key, entry.getValue());
|
|
|
}
|
|
|
}
|
|
|
return "";
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 上传文件至Mongodb by GridFS
|
|
|
*
|
|
|
* @param dbName 数据库名
|
|
|
* @param filePath 文件路径
|
|
|
*/
|
|
|
public static String uploadFile(String dbName, String filePath, String fileName) {
|
|
|
//自定义字段
|
|
|
Document metaDocument = new Document();
|
|
|
// Create some custom options
|
|
|
GridFSUploadOptions gridFSUploadOptions = new GridFSUploadOptions()
|
|
|
.chunkSizeBytes(defaultChunkSize).metadata(metaDocument);
|
|
|
FileInputStream fileInputStream = null;
|
|
|
|
|
|
// Create some custom options
|
|
|
// GridFSUploadOptions gridFSUploadOptions = new GridFSUploadOptions()
|
|
|
// .chunkSizeBytes(defaultChunkSize).metadata(metaDocument);
|
|
|
// String fileName = UUID.randomUUID() + file.getFileItem().getName();
|
|
|
// GridFSUploadStream uploadStream = gridFS.openUploadStream(fileName, gridFSUploadOptions);
|
|
|
// try {
|
|
|
// byte[] data = FileUtil.toByteArray(filePath);
|
|
|
// uploadStream.write(data);
|
|
|
// ObjectId id = uploadStream.getFileId();
|
|
|
// if (id != null) {
|
|
|
// return fileName;
|
|
|
// }
|
|
|
// } finally {
|
|
|
// if (uploadStream != null) {
|
|
|
// uploadStream.close();
|
|
|
// }
|
|
|
// }
|
|
|
|
|
|
try {
|
|
|
fileInputStream = new FileInputStream(filePath);
|
|
|
com.mongodb.gridfs.GridFSFile gridFSFile = gridFsOperations.store(fileInputStream, gridFSUploadOptions);
|
|
|
ObjectId id = (ObjectId) gridFSFile.getId();
|
|
|
if (id != null) {
|
|
|
return fileName;
|
|
|
com.mongodb.gridfs.GridFSFile gridFSFile = gridFsOperations.store(inputStream, saveFileName, "", metaDocument);
|
|
|
if (gridFSFile != null) {
|
|
|
return saveFileName;
|
|
|
}
|
|
|
} catch (Exception e) {
|
|
|
e.printStackTrace();
|
|
|
} finally {
|
|
|
if (fileInputStream != null) {
|
|
|
if (inputStream != null) {
|
|
|
try {
|
|
|
fileInputStream.close();
|
|
|
inputStream.close();
|
|
|
} catch (IOException e) {
|
|
|
e.printStackTrace();
|
|
|
}
|
|
|
}
|
|
|
return null;
|
|
|
}
|
|
|
return "";
|
|
|
}
|
|
|
|
|
|
/**
|
|
@ -420,11 +324,10 @@ public class GridFSUtil {
|
|
|
/**
|
|
|
* 读取文件内容
|
|
|
*
|
|
|
* @param dbName 数据库名
|
|
|
* @param fileName 文件名
|
|
|
* @return
|
|
|
*/
|
|
|
public static String readFile(String dbName, String fileName) {
|
|
|
public static String readFileContent( String fileName) {
|
|
|
try {
|
|
|
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
|
|
|
GridFSDBFile gridFSDBFile = gridFsOperations.findOne(Query.query(GridFsCriteria.where("filename").is(fileName)));
|
|
@ -445,14 +348,19 @@ public class GridFSUtil {
|
|
|
* @param fileName GridFS文件名
|
|
|
* @return
|
|
|
*/
|
|
|
public static String downFile(String dbName, String savePath, String fileName) {
|
|
|
public static String downFile( String savePath, String fileName) {
|
|
|
FileOutputStream fileOutputStream = null;
|
|
|
try {
|
|
|
File file = new File(savePath);
|
|
|
fileOutputStream = new FileOutputStream(file);
|
|
|
GridFSDBFile gridFSDBFile = gridFsOperations.findOne(Query.query(GridFsCriteria.where("").is(fileName)));
|
|
|
gridFSDBFile.writeTo(fileOutputStream);
|
|
|
return savePath;
|
|
|
List<GridFSDBFile> gridFSDBFiles = gridFsOperations.find(Query.query(GridFsCriteria.where("filename").is(fileName)));
|
|
|
if (gridFSDBFiles==null || gridFSDBFiles.isEmpty()){
|
|
|
return null;
|
|
|
}else {
|
|
|
GridFSDBFile gridFSDBFile = gridFSDBFiles.get(0);
|
|
|
gridFSDBFile.writeTo(fileOutputStream);
|
|
|
return savePath;
|
|
|
}
|
|
|
} catch (IOException e) {
|
|
|
e.printStackTrace();
|
|
|
} finally {
|