增加mongo gridFS存储服务
This commit is contained in:
19
README.md
19
README.md
@@ -48,16 +48,27 @@ http://localhost:9091/files/index/
|
|||||||
#### 截图预览
|
#### 截图预览
|
||||||
首页界面:
|
首页界面:
|
||||||
<br>
|
<br>
|
||||||
<img alt="XyTalk IM" src="http://111.230.157.216/img/index.png" />
|
<img alt="XyTalk FS" src="http://111.230.157.216/img/index.png" WIDTH="800" />
|
||||||
|
<br>
|
||||||
文件管理:
|
文件管理:
|
||||||
<br>
|
<br>
|
||||||
<img alt="XyTalk IM" src="http://111.230.157.216/img/files.png" />
|
<img alt="XyTalk FS" src="http://111.230.157.216/img/files.png" WIDTH="800" />
|
||||||
|
<br>
|
||||||
API管理:
|
API管理:
|
||||||
<br>
|
<br>
|
||||||
<img alt="XyTalk IM" src="http://111.230.157.216/img/api.png" />
|
<img alt="XyTalk FS" src="http://111.230.157.216/img/api.png" WIDTH="800"/>
|
||||||
|
<br>
|
||||||
测试上传:
|
测试上传:
|
||||||
<br>
|
<br>
|
||||||
<img alt="XyTalk IM" src="http://111.230.157.216/img/upload.png" />
|
<img alt="XyTalk FS" src="http://111.230.157.216/img/upload.png" WIDTH="800" />
|
||||||
|
<br>
|
||||||
|
上传到七牛云效果:
|
||||||
|
<br>
|
||||||
|
<img alt="XyTalk FS" src="http://111.230.157.216/img/qiniufile.png" WIDTH="800"/>
|
||||||
|
<br>
|
||||||
|
上传到Mongo GridFS效果:
|
||||||
|
<br>
|
||||||
|
<img alt="XyTalk FS" src="http://111.230.157.216/img/gridfsfile.png" WIDTH="800" />
|
||||||
|
|
||||||
#### 安装、使用教程
|
#### 安装、使用教程
|
||||||
|
|
||||||
|
|||||||
@@ -19,7 +19,7 @@ storage.rename = true
|
|||||||
storage.todisk = true
|
storage.todisk = true
|
||||||
storage.toqiniu = true
|
storage.toqiniu = true
|
||||||
storage.tofastdfs = false
|
storage.tofastdfs = false
|
||||||
storage.tomongodb = false
|
storage.tomongodb = true
|
||||||
storage.toseaweedfs = false
|
storage.toseaweedfs = false
|
||||||
storage.toalioss = false
|
storage.toalioss = false
|
||||||
storage.tocfs = false
|
storage.tocfs = false
|
||||||
|
|||||||
@@ -161,6 +161,10 @@ public class FileUploadDownloadController {
|
|||||||
dbFile.setUrlqiniu(prop.getQiniuprefix() + fileName);
|
dbFile.setUrlqiniu(prop.getQiniuprefix() + fileName);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (prop.isTomongodb()) {
|
||||||
|
dbFile.setUrlmongodb(fileId);
|
||||||
|
}
|
||||||
|
|
||||||
diskfileRepository.save(dbFile);
|
diskfileRepository.save(dbFile);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -6,6 +6,7 @@ import java.util.HashMap;
|
|||||||
import java.util.LinkedHashMap;
|
import java.util.LinkedHashMap;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
import java.util.UUID;
|
||||||
|
|
||||||
import javax.servlet.http.HttpServletResponse;
|
import javax.servlet.http.HttpServletResponse;
|
||||||
|
|
||||||
@@ -17,9 +18,11 @@ import com.mongodb.DB;
|
|||||||
import com.mongodb.DBObject;
|
import com.mongodb.DBObject;
|
||||||
import com.mongodb.MongoClient;
|
import com.mongodb.MongoClient;
|
||||||
import com.mongodb.ServerAddress;
|
import com.mongodb.ServerAddress;
|
||||||
|
import com.mongodb.client.gridfs.GridFSUploadStream;
|
||||||
import com.mongodb.gridfs.GridFS;
|
import com.mongodb.gridfs.GridFS;
|
||||||
import com.mongodb.gridfs.GridFSDBFile;
|
import com.mongodb.gridfs.GridFSDBFile;
|
||||||
import com.mongodb.gridfs.GridFSFile;
|
import com.mongodb.gridfs.GridFSFile;
|
||||||
|
import com.qiniu.storage.model.DefaultPutRet;
|
||||||
|
|
||||||
import xy.FileSystem.File.FileListener;
|
import xy.FileSystem.File.FileListener;
|
||||||
import xy.FileSystem.File.UploadFileExt;
|
import xy.FileSystem.File.UploadFileExt;
|
||||||
@@ -30,8 +33,7 @@ import xy.FileSystem.Propert.StorageProperties;
|
|||||||
public class MongoService implements FileListener{
|
public class MongoService implements FileListener{
|
||||||
@Autowired
|
@Autowired
|
||||||
private StorageProperties prop;
|
private StorageProperties prop;
|
||||||
private MongoClient mongo;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @MethodName : getMongo
|
* @MethodName : getMongo
|
||||||
* @Description : 获取数据连接
|
* @Description : 获取数据连接
|
||||||
@@ -56,8 +58,27 @@ public class MongoService implements FileListener{
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public UploadResult Store(UploadFileExt ufe) {
|
public UploadResult Store(UploadFileExt ufe) {
|
||||||
// TODO Auto-generated method stub
|
UploadResult result = new UploadResult();
|
||||||
return null;
|
|
||||||
|
String id = UUID.randomUUID().toString();
|
||||||
|
LinkedHashMap<String, Object> map = new LinkedHashMap<String, Object>();
|
||||||
|
|
||||||
|
if (ufe.getBytes() != null) {
|
||||||
|
System.out.println("mongo gridfs upload:"+ufe.getFileName());
|
||||||
|
uploadFile(ufe,id, map);
|
||||||
|
result.fileName = ufe.getFileName();
|
||||||
|
result.fsize = ufe.getSize();
|
||||||
|
result.hash = id;
|
||||||
|
result.key = id;
|
||||||
|
result.bucket = prop.getGridfscollectionname();
|
||||||
|
result.location = id;
|
||||||
|
result.tag = "";
|
||||||
|
}
|
||||||
|
System.out.println("#########################");
|
||||||
|
System.out.println("Mongo GridFS upload success,file id:"+ result.location);
|
||||||
|
System.out.println("#########################");
|
||||||
|
|
||||||
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@@ -66,52 +87,37 @@ public class MongoService implements FileListener{
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
public void uploadFile(UploadFileExt ufe,String id,LinkedHashMap<String, Object> map){
|
||||||
* @MethodName : uploadFile
|
try {
|
||||||
* @Description : 上传文件
|
|
||||||
* @param file :文件,File类型
|
DB db = getMongo().getDB(prop.getGridfsdbname());
|
||||||
* @param id :唯一标示文件,可根据id查询到文件.必须设置
|
GridFS gridFS= new GridFS(db,prop.getGridfscollectionname());
|
||||||
* @param prop.getGridfsdbname() :库名,每个系统使用一个库
|
GridFSFile gridFSFile = gridFS.createFile(ufe.getBytes());
|
||||||
* @param prop.getGridfscollectionname():集合名,如果传入的集合名库中没有,则会自动新建并保存
|
|
||||||
* @param map:放入你想要保存的属性,例如文件类型(“congtentType”".jpg"),字符串类型,区分大小写,如果属性没有的话会自动创建并保存
|
//将文件属性设置到
|
||||||
*/
|
gridFSFile.put("_id", id);
|
||||||
public void uploadFile(File file,String id,LinkedHashMap<String, Object> map){
|
gridFSFile.put("contentType", ufe.getMimeType());
|
||||||
//把mongoDB的数据库地址配置在外部。
|
gridFSFile.put("filename", ufe.getFileName());
|
||||||
|
//循环设置的参数
|
||||||
try {
|
if (map != null && map.size() > 0) {
|
||||||
|
for (String key : map.keySet()) {
|
||||||
DB db = mongo.getDB(prop.getGridfsdbname());
|
gridFSFile.put(key, map.get(key));
|
||||||
System.out.println(db.toString());
|
}
|
||||||
//每个库中可以分子集
|
|
||||||
GridFS gridFS= new GridFS(db,prop.getGridfscollectionname());
|
|
||||||
|
|
||||||
// 创建gridfsfile文件
|
|
||||||
GridFSFile gridFSFile = gridFS.createFile(file);
|
|
||||||
//判断是否已经存在文件,如果存在则先删除
|
|
||||||
GridFSDBFile gridFSDBFile=getFileById(id);
|
|
||||||
if(gridFSDBFile!=null){
|
|
||||||
deleteFile(id);
|
|
||||||
}
|
|
||||||
//将文件属性设置到
|
|
||||||
gridFSFile.put("_id", id);
|
|
||||||
//循环设置的参数
|
|
||||||
if (map != null && map.size() > 0) {
|
|
||||||
for (String key : map.keySet()) {
|
|
||||||
gridFSFile.put(key, map.get(key));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//保存上传
|
||||||
|
gridFSFile.save();
|
||||||
|
getMongo().close();
|
||||||
|
} catch (Exception e) {
|
||||||
|
e.printStackTrace();
|
||||||
}
|
}
|
||||||
//保存上传
|
finally{
|
||||||
gridFSFile.save();
|
getMongo().close();
|
||||||
mongo.close();
|
}
|
||||||
} catch (Exception e) {
|
|
||||||
e.printStackTrace();
|
|
||||||
}
|
}
|
||||||
finally{
|
|
||||||
mongo.close();
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @MethodName : deleteFile
|
* @MethodName : deleteFile
|
||||||
* @Description : 删除文件
|
* @Description : 删除文件
|
||||||
@@ -123,7 +129,7 @@ public class MongoService implements FileListener{
|
|||||||
|
|
||||||
try {
|
try {
|
||||||
//获得库
|
//获得库
|
||||||
DB db= mongo.getDB(prop.getGridfsdbname());
|
DB db= getMongo().getDB(prop.getGridfsdbname());
|
||||||
//获得子集
|
//获得子集
|
||||||
GridFS gridFS= new GridFS(db,prop.getGridfscollectionname());
|
GridFS gridFS= new GridFS(db,prop.getGridfscollectionname());
|
||||||
//删除文件
|
//删除文件
|
||||||
@@ -133,7 +139,7 @@ public class MongoService implements FileListener{
|
|||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
}
|
}
|
||||||
finally{
|
finally{
|
||||||
mongo.close();
|
getMongo().close();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -150,7 +156,7 @@ public class MongoService implements FileListener{
|
|||||||
|
|
||||||
try {
|
try {
|
||||||
//获得库
|
//获得库
|
||||||
DB db= mongo.getDB(prop.getGridfsdbname());
|
DB db= getMongo().getDB(prop.getGridfsdbname());
|
||||||
//获得子集
|
//获得子集
|
||||||
GridFS gridFS= new GridFS(db,prop.getGridfscollectionname());
|
GridFS gridFS= new GridFS(db,prop.getGridfscollectionname());
|
||||||
Map<String,String> map = new HashMap<String,String>();
|
Map<String,String> map = new HashMap<String,String>();
|
||||||
@@ -163,7 +169,7 @@ public class MongoService implements FileListener{
|
|||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
}
|
}
|
||||||
finally{
|
finally{
|
||||||
mongo.close();
|
getMongo().close();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -179,7 +185,7 @@ public class MongoService implements FileListener{
|
|||||||
GridFSDBFile gridFSDBFile=null;
|
GridFSDBFile gridFSDBFile=null;
|
||||||
try {
|
try {
|
||||||
//获得库
|
//获得库
|
||||||
DB db= mongo.getDB(prop.getGridfsdbname());
|
DB db= getMongo().getDB(prop.getGridfsdbname());
|
||||||
//获得子集
|
//获得子集
|
||||||
GridFS gridFS= new GridFS(db,prop.getGridfscollectionname());
|
GridFS gridFS= new GridFS(db,prop.getGridfscollectionname());
|
||||||
//获得文件
|
//获得文件
|
||||||
@@ -189,7 +195,7 @@ public class MongoService implements FileListener{
|
|||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
}
|
}
|
||||||
finally{
|
finally{
|
||||||
mongo.close();
|
getMongo().close();
|
||||||
}
|
}
|
||||||
//返回数据
|
//返回数据
|
||||||
return gridFSDBFile;
|
return gridFSDBFile;
|
||||||
@@ -207,7 +213,7 @@ public class MongoService implements FileListener{
|
|||||||
try {
|
try {
|
||||||
|
|
||||||
//获得库
|
//获得库
|
||||||
DB db= mongo.getDB(prop.getGridfsdbname());
|
DB db= getMongo().getDB(prop.getGridfsdbname());
|
||||||
//获得子集
|
//获得子集
|
||||||
GridFS gridFS= new GridFS(db,prop.getGridfscollectionname());
|
GridFS gridFS= new GridFS(db,prop.getGridfscollectionname());
|
||||||
//获得文件
|
//获得文件
|
||||||
@@ -217,7 +223,7 @@ public class MongoService implements FileListener{
|
|||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
}
|
}
|
||||||
finally{
|
finally{
|
||||||
mongo.close();
|
getMongo().close();
|
||||||
}
|
}
|
||||||
//返回数据
|
//返回数据
|
||||||
return gridFSDBFileList;
|
return gridFSDBFileList;
|
||||||
|
|||||||
@@ -19,7 +19,7 @@ storage.rename = true
|
|||||||
storage.todisk = true
|
storage.todisk = true
|
||||||
storage.toqiniu = true
|
storage.toqiniu = true
|
||||||
storage.tofastdfs = false
|
storage.tofastdfs = false
|
||||||
storage.tomongodb = false
|
storage.tomongodb = true
|
||||||
storage.toseaweedfs = false
|
storage.toseaweedfs = false
|
||||||
storage.toalioss = false
|
storage.toalioss = false
|
||||||
storage.tocfs = false
|
storage.tocfs = false
|
||||||
|
|||||||
@@ -33,12 +33,12 @@
|
|||||||
<thead>
|
<thead>
|
||||||
<tr>
|
<tr>
|
||||||
<th><a class="sorted" sd:pagination-sort="fileid" >文件ID</a></th>
|
<th><a class="sorted" sd:pagination-sort="fileid" >文件ID</a></th>
|
||||||
<th>appId</th>
|
<th><a class="sorted" sd:pagination-sort="appid" >appId</a></th>
|
||||||
<th>上传者</th>
|
<th><a class="sorted" sd:pagination-sort="uploadUser" >上传者</a></th>
|
||||||
<th>文件名</th>
|
<th>文件名</th>
|
||||||
<th>大小(KB)</th>
|
<th>大小(KB)</th>
|
||||||
<th>OSS下载</th>
|
<th>OSS下载</th>
|
||||||
<th>云端下载</th>
|
<th>云端下载通道</th>
|
||||||
</tr>
|
</tr>
|
||||||
</thead>
|
</thead>
|
||||||
<tbody>
|
<tbody>
|
||||||
@@ -51,14 +51,28 @@
|
|||||||
|
|
||||||
<td >
|
<td >
|
||||||
<div class="d-flex align-items-center">
|
<div class="d-flex align-items-center">
|
||||||
<A th:href="${row.urldisk}" target="_blank" class="stamp stamp-md bg-blue mr-3"><i class="fe fe-download"></i></A>
|
<A th:href="${row.urldisk}" target="_blank" class="stamp stamp-md bg-blue mr-3" data-toggle="tooltip" title="OSS下载"><i class="fe fe-download" ></i></A>
|
||||||
<div>
|
<div>
|
||||||
|
|
||||||
</td>
|
</td>
|
||||||
<td >
|
<td >
|
||||||
<div class="d-flex align-items-center">
|
<div class="d-flex align-items-center" th:if="${!#strings.isEmpty(row.urlqiniu)}">
|
||||||
<A th:href="${row.urlqiniu}" target="_blank" class="stamp stamp-md bg-blue mr-3"><i class="fe fe-download-cloud"></i></A>
|
<A th:href="${row.urlqiniu}" target="_blank" class="stamp stamp-md bg-blue mr-3" data-toggle="tooltip" title="七牛"><i class="fe fe-download-cloud" ></i></A>
|
||||||
<div>
|
<div>
|
||||||
|
|
||||||
|
<div class="d-flex align-items-center" th:if="${#objects.nullSafe(row.urlmongodb,default)}">
|
||||||
|
<A th:href="${row.urlmongodb}" target="_blank" class="stamp stamp-md bg-blue mr-3" data-toggle="tooltip" title="mongodb下载"><i class="fe fe-download-cloud"></i></A>
|
||||||
|
<div>
|
||||||
|
|
||||||
|
|
||||||
|
<div class="d-flex align-items-center" th:if="${!#strings.isEmpty(row.urlfastdfs)}">
|
||||||
|
<A th:href="${row.urlfastdfs}" target="_blank" class="stamp stamp-md bg-blue mr-3" data-toggle="tooltip" title="fastDFS下载"><i class="fe fe-download-cloud"></i></A>
|
||||||
|
<div>
|
||||||
|
|
||||||
|
<div class="d-flex align-items-center" th:if="${!#strings.isEmpty(row.urlalioss)}">
|
||||||
|
<A th:href="/files/gridfsdownload/${row.urlalioss}" target="_blank" class="stamp stamp-md bg-blue mr-3" data-toggle="tooltip" title="阿里云下载"><i class="fe fe-download-cloud"></i></A>
|
||||||
|
<div>
|
||||||
|
|
||||||
|
|
||||||
</td>
|
</td>
|
||||||
</tr>
|
</tr>
|
||||||
</tbody>
|
</tbody>
|
||||||
|
|||||||
Reference in New Issue
Block a user