文件服务配置
This commit is contained in:
parent
4e67f58655
commit
76339b5a2a
|
|
@ -1,14 +1,14 @@
|
|||
package com.securitycontrol.system.api;
|
||||
|
||||
import org.springframework.cloud.openfeign.FeignClient;
|
||||
import org.springframework.http.MediaType;
|
||||
import org.springframework.web.bind.annotation.PostMapping;
|
||||
import org.springframework.web.bind.annotation.RequestPart;
|
||||
import org.springframework.web.multipart.MultipartFile;
|
||||
import com.securitycontrol.common.core.constant.SecurityConstants;
|
||||
import com.securitycontrol.common.core.constant.ServiceNameConstants;
|
||||
import com.securitycontrol.common.core.domain.Result;
|
||||
import com.securitycontrol.system.api.domain.SysFile;
|
||||
import com.securitycontrol.system.api.factory.RemoteFileFallbackFactory;
|
||||
import org.springframework.cloud.openfeign.FeignClient;
|
||||
import org.springframework.http.MediaType;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
import org.springframework.web.multipart.MultipartFile;
|
||||
|
||||
/**
|
||||
* 文件服务
|
||||
|
|
@ -16,8 +16,7 @@ import com.securitycontrol.system.api.factory.RemoteFileFallbackFactory;
|
|||
* @author czc
|
||||
*/
|
||||
@FeignClient(contextId = "remoteFileService", value = ServiceNameConstants.FILE_SERVICE, fallbackFactory = RemoteFileFallbackFactory.class)
|
||||
public interface RemoteFileService
|
||||
{
|
||||
public interface RemoteFileService {
|
||||
/**
|
||||
* 上传文件
|
||||
*
|
||||
|
|
@ -26,4 +25,17 @@ public interface RemoteFileService
|
|||
*/
|
||||
@PostMapping(value = "/upload", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
|
||||
public Result<SysFile> upload(@RequestPart(value = "file") MultipartFile file);
|
||||
|
||||
/**
|
||||
* 获取图片的base64地址
|
||||
*
|
||||
* @param fileId
|
||||
* @param source
|
||||
* @return Result<SysFile>
|
||||
* @description
|
||||
* @author cwchen
|
||||
* @date 2024/3/21 15:24
|
||||
*/
|
||||
@PostMapping(value = "/file/getImgBase64")
|
||||
public Result<SysFile> getImgBase64(@RequestBody String fileId, @RequestHeader(SecurityConstants.FROM_SOURCE) String source);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -30,6 +30,11 @@ public class RemoteFileFallbackFactory implements FallbackFactory<RemoteFileServ
|
|||
{
|
||||
return Result.fail("上传文件失败:" + throwable.getMessage());
|
||||
}
|
||||
|
||||
@Override
|
||||
public Result<SysFile> getImgBase64(String fileId, String source) {
|
||||
return Result.fail("文件服务调用失败---获取图片base64地址:" + throwable.getMessage());
|
||||
}
|
||||
};
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -19,7 +19,7 @@ public class ServiceNameConstants {
|
|||
/**
|
||||
* 文件服务的serviceid
|
||||
*/
|
||||
public static final String FILE_SERVICE = "securityControl-file";
|
||||
public static final String FILE_SERVICE = "securityControl-files";
|
||||
|
||||
/**
|
||||
* 分析决策的serviceid
|
||||
|
|
|
|||
|
|
@ -65,7 +65,8 @@ public class ParamSecureHandler implements AsyncHandlerInterceptor {
|
|||
|
||||
public boolean isFileUpload(HttpServletRequest request) {
|
||||
for (String excludeUrl : EXCLUDE_URLS) {
|
||||
if (Objects.equals(excludeUrl, request.getRequestURI())) {
|
||||
// 忽略文件上传
|
||||
if (Objects.equals(excludeUrl, request.getRequestURI()) || request.getRequestURI().contains("file")) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -86,6 +86,10 @@
|
|||
<groupId>net.logstash.logback</groupId>
|
||||
<artifactId>logstash-logback-encoder</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-data-mongodb</artifactId>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
<build>
|
||||
<finalName>${project.artifactId}</finalName>
|
||||
|
|
|
|||
|
|
@ -0,0 +1,44 @@
|
|||
package com.securitycontrol.files;
|
||||
|
||||
import com.securitycontrol.common.core.domain.Result;
|
||||
import com.securitycontrol.files.mongodb.service.FileUploadService;
|
||||
import com.securitycontrol.files.mongodb.util.MongodbFileUtil;
|
||||
import com.securitycontrol.files.mongodb.vo.FileExportVo;
|
||||
import com.securitycontrol.system.api.domain.SysFile;
|
||||
import io.swagger.annotations.ApiOperation;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
|
||||
/**
|
||||
* @author:cwchen
|
||||
* @date:2024-03-21-14:58
|
||||
* @version:1.0
|
||||
* @description:文件上传
|
||||
*/
|
||||
@RestController
|
||||
@RequestMapping("/file/")
|
||||
@Slf4j
|
||||
public class FileUploadController {
|
||||
|
||||
@Resource(name = "fileMongoServiceImpl")
|
||||
private FileUploadService mongoService;
|
||||
|
||||
@ApiOperation(value = "获取图片base64地址")
|
||||
@PostMapping("getImgBase64")
|
||||
public Result<SysFile> getImgBase64(@RequestBody String fileId){
|
||||
try {
|
||||
SysFile sysFile = new SysFile();
|
||||
FileExportVo fileExportVo = mongoService.downloadFile(fileId);
|
||||
String base64 = MongodbFileUtil.getBase64(fileExportVo);
|
||||
sysFile.setName(fileExportVo.getFileName());
|
||||
sysFile.setUrl(base64);
|
||||
return Result.ok(sysFile);
|
||||
} catch (Exception e) {
|
||||
log.error("获取图片的base64地址",e);
|
||||
return Result.fail();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
|
@ -3,6 +3,7 @@ package com.securitycontrol.files;
|
|||
import com.securitycontrol.common.security.annotation.EnableCustomConfig;
|
||||
import com.securitycontrol.common.security.annotation.EnableRyFeignClients;
|
||||
import com.securitycontrol.common.swagger.annotation.EnableCustomSwagger2;
|
||||
import org.mybatis.spring.annotation.MapperScan;
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
|
||||
|
|
|
|||
|
|
@ -0,0 +1,57 @@
|
|||
package com.securitycontrol.files.mongodb.config;
|
||||
|
||||
import com.mongodb.client.MongoClient;
|
||||
import com.mongodb.client.MongoClients;
|
||||
import com.mongodb.client.MongoDatabase;
|
||||
import com.mongodb.client.gridfs.GridFSBucket;
|
||||
import com.mongodb.client.gridfs.GridFSBuckets;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
|
||||
|
||||
/**
|
||||
* @author coisini
|
||||
* @version 1.0
|
||||
* @Description MongoDB配置类
|
||||
* @date Apr 17, 2022
|
||||
*/
|
||||
@Configuration
|
||||
public class MongoConfig {
|
||||
/**
|
||||
* 数据库配置信息
|
||||
*/
|
||||
@Value("${spring.data.mongodb.database}")
|
||||
private String db;
|
||||
|
||||
@Value("${spring.data.mongodb.host}")
|
||||
private String host;
|
||||
|
||||
@Value("${spring.data.mongodb.port}")
|
||||
private Integer port;
|
||||
|
||||
private static final String UN = "zhgd";
|
||||
|
||||
private static final String PD = "Bonus%40admin123";
|
||||
|
||||
private MongoDatabase mongoDatabase;
|
||||
|
||||
public MongoClient getMongoClient() {
|
||||
String addr = "mongodb://" + UN + ":" + PD + "@" + host + ":" + port + "/" + db;
|
||||
MongoClient mongoClient = MongoClients.create(addr);
|
||||
return mongoClient;
|
||||
}
|
||||
|
||||
public MongoDatabase getMongoConn() {
|
||||
MongoClient mongoClient = getMongoClient();
|
||||
mongoDatabase = mongoClient.getDatabase(db);
|
||||
System.out.println("mongodb数据库连接成功!");
|
||||
return mongoDatabase;
|
||||
}
|
||||
|
||||
@Bean
|
||||
public GridFSBucket getGridFsBucket() {
|
||||
MongoDatabase mongoDatabase = getMongoConn();
|
||||
return GridFSBuckets.create(mongoDatabase);
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,71 @@
|
|||
package com.securitycontrol.files.mongodb.model;
|
||||
|
||||
import lombok.Builder;
|
||||
import lombok.Data;
|
||||
import org.bson.types.Binary;
|
||||
import org.springframework.data.annotation.Id;
|
||||
import org.springframework.data.mongodb.core.mapping.Document;
|
||||
|
||||
/**
|
||||
* @author coisini
|
||||
* @version 1.0
|
||||
* @Description MongoDB文件实体
|
||||
* @date Apr 17, 2022
|
||||
*/
|
||||
@Document
|
||||
@Builder
|
||||
@Data
|
||||
public class MongoFile {
|
||||
|
||||
/**
|
||||
* 主键
|
||||
*/
|
||||
@Id
|
||||
public String id;
|
||||
|
||||
/**
|
||||
* 文件名称
|
||||
*/
|
||||
public String fileName;
|
||||
|
||||
/**
|
||||
* 文件大小
|
||||
*/
|
||||
public long fileSize;
|
||||
|
||||
/**
|
||||
* 上传时间
|
||||
*/
|
||||
public String uploadDate;
|
||||
|
||||
/**
|
||||
* MD5值
|
||||
*/
|
||||
public String md5;
|
||||
|
||||
/**
|
||||
* 文件内容
|
||||
*/
|
||||
private Binary content;
|
||||
|
||||
/**
|
||||
* 文件类型
|
||||
*/
|
||||
public String contentType;
|
||||
|
||||
/**
|
||||
* 文件后缀名
|
||||
*/
|
||||
public String suffix;
|
||||
|
||||
/**
|
||||
* 文件描述
|
||||
*/
|
||||
public String description;
|
||||
|
||||
/**
|
||||
* 大文件管理GridFS的ID
|
||||
*/
|
||||
private String gridFsId;
|
||||
|
||||
}
|
||||
|
|
@ -0,0 +1,78 @@
|
|||
package com.securitycontrol.files.mongodb.model;
|
||||
|
||||
/**
|
||||
* @author coisini
|
||||
* @version 1.0
|
||||
* @Description 统一消息
|
||||
* @date Apr 17, 2022
|
||||
*/
|
||||
public class ResponseMessage<T> {
|
||||
|
||||
private String status;
|
||||
private String message;
|
||||
private T data;
|
||||
|
||||
public static ResponseMessage<?> ok() {
|
||||
return create("0", (String) null, (Object) null);
|
||||
}
|
||||
|
||||
public static ResponseMessage<?> ok(String message) {
|
||||
return create("0", message, (Object) null);
|
||||
}
|
||||
|
||||
public static <T> ResponseMessage<T> ok(String message, T data) {
|
||||
return create("0", message, data);
|
||||
}
|
||||
|
||||
public static <T> ResponseMessage<T> ok(T data) {
|
||||
return create("0", (String) null, data);
|
||||
}
|
||||
|
||||
public static ResponseMessage<?> error() {
|
||||
return create("1", (String) null, (Object) null);
|
||||
}
|
||||
|
||||
public static ResponseMessage<?> error(String message) {
|
||||
return create("1", message, (Object) null);
|
||||
}
|
||||
|
||||
public static <T> ResponseMessage<T> error(String message, T data) {
|
||||
return create("1", message, data);
|
||||
}
|
||||
|
||||
private static <T> ResponseMessage<T> create(String status, String message, T data) {
|
||||
ResponseMessage<T> t = new ResponseMessage();
|
||||
t.setStatus(status);
|
||||
t.setMessage(message);
|
||||
t.setData(data);
|
||||
return t;
|
||||
}
|
||||
|
||||
public ResponseMessage() {
|
||||
}
|
||||
|
||||
public String getStatus() {
|
||||
return this.status;
|
||||
}
|
||||
|
||||
public String getMessage() {
|
||||
return this.message;
|
||||
}
|
||||
|
||||
public T getData() {
|
||||
return this.data;
|
||||
}
|
||||
|
||||
public void setStatus(final String status) {
|
||||
this.status = status;
|
||||
}
|
||||
|
||||
public void setMessage(final String message) {
|
||||
this.message = message;
|
||||
}
|
||||
|
||||
public void setData(final T data) {
|
||||
this.data = data;
|
||||
}
|
||||
|
||||
}
|
||||
|
|
@ -0,0 +1,15 @@
|
|||
package com.securitycontrol.files.mongodb.repository;
|
||||
|
||||
|
||||
import com.securitycontrol.files.mongodb.model.MongoFile;
|
||||
import org.springframework.data.mongodb.repository.MongoRepository;
|
||||
|
||||
/**
|
||||
* @author coisini
|
||||
* @version 1.0
|
||||
* @Description MongoDB文件仓储
|
||||
* @date Apr 17, 2022
|
||||
*/
|
||||
public interface MongoFileRepository extends MongoRepository<MongoFile, String> {
|
||||
|
||||
}
|
||||
|
|
@ -0,0 +1,51 @@
|
|||
package com.securitycontrol.files.mongodb.service;
|
||||
|
||||
import com.securitycontrol.files.mongodb.vo.FileExportVo;
|
||||
import org.springframework.web.multipart.MultipartFile;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* @author coisini
|
||||
* @version 1.0
|
||||
* @Description 文件上传接口
|
||||
* @date Apr 17, 2022
|
||||
*/
|
||||
public interface FileUploadService {
|
||||
|
||||
/**
|
||||
* 文件上传
|
||||
*
|
||||
* @param file
|
||||
* @return FileExportVo
|
||||
* @throws Exception
|
||||
* @description
|
||||
* @author cwchen
|
||||
* @date 2024/3/11 15:44
|
||||
*/
|
||||
FileExportVo uploadFile(MultipartFile file) throws Exception;
|
||||
|
||||
/**
|
||||
* 多文件上传
|
||||
*
|
||||
* @param files
|
||||
* @return
|
||||
*/
|
||||
List<FileExportVo> uploadFiles(List<MultipartFile> files);
|
||||
|
||||
/**
|
||||
* 文件下载
|
||||
*
|
||||
* @param fileId
|
||||
* @return
|
||||
*/
|
||||
FileExportVo downloadFile(String fileId);
|
||||
|
||||
/**
|
||||
* 文件删除
|
||||
*
|
||||
* @param fileId
|
||||
*/
|
||||
void removeFile(String fileId);
|
||||
|
||||
}
|
||||
|
|
@ -0,0 +1,275 @@
|
|||
package com.securitycontrol.files.mongodb.service.impl;
|
||||
|
||||
import cn.hutool.core.io.IoUtil;
|
||||
import cn.hutool.core.util.IdUtil;
|
||||
import com.mongodb.client.gridfs.GridFSBucket;
|
||||
import com.mongodb.client.gridfs.GridFSDownloadStream;
|
||||
import com.mongodb.client.gridfs.model.GridFSFile;
|
||||
import com.securitycontrol.common.core.utils.aes.DateTimeHelper;
|
||||
import com.securitycontrol.files.mongodb.model.MongoFile;
|
||||
import com.securitycontrol.files.mongodb.repository.MongoFileRepository;
|
||||
import com.securitycontrol.files.mongodb.service.FileUploadService;
|
||||
import com.securitycontrol.files.mongodb.util.Md5Util;
|
||||
import com.securitycontrol.files.mongodb.vo.FileExportVo;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.bson.types.Binary;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.data.mongodb.core.MongoTemplate;
|
||||
import org.springframework.data.mongodb.core.query.Criteria;
|
||||
import org.springframework.data.mongodb.core.query.Query;
|
||||
import org.springframework.data.mongodb.gridfs.GridFsResource;
|
||||
import org.springframework.data.mongodb.gridfs.GridFsTemplate;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.web.multipart.MultipartFile;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.util.List;
|
||||
import java.util.Objects;
|
||||
import java.util.Optional;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
/**
|
||||
* @author coisini
|
||||
* @version 1.0
|
||||
* @Description MongoDB文件上传实现类
|
||||
* @date Apr 17, 2022
|
||||
*/
|
||||
@Slf4j
|
||||
@Service("fileMongoServiceImpl")
|
||||
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
|
||||
public class FileMongoServiceImpl implements FileUploadService {
|
||||
|
||||
private final MongoFileRepository mongoFileRepository;
|
||||
private final MongoTemplate mongoTemplate;
|
||||
private final GridFsTemplate gridFsTemplate;
|
||||
private final GridFSBucket gridFSBucket;
|
||||
|
||||
private static final int MAX_SIZE = 16777216;
|
||||
private static final String SUFFIX = ".";
|
||||
|
||||
/**
|
||||
* 多文件上传
|
||||
*
|
||||
* @param files
|
||||
* @return
|
||||
*/
|
||||
@Override
|
||||
public List<FileExportVo> uploadFiles(List<MultipartFile> files) {
|
||||
|
||||
return files.stream().map(file -> {
|
||||
try {
|
||||
return this.uploadFile(file);
|
||||
} catch (Exception e) {
|
||||
log.error("文件上传失败", e);
|
||||
return null;
|
||||
}
|
||||
}).filter(Objects::nonNull).collect(Collectors.toList());
|
||||
}
|
||||
|
||||
/**
|
||||
* 文件上传
|
||||
*
|
||||
* @param file
|
||||
* @return
|
||||
* @throws Exception
|
||||
*/
|
||||
@Override
|
||||
public FileExportVo uploadFile(MultipartFile file) throws Exception {
|
||||
if (file.getSize() > MAX_SIZE) {
|
||||
return this.saveGridFsFile(file);
|
||||
} else {
|
||||
return this.saveBinaryFile(file);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 文件下载
|
||||
*
|
||||
* @param fileId
|
||||
* @return
|
||||
*/
|
||||
@Override
|
||||
public FileExportVo downloadFile(String fileId) {
|
||||
Optional<MongoFile> option = this.getBinaryFileById(fileId);
|
||||
|
||||
if (option.isPresent()) {
|
||||
MongoFile mongoFile = option.get();
|
||||
if (Objects.isNull(mongoFile.getContent())) {
|
||||
option = this.getGridFsFileById(fileId);
|
||||
}
|
||||
}
|
||||
|
||||
return option.map(FileExportVo::new).orElse(null);
|
||||
}
|
||||
|
||||
/**
|
||||
* 文件删除
|
||||
*
|
||||
* @param fileId
|
||||
*/
|
||||
@Override
|
||||
public void removeFile(String fileId) {
|
||||
Optional<MongoFile> option = this.getBinaryFileById(fileId);
|
||||
|
||||
if (option.isPresent()) {
|
||||
if (Objects.nonNull(option.get().getGridFsId())) {
|
||||
this.removeGridFsFile(fileId);
|
||||
} else {
|
||||
this.removeBinaryFile(fileId);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 删除Binary文件
|
||||
*
|
||||
* @param fileId
|
||||
*/
|
||||
public void removeBinaryFile(String fileId) {
|
||||
mongoFileRepository.deleteById(fileId);
|
||||
}
|
||||
|
||||
/**
|
||||
* 删除GridFs文件
|
||||
*
|
||||
* @param fileId
|
||||
*/
|
||||
public void removeGridFsFile(String fileId) {
|
||||
// TODO 根据id查询文件
|
||||
MongoFile mongoFile = mongoTemplate.findById(fileId, MongoFile.class);
|
||||
if (Objects.nonNull(mongoFile)) {
|
||||
// TODO 根据文件ID删除fs.files和fs.chunks中的记录
|
||||
Query deleteFileQuery = new Query().addCriteria(Criteria.where("filename").is(mongoFile.getGridFsId()));
|
||||
gridFsTemplate.delete(deleteFileQuery);
|
||||
// TODO 删除集合mongoFile中的数据
|
||||
Query deleteQuery = new Query(Criteria.where("id").is(fileId));
|
||||
mongoTemplate.remove(deleteQuery, MongoFile.class);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 保存Binary文件(小文件)
|
||||
*
|
||||
* @param file
|
||||
* @return
|
||||
* @throws Exception
|
||||
*/
|
||||
public FileExportVo saveBinaryFile(MultipartFile file) throws Exception {
|
||||
|
||||
String suffix = getFileSuffix(file);
|
||||
|
||||
MongoFile mongoFile = mongoFileRepository.save(
|
||||
MongoFile.builder()
|
||||
.fileName(file.getOriginalFilename())
|
||||
.fileSize(file.getSize())
|
||||
.content(new Binary(file.getBytes()))
|
||||
.contentType(file.getContentType())
|
||||
.uploadDate(DateTimeHelper.getNowTime())
|
||||
.suffix(suffix)
|
||||
.md5(Md5Util.getMd5(file.getInputStream()))
|
||||
.build()
|
||||
);
|
||||
|
||||
return new FileExportVo(mongoFile);
|
||||
}
|
||||
|
||||
/**
|
||||
* 保存GridFs文件(大文件)
|
||||
*
|
||||
* @param file
|
||||
* @return
|
||||
* @throws Exception
|
||||
*/
|
||||
public FileExportVo saveGridFsFile(MultipartFile file) throws Exception {
|
||||
String suffix = getFileSuffix(file);
|
||||
|
||||
String gridFsId = this.storeFileToGridFs(file.getInputStream(), file.getContentType());
|
||||
|
||||
MongoFile mongoFile = mongoTemplate.save(
|
||||
MongoFile.builder()
|
||||
.fileName(file.getOriginalFilename())
|
||||
.fileSize(file.getSize())
|
||||
.contentType(file.getContentType())
|
||||
.uploadDate(DateTimeHelper.getNowTime())
|
||||
.suffix(suffix)
|
||||
.md5(Md5Util.getMd5(file.getInputStream()))
|
||||
.gridFsId(gridFsId)
|
||||
.build()
|
||||
);
|
||||
|
||||
return new FileExportVo(mongoFile);
|
||||
}
|
||||
|
||||
/**
|
||||
* 上传文件到Mongodb的GridFs中
|
||||
*
|
||||
* @param in
|
||||
* @param contentType
|
||||
* @return
|
||||
*/
|
||||
public String storeFileToGridFs(InputStream in, String contentType) {
|
||||
String gridFsId = IdUtil.simpleUUID();
|
||||
// TODO 将文件存储进GridFS中
|
||||
gridFsTemplate.store(in, gridFsId, contentType);
|
||||
return gridFsId;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取Binary文件
|
||||
*
|
||||
* @param id
|
||||
* @return
|
||||
*/
|
||||
public Optional<MongoFile> getBinaryFileById(String id) {
|
||||
return mongoFileRepository.findById(id);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取Grid文件
|
||||
*
|
||||
* @param id
|
||||
* @return
|
||||
*/
|
||||
public Optional<MongoFile> getGridFsFileById(String id) {
|
||||
MongoFile mongoFile = mongoTemplate.findById(id, MongoFile.class);
|
||||
if (Objects.nonNull(mongoFile)) {
|
||||
Query gridQuery = new Query().addCriteria(Criteria.where("filename").is(mongoFile.getGridFsId()));
|
||||
try {
|
||||
// TODO 根据id查询文件
|
||||
GridFSFile fsFile = gridFsTemplate.findOne(gridQuery);
|
||||
// TODO 打开流下载对象
|
||||
GridFSDownloadStream in = gridFSBucket.openDownloadStream(fsFile.getObjectId());
|
||||
if (in.getGridFSFile().getLength() > 0) {
|
||||
// TODO 获取流对象
|
||||
GridFsResource resource = new GridFsResource(fsFile, in);
|
||||
// TODO 获取数据
|
||||
mongoFile.setContent(new Binary(IoUtil.readBytes(resource.getInputStream())));
|
||||
return Optional.of(mongoFile);
|
||||
} else {
|
||||
return Optional.empty();
|
||||
}
|
||||
} catch (IOException e) {
|
||||
log.error("获取MongoDB大文件失败", e);
|
||||
}
|
||||
}
|
||||
|
||||
return Optional.empty();
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取文件后缀
|
||||
*
|
||||
* @param file
|
||||
* @return
|
||||
*/
|
||||
private String getFileSuffix(MultipartFile file) {
|
||||
String suffix = "";
|
||||
if (Objects.requireNonNull(file.getOriginalFilename()).contains(SUFFIX)) {
|
||||
suffix = file.getOriginalFilename().substring(file.getOriginalFilename().lastIndexOf("."));
|
||||
}
|
||||
return suffix;
|
||||
}
|
||||
|
||||
}
|
||||
|
|
@ -0,0 +1,36 @@
|
|||
package com.securitycontrol.files.mongodb.util;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.security.MessageDigest;
|
||||
import java.security.NoSuchAlgorithmException;
|
||||
|
||||
/**
|
||||
* @author 10488
|
||||
* @version 1.0
|
||||
* @Description MD5工具类
|
||||
* @date Apr 8, 2022
|
||||
*/
|
||||
public class Md5Util {
|
||||
/**
|
||||
* 获取该输入流的MD5值
|
||||
*/
|
||||
public static String getMd5(InputStream is) throws NoSuchAlgorithmException, IOException {
|
||||
StringBuffer md5 = new StringBuffer();
|
||||
MessageDigest md = MessageDigest.getInstance("MD5");
|
||||
byte[] dataBytes = new byte[1024];
|
||||
|
||||
int nread = 0;
|
||||
while ((nread = is.read(dataBytes)) != -1) {
|
||||
md.update(dataBytes, 0, nread);
|
||||
}
|
||||
;
|
||||
byte[] mdbytes = md.digest();
|
||||
|
||||
// convert the byte to hex format
|
||||
for (int i = 0; i < mdbytes.length; i++) {
|
||||
md5.append(Integer.toString((mdbytes[i] & 0xff) + 0x100, 16).substring(1));
|
||||
}
|
||||
return md5.toString();
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,39 @@
|
|||
package com.securitycontrol.files.mongodb.util;
|
||||
|
||||
import com.securitycontrol.files.mongodb.vo.FileExportVo;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.util.Base64Utils;
|
||||
|
||||
import java.util.Objects;
|
||||
|
||||
/**
|
||||
* @author:cwchen
|
||||
* @date:2024-03-11-15:57
|
||||
* @version:1.0
|
||||
* @description:mongodb文件转base64工具类
|
||||
*/
|
||||
@Slf4j
|
||||
public class MongodbFileBase64Util {
|
||||
|
||||
/**
|
||||
* img图片转base64url
|
||||
*
|
||||
* @param vo
|
||||
* @return String
|
||||
* @description
|
||||
* @author cwchen
|
||||
* @date 2024/3/11 15:59
|
||||
*/
|
||||
public String getImgBase64(FileExportVo vo) {
|
||||
try {
|
||||
byte[] bytes = vo.getData();
|
||||
if (!Objects.isNull(bytes)) {
|
||||
String suffix = vo.getSuffix().replace(".", "");
|
||||
return "data:image/" + suffix + ";base64," + Base64Utils.encodeToString(bytes);
|
||||
}
|
||||
} catch (Exception e) {
|
||||
log.error("byte流转base64", e);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,50 @@
|
|||
package com.securitycontrol.files.mongodb.util;
|
||||
|
||||
import com.securitycontrol.files.mongodb.service.FileUploadService;
|
||||
import com.securitycontrol.files.mongodb.vo.FileExportVo;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.apache.logging.log4j.util.Base64Util;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.util.Base64Utils;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
import java.util.Objects;
|
||||
|
||||
/**
|
||||
* @author:cwchen
|
||||
* @date:2024-03-11-15:55
|
||||
* @version:1.0
|
||||
* @description:mongodb文件上传-工具类
|
||||
*/
|
||||
@Slf4j
|
||||
public class MongodbFileUtil {
|
||||
|
||||
@Resource(name = "fileMongoServiceImpl")
|
||||
private FileUploadService mongoService;
|
||||
|
||||
@Autowired
|
||||
private Base64Util base64Util;
|
||||
|
||||
/**
|
||||
* 获取文件的base64地址
|
||||
*
|
||||
* @param vo
|
||||
* @return String
|
||||
* @description
|
||||
* @author cwchen
|
||||
* @date 2024/3/12 11:00
|
||||
*/
|
||||
public static String getBase64(FileExportVo vo) {
|
||||
try {
|
||||
byte[] bytes = vo.getData();
|
||||
if (!Objects.isNull(bytes)) {
|
||||
String suffix = vo.getSuffix().replace(".", "");
|
||||
return "data:image/" + suffix + ";base64," + Base64Utils.encodeToString(bytes);
|
||||
}
|
||||
} catch (Exception e) {
|
||||
log.error("byte流转base64", e);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
}
|
||||
|
|
@ -0,0 +1,40 @@
|
|||
package com.securitycontrol.files.mongodb.vo;
|
||||
|
||||
import cn.hutool.core.bean.BeanUtil;
|
||||
import com.fasterxml.jackson.annotation.JsonIgnore;
|
||||
import com.securitycontrol.files.mongodb.model.MongoFile;
|
||||
import lombok.Data;
|
||||
|
||||
import java.util.Objects;
|
||||
|
||||
/**
|
||||
* @author lit@epsoft.com.cn
|
||||
* @version 1.0
|
||||
* @Description 统一文件下载vo
|
||||
* @date Apr 8, 2022
|
||||
*/
|
||||
@Data
|
||||
public class FileExportVo {
|
||||
|
||||
private String fileId;
|
||||
|
||||
private String fileName;
|
||||
|
||||
private String contentType;
|
||||
|
||||
private String suffix;
|
||||
|
||||
private long fileSize;
|
||||
|
||||
@JsonIgnore
|
||||
private byte[] data;
|
||||
|
||||
public FileExportVo(MongoFile mongoFile) {
|
||||
BeanUtil.copyProperties(mongoFile, this);
|
||||
if (Objects.nonNull(mongoFile.getContent())) {
|
||||
this.data = mongoFile.getContent().getData();
|
||||
}
|
||||
this.fileId = mongoFile.getId();
|
||||
}
|
||||
|
||||
}
|
||||
|
|
@ -16,17 +16,24 @@ spring:
|
|||
server-addr: 127.0.0.1:8848
|
||||
namespace: jjzhgd
|
||||
username: nacos
|
||||
password: Jjsp@nacos2023
|
||||
password: nacos
|
||||
config:
|
||||
server-addr: 127.0.0.1:8848
|
||||
namespace: jjzhgd
|
||||
username: nacos
|
||||
password: Jjsp@nacos2023
|
||||
password: nacos
|
||||
# 配置文件格式
|
||||
file-extension: yml
|
||||
# 共享配置
|
||||
shared-configs:
|
||||
- vsc-dev.yml
|
||||
data:
|
||||
mongodb:
|
||||
host: 192.168.0.56
|
||||
port: 27017
|
||||
database: zhgd
|
||||
username: zhgd
|
||||
password: Bonus@admin123
|
||||
#加密组件
|
||||
jasypt:
|
||||
encryptor:
|
||||
|
|
|
|||
Loading…
Reference in New Issue