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