Compare commits

..

9 Commits

Author SHA1 Message Date
578646533@qq.com 464f46cbf7 Merge branch 'develop' into test
# Conflicts:
#	bootstrap/src/main/java/com/electromagnetic/industry/software/data/manage/controller/EDDataController.java
#	domain/src/main/java/com/electromagnetic/industry/software/data/manage/domain/boardservice/indicator/service/impl/EDDataServiceImpl.java
#	service/src/main/java/com/electromagnetic/industry/software/data/manage/service/facade/EDDataFacadeImpl.java
2024-11-27 11:23:15 +08:00
chenxudong 92699d253d 解决打包的问题。 2024-11-27 10:04:51 +08:00
sxlong 9709d6a691 1.修改目录结构相关的接口代码逻辑。 2024-11-26 18:02:12 +08:00
chenxudong c91f2cc8a9 编译 2024-11-26 15:47:52 +08:00
chenxudong 1f18245c8f 文件下载接口实现 2024-11-26 10:31:35 +08:00
sxlong 286f23ad21 1.增加导出数据接口。 2024-11-26 09:31:30 +08:00
sxlong 4bc0c04706 1.增加数据导出接口。 2024-11-25 11:09:55 +08:00
chenxudong a53b06385f 改bug 2024-11-25 09:01:45 +08:00
chenxudong 516e81ba91 实现文件上传的功能。 2024-11-22 17:17:15 +08:00
22 changed files with 1406 additions and 410 deletions

View File

@ -2,14 +2,21 @@ package com.electromagnetic.industry.software.data.manage.controller;
import com.electromagnetic.industry.software.data.manage.facade.EDDataFacade;
import com.electromagnetic.industry.software.data.manage.request.indicator.EDDataRequest;
import com.electromagnetic.industry.software.data.manage.request.indicator.FileChunkDTO;
import com.electromagnetic.industry.software.data.manage.request.indicator.FileDownloadDTO;
import electromagnetic.data.framework.share.model.ElectromagneticResult;
import electromagnetic.data.framework.share.model.ElectromagneticResultUtil;
import io.swagger.annotations.ApiOperation;
import org.springframework.core.io.InputStreamResource;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Date;
import java.util.List;
@RequestMapping("/data/ed/file")
@RestController
@ -55,14 +62,14 @@ public class EDDataController {
request.setParentId(parentId);
request.setFileData(file);
request.setGmtBatchUpload(new Date(gmtBatchUpload));
return edDataFacade.upload(request);
return edDataFacade.uploadFile(request);
}
@ApiOperation(value = "下载",notes = "")
@RequestMapping(value = "/download",method = RequestMethod.POST)
public ElectromagneticResult<?> download(@RequestBody EDDataRequest request){
return edDataFacade.download(request);
@RequestMapping(value = "/download",method = RequestMethod.GET)
public ResponseEntity<InputStreamResource> download(@RequestBody FileDownloadDTO fileDownloadDTO, HttpServletResponse response) throws IOException {
return edDataFacade.download(fileDownloadDTO, response);
}
@ -72,5 +79,36 @@ public class EDDataController {
return ElectromagneticResultUtil.success("复制");
}
@ApiOperation(value = "数据导出",notes = "")
@RequestMapping(value = "/batchExport",method = RequestMethod.POST)
public ElectromagneticResult<?> batchExport(@RequestBody EDDataRequest request){
return edDataFacade.batchExport(request);
}
@ApiOperation(value = "获取已经上传的分片",notes = "")
@RequestMapping(value = "/getUploadedChunkNums",method = RequestMethod.GET)
public ElectromagneticResult<?> getUploadedChunkNums(@RequestParam String identifier) {
return edDataFacade.getUploadedChunkNums(identifier);
}
@ApiOperation(value = "合并分片",notes = "")
@RequestMapping(value = "/mergeChunks",method = RequestMethod.GET)
public ElectromagneticResult<?> mergeChunks(@RequestParam String identifier,
@RequestParam String fileName,
@RequestParam Integer totalChunks) {
return edDataFacade.mergeChunks(identifier, fileName, totalChunks);
}
@ApiOperation(value = "分片上传",notes = "")
@RequestMapping(value = "/batchImport",method = RequestMethod.POST)
public ElectromagneticResult<?> batchImport(FileChunkDTO fileChunkDTO) {
return edDataFacade.batchImport(fileChunkDTO);
}
@ApiOperation(value = "检查分片是否存在",notes = "")
@RequestMapping(value = "/batchImport",method = RequestMethod.GET)
public ElectromagneticResult<?> checkChunkExist(FileChunkDTO fileChunkDTO) {
return edDataFacade.checkChunkExist(fileChunkDTO);
}
}

View File

@ -1,4 +1,4 @@
#required
#required
spring.application.name=electromagnetic-data
# security
run.mode=NORMAL
@ -18,7 +18,8 @@ spring.datasource.typd=com.alibaba.druid.pool.DruidDataSource
spring.datasource.url=jdbc:mysql://139.224.43.89:3306/em_data?serverTimezone=UTC&useUnicode=true&characterEncoding=utf-8&useSSL=true&serverTimezone=GMT%2B8&allowMultiQueries=true&rewriteBatchedStatements=true
spring.datasource.username=comac
spring.datasource.password=2024*Comac
spring.servlet.multipart.max-file-size=500MB
spring.servlet.multipart.max-request-size=10MB
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.type=com.alibaba.druid.pool.DruidDataSource
@ -26,4 +27,15 @@ mybatis.mapper-locations=classpath:sqlmapper/*.xml
pagehelper.helperDialect=mysql
pagehelper.reasonable=false
server.port=8888
zip.password=~~BYE6cWGBJJjsU9DczYtZIQ==
#windows文件存储目录
data.windows.path=E:/comacFileStorage/
#文件缓存路径
data.file.cache.dir=/szsd/cache/
#文件存储路径
data.file.storage.dir=/szsd/fileStorage/
#上传文件时文件的缓存文件夹名称
data.upload.cache.dir=upload
#导出数据时文件的缓存文件夹名称
data.export.cache.dir=export

View File

@ -17,6 +17,14 @@ public class EDDataParams implements Serializable {
* 每页数据
*/
private Integer pageSize = 10;
/**
* 操作用户id
*/
private String userId;
/**
* 用户名
*/
private String userName;
/**
* 上级文件夹数据码,为空是顶级
*/

View File

@ -42,12 +42,4 @@ public interface EDDataRepository {
*/
Boolean updateFileInfo(EDDataParams parames);
/**
* 下载
* @param parames
* @return
*/
Boolean download(EDDataParams parames);
}

View File

@ -4,6 +4,9 @@ import com.electromagnetic.industry.software.data.manage.domain.boardservice.ind
import com.electromagnetic.industry.software.data.manage.domain.boardservice.indicator.model.EDDataPage;
import com.electromagnetic.industry.software.data.manage.domain.boardservice.indicator.parames.EDDataParams;
import java.util.List;
import java.util.Map;
public interface EDDataService {
/**
@ -42,24 +45,38 @@ public interface EDDataService {
Integer getChildFileCount(EDDataParams parames);
/**
* 下载
* 获取文件的文件夹路径
* @param categoryId
* @return
*/
String getFilePathOfFolder(String categoryId);
/**
* 文件上传
* @param parames
* @return
*/
Boolean download(EDDataParams parames);
Boolean uploadFile(EDDataParams parames);
/**
* 检查文件夹是否存在
* @param path
* 文件下载
* @param parames
* @return
*/
Boolean checkFolderIsExist(String path);
List<String> downloadFile(EDDataParams parames);
/**
* 创建多级目录
* @param path
* 导出数据
* @param parames
* @return
*/
Boolean createMultiLevelDirectory(String path);
String exportData(EDDataParams parames);
/**
* 解压后的数据目录
* @param folderPath
* @return
*/
Map<String, List<String>> importData(String folderPath);
}

View File

@ -3,36 +3,102 @@
*/
package com.electromagnetic.industry.software.data.manage.domain.boardservice.indicator.service.impl;
import cn.hutool.core.io.FileUtil;
import cn.hutool.core.util.ZipUtil;
import cn.hutool.crypto.SecureUtil;
import cn.hutool.crypto.symmetric.AES;
import cn.hutool.poi.excel.cell.CellSetter;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.electromagnetic.industry.software.data.manage.domain.boardservice.category.model.Category;
import com.electromagnetic.industry.software.data.manage.domain.boardservice.indicator.model.EDDataInfo;
import com.electromagnetic.industry.software.data.manage.domain.boardservice.indicator.model.EDDataPage;
import com.electromagnetic.industry.software.data.manage.domain.boardservice.indicator.parames.EDDataParams;
import com.electromagnetic.industry.software.data.manage.domain.boardservice.indicator.repository.EDDataRepository;
import com.electromagnetic.industry.software.data.manage.domain.boardservice.indicator.service.EDDataService;
import com.electromagnetic.industry.software.data.manage.domain.boardservice.user.repository.CategoryRepository;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import electromagnetic.data.framework.share.exception.LoggerConstant;
import electromagnetic.data.framework.share.id.IdWorker;
import electromagnetic.data.framework.share.model.ElectromagneticResultUtil;
import electromagnetic.data.framework.share.util.EleZipUtil;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;
import javax.annotation.Resource;
import java.io.IOException;
import java.io.*;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.List;
import java.util.*;
/**
* @author
* @version $Id: IndicatorCardServiceImpl.java, v 0.1 2024-07-26 17:20
*/
@Service
@Slf4j
public class EDDataServiceImpl implements EDDataService {
private static final Logger LOGGER = LoggerFactory.getLogger(LoggerConstant.DOMAIN_SERVICE);
// private static final Logger LOGGER = LoggerFactory.getLogger(LoggerConstant.DOMAIN_SERVICE);
@Resource
private EDDataRepository edDataRepository;
@Resource
private CategoryRepository categoryRepository;
@Value("${zip.password}")
private String passwordZIP;
@Value("${data.windows.path}")
private String windowsDir;
@Value("${data.file.cache.dir}")
private String fileCacheDir;
@Value("${data.file.storage.dir}")
private String fileStorageDir;
@Value("${data.upload.cache.dir}")
private String uploadCacheDir;
@Value("${data.export.cache.dir}")
private String exportCacheDir;
// 文件夹名称分隔符
private static final String FOLDER_NAME_SEPARATOR = "_";
/**
* 获取时间戳字符串
* @return
*/
public String getTimeStampString() {
long timestamp = System.currentTimeMillis();
return String.valueOf(timestamp);
}
/**
* 获取数据存储目录
* @return
*/
public String getDataStoragePath()
{
String osName = System.getProperty("os.name").toLowerCase();
return osName.startsWith("win") ? windowsDir + fileStorageDir : fileStorageDir;
}
/**
* 获取数据缓存目录
* @return
*/
public String getDataCachePath()
{
String osName = System.getProperty("os.name").toLowerCase();
return osName.startsWith("win") ? windowsDir + fileCacheDir : fileCacheDir;
}
/**
* 创建文件/文件夹数据信息
@ -41,7 +107,42 @@ public class EDDataServiceImpl implements EDDataService {
*/
public Boolean createDataInfo(EDDataInfo edDataInfo)
{
edDataRepository.createDataInfo(edDataInfo);
// 获取上级目录的名称
Category categoryParent = new Category();
categoryParent.setCategoryId(edDataInfo.getCategoryId());
List<Category> categoryParentList = categoryRepository.selectCategories(categoryParent);
if(categoryParentList.size() < 1)
{
return Boolean.FALSE;
}
else
{
// 获取新文件夹路径信息
categoryParent = categoryParentList.get(0);
String dataStoragePath = getDataStoragePath();
String folderParent = categoryParent.getCategoryId() + FOLDER_NAME_SEPARATOR + categoryParent.getCategoryName() ;
String folderNew = dataStoragePath + File.separator + folderParent + File.separator + edDataInfo.getDataName();
// 判断文件夹名称是否存在
EDDataParams folderParames = new EDDataParams();
folderParames.setParentId(edDataInfo.getCategoryId());
List<EDDataInfo> childFileInfoList = edDataRepository.getDataInfoList(folderParames);
for(EDDataInfo fileInfo : childFileInfoList)
{
if(fileInfo.getDataName() == edDataInfo.getDataName()){
return Boolean.FALSE;
}
}
// 将文件夹数据写到数据库中
if(edDataRepository.createDataInfo(edDataInfo)) {
if (!FileUtil.exist(folderNew)){
FileUtil.mkdir(folderNew);
}
}
}
return Boolean.TRUE;
}
@ -78,7 +179,33 @@ public class EDDataServiceImpl implements EDDataService {
*/
public Boolean updateFileInfo(EDDataParams parames)
{
String dataStoragePath = getDataStoragePath();
if (!FileUtil.exist(dataStoragePath)){
return Boolean.FALSE;
}
EDDataParams paramesFind = new EDDataParams();
paramesFind.setDataId(parames.getDataId());
List<EDDataInfo> edDataInfoList = edDataRepository.getDataInfoList(paramesFind);
if(edDataInfoList.size() < 1) {
return Boolean.FALSE;
}
EDDataInfo edDataInfo = edDataInfoList.get(0);
JSONObject implantJsonObject = JSON.parseObject(edDataInfo.getImplantJson());
String filePathOfFolder = implantJsonObject.getString("folderPath");
String fileStorageFullPath = dataStoragePath + filePathOfFolder + File.separator + edDataInfo.getDataName();
if (!FileUtil.exist(fileStorageFullPath)){
return Boolean.FALSE;
}
String fileNameNew = parames.getName();
FileUtil.rename(Paths.get(fileStorageFullPath) ,fileNameNew,true);
edDataRepository.updateFileInfo(parames);
return Boolean.TRUE;
}
@ -108,39 +235,510 @@ public class EDDataServiceImpl implements EDDataService {
}
/**
* 下载
* 获取文件的文件夹路径
* @param categoryId
* @return
*/
public String getFilePathOfFolder(String categoryId)
{
String filePathOfFolder = ""; //文件存放在文件夹的路径
String categoryIdHighest = categoryId; //最高级的目录编码
EDDataParams folderParames = new EDDataParams();
folderParames.setDataId(categoryId);
EDDataInfo edDataInfoParent = edDataRepository.getDataInfo(folderParames);
if(edDataInfoParent != null) {
categoryIdHighest = edDataInfoParent.getCategoryId();
}
Category categoryParent = new Category();
categoryParent.setCategoryId(categoryIdHighest);
List<Category> categoryParentList = categoryRepository.selectCategories(categoryParent);
if(categoryParentList.size() > 0){
categoryParent = categoryParentList.get(0);
filePathOfFolder = categoryParent.getCategoryId() + FOLDER_NAME_SEPARATOR + categoryParent.getCategoryName();
if(edDataInfoParent != null){
filePathOfFolder = filePathOfFolder + File.separator + edDataInfoParent.getDataName();
}
}
return filePathOfFolder;
}
/**
* 文件上传
* @param parames
* @return
*/
public Boolean download(EDDataParams parames)
public Boolean uploadFile(EDDataParams parames)
{
edDataRepository.download(parames);
return Boolean.TRUE;
}
/**
* 检查文件夹是否存在
* @param path
* @return
*/
public Boolean checkFolderIsExist(String path)
{
return Files.exists(Paths.get(path));
}
// 获取目录编码ID
String categoryId = parames.getParentId();
/**
* 创建多级目录
* @param path
* @return
*/
public Boolean createMultiLevelDirectory(String path)
{
try {
Files.createDirectories(Paths.get(path));
return Boolean.TRUE;
} catch (IOException e) {
// 获取要上传的文件
MultipartFile fileInput = parames.getFileData();
// 检查文件是否为空
if (fileInput == null || fileInput.isEmpty()) {
return Boolean.FALSE;
}
// 获取文件名
String fileFullName = fileInput.getOriginalFilename();
// 获取文件类型
String fileType = "";
// 获取文件名称
String fileName = "";
if (fileFullName.lastIndexOf(".") != -1 && fileFullName.lastIndexOf(".") != 0) {
fileType = fileFullName.substring(fileFullName.lastIndexOf(".") + 1);
fileName = fileFullName.substring(fileFullName.lastIndexOf("."));
}
String dataStoragePath = getDataStoragePath();
if (!FileUtil.exist(dataStoragePath)){
FileUtil.mkdir(dataStoragePath);
}
//获取文件存放在文件夹的路径
String filePathOfFolder = getFilePathOfFolder(categoryId);
// 将文件数据信息写到数据库
EDDataInfo edDataInfo = new EDDataInfo();
{
// 临时数据设置
if (Boolean.TRUE) {
if (parames.getUserId() == null || parames.getUserId().isEmpty()) parames.setUserId(IdWorker.getSnowFlakeIdString());
if (parames.getUserName() == null || parames.getUserName().isEmpty()) parames.setUserName("user");
}
// 创建新文件数据
edDataInfo.setCreator(parames.getUserId());
edDataInfo.setCreatorName(parames.getUserName());
edDataInfo.setModifier(parames.getUserId());
edDataInfo.setModifierName(parames.getUserName());
edDataInfo.setCategoryId(parames.getParentId());
edDataInfo.setDataName(fileFullName);
edDataInfo.setNote(parames.getNote());
edDataInfo.setFileType(fileType);
edDataInfo.setGmtBatchUpload(parames.getGmtBatchUpload());
edDataInfo.setDataId(IdWorker.getSnowFlakeIdString());
edDataInfo.setDataNo(edDataInfo.getDataId());
edDataInfo.setDataType("file");
edDataInfo.setVersion("1.0.0");
edDataInfo.setDataStatus("publish");
edDataInfo.setSaveStatus("saving");
JSONObject newImplantJsonObject = new JSONObject();
newImplantJsonObject.put("folderPath", filePathOfFolder);
edDataInfo.setImplantJson(newImplantJsonObject.toJSONString());
boolean isSuccess = edDataRepository.createDataInfo(edDataInfo);
}
// 保存文件数据 文件存储目录
{
// 文件保存目录路径
String fileSavePath = dataStoragePath + File.separator + filePathOfFolder;
String newFileName = edDataInfo.getDataId() + FOLDER_NAME_SEPARATOR + fileFullName;
String dataCachePath = getDataCachePath();
String uploadFileCachePath = dataCachePath + uploadCacheDir + FOLDER_NAME_SEPARATOR + getTimeStampString();
if (!FileUtil.exist(uploadFileCachePath)){
FileUtil.mkdir(uploadFileCachePath);
}
String fileCacheFullPath = uploadFileCachePath + File.separator + newFileName;
String fileSaveFullPath = fileSavePath + File.separator + fileFullName;
log.info("文件缓存路径为: " + fileCacheFullPath);
// 这里可以添加将文件保存到本地磁盘或其他存储介质的逻辑
File saveFile = new File(fileCacheFullPath);
// 将文件保存到硬盘
try {
fileInput.transferTo(saveFile);
EDDataParams fileParames = new EDDataParams();
fileParames.setDataId(edDataInfo.getDataId());
if(fileInput.getSize() == saveFile.length())
{
Path source = Paths.get(fileCacheFullPath);
Path target = Paths.get(fileSaveFullPath);
Files.move(source, target);
fileParames.setSaveStatus("success");
}
else
{
saveFile.delete();
fileParames.setSaveStatus("failure");
}
boolean isSuccess = edDataRepository.updateFileInfo(fileParames);
FileUtil.del(uploadFileCachePath);//删除临时目录
log.info("文件保存成功: " + fileSaveFullPath);
return Boolean.TRUE;
} catch (IOException e) {
e.printStackTrace();
}
}
return Boolean.FALSE;
}
/**
* 文件下载
* @param parames
* @return
*/
public List<String> downloadFile(EDDataParams parames)
{
// 获取文件存储的文件夹路径
String dataStoragePath = getDataStoragePath();
EDDataParams paramesFileInfo = new EDDataParams();
List<String> dataPathArr = new ArrayList();
if (!FileUtil.exist(dataStoragePath)) {
dataPathArr.add("error : The directory where the file was stored was not found!");
return dataPathArr;
}
for(String dataId : parames.getDataIdArr())
{
paramesFileInfo.setDataId(dataId);
EDDataInfo edDataInfo = edDataRepository.getDataInfo(paramesFileInfo);
if(edDataInfo != null)
{
JSONObject implantJsonObject = JSON.parseObject(edDataInfo.getImplantJson());
String filePathOfFolder = implantJsonObject.getString("folderPath");
String fileStoragePath = dataStoragePath + File.separator + filePathOfFolder;
String fileStorageFullPath = fileStoragePath + File.separator + edDataInfo.getDataName();
if (FileUtil.exist(fileStoragePath)) {
if (!FileUtil.exist(fileStorageFullPath)) {
fileStorageFullPath = "error : No file found : " + fileStorageFullPath;
}
}
else {
//获取文件存放在文件夹的路径
String filePathOfFolderByDB = getFilePathOfFolder(edDataInfo.getCategoryId());
if (!FileUtil.exist(filePathOfFolderByDB)) {
fileStorageFullPath = "error : The directory path does not exist : " + edDataInfo.getCategoryId();
}
}
dataPathArr.add(fileStorageFullPath);
}
}
return dataPathArr;
}
/**
* 导出数据
* @param parames
* @return
*/
public String exportData(EDDataParams parames)
{
// 获取文件夹编码ID数组
List<String> categoryIdInputList = Arrays.asList(parames.getDataIdArr());
String dataCachePath = getDataCachePath();
// 导出数据时的临时存放目录
String exportDataCachePath = dataCachePath + exportCacheDir + FOLDER_NAME_SEPARATOR + getTimeStampString();
if (!FileUtil.exist(exportDataCachePath)){
FileUtil.mkdir(exportDataCachePath);
}
List<Category> categoryAllList = categoryRepository.getAllCategories();
Map<String, Category> categoryAllMap = new HashMap(); //所有目录数组map
for (Category category : categoryAllList) {
categoryAllMap.put(category.getCategoryId(), category);
}
EDDataParams folderParames = new EDDataParams();
List<EDDataInfo> fileAllList = edDataRepository.getDataInfoList(folderParames); //获取所有文件信息数据
Map<String, EDDataInfo> fileAllMap = new HashMap(); //所有文件数组map
for (EDDataInfo fileFinfo : fileAllList) {
fileAllMap.put(fileFinfo.getDataId(), fileFinfo);
}
List<Category> categoryInputList = new ArrayList(); //客户端输入的目录数组
Map<String, Category> categoryInputMap = new HashMap(); //客户端输入的目录数组map
List<EDDataInfo> fileInputList = new ArrayList(); //客户端输入的目录下的文件夹数组
Map<String, EDDataInfo> fileInputMap = new HashMap(); //客户端输入的目录下的文件夹数组map
for (String categoryId : categoryIdInputList) {
Category category = categoryAllMap.get(categoryId);
if(category != null) {
categoryInputList.add(category);
categoryInputMap.put(categoryId, category);
} else {
EDDataInfo folder = fileAllMap.get(categoryId);
if(folder != null) {
fileInputList.add(folder);
fileInputMap.put(categoryId, folder);
}
}
}
// 导出目录树数据为json文件
{
// 创建ObjectMapper对象用于JSON处理
ObjectMapper objectMapper = new ObjectMapper();
// 将List<Category>转换为JSON字符串
try {
String jsonString = objectMapper.writeValueAsString(categoryInputList);
// 将JSON字符串写入文件
File jsonFile = new File(exportDataCachePath + "/categoryList.json");
objectMapper.writeValue(jsonFile, jsonString);
log.info("目录树数据已成功导出为JSON文件。" + jsonFile.getAbsolutePath());
} catch (JsonProcessingException e) {
e.printStackTrace();
}
catch (IOException e) {
e.printStackTrace();
}
}
// 导出文件信息数据和文件数据
{
// 保存文件信息数据为json文件
List<EDDataInfo> fileExportList = new ArrayList(); //需要导出的文件信息数据数组
for (EDDataInfo fileFinfo : fileAllList) {
if(fileFinfo.getDataType().equals("folder")) {
if(categoryInputMap.get(fileFinfo.getCategoryId()) != null) {
fileExportList.add(fileFinfo);
}
} else if(fileFinfo.getDataType().equals("file")) {
if(fileInputMap.get(fileFinfo.getCategoryId()) != null) {
fileExportList.add(fileFinfo);
} else if(categoryInputMap.get(fileFinfo.getCategoryId()) != null) {
fileExportList.add(fileFinfo);
}
}
}
// 创建ObjectMapper对象用于JSON处理
ObjectMapper objectMapper = new ObjectMapper();
// 将List<EDDataInfo>转换为JSON字符串
try {
String jsonString = objectMapper.writeValueAsString(fileExportList);
// 将JSON字符串写入文件
File jsonFile = new File(exportDataCachePath + "/fileInfoList.json");
objectMapper.writeValue(jsonFile, jsonString);
log.info("目录树数据已成功导出为JSON文件。" + jsonFile.getAbsolutePath());
} catch (JsonProcessingException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
// 将文件 复制到 数据导出的缓存目录中
String dataStoragePath = getDataStoragePath();
String needExportfolder = "";
for (Category category : categoryInputList)
{
needExportfolder = dataStoragePath + category.getCategoryId() + FOLDER_NAME_SEPARATOR + category.getCategoryName();
if (FileUtil.exist(needExportfolder)){
Path source = Paths.get(needExportfolder);
Path target = Paths.get(exportDataCachePath);
FileUtil.copy(source, target); // StandardCopyOption.REPLACE_EXISTING
}
}
log.info("文件数据已成功复制到目标目录。");
}
// 将目录树数据 文件夹及文件夹内数据 进行压缩打包
{
String zipPathFileName = dataCachePath + "/comac_exportData1.comac";
String enCodeZipPathFileName = dataCachePath + "/comac_exportData.comac";
// EleZipUtil.zipFile(exportDataDirectory, zipPathFileName, passwordZIP);
ZipUtil.zip(exportDataCachePath, zipPathFileName);
String password = "adknfhkj87654knd";
AES aes = SecureUtil.aes(password.getBytes()); // aesKey是加密密钥
try(
InputStream inputStream = new FileInputStream(zipPathFileName);
OutputStream outputStream = new FileOutputStream(enCodeZipPathFileName);
) {
aes.encrypt(inputStream, outputStream, true);
} catch (Exception e) {
log.error("文件加密错误..", e);
}
//FileUtil.del(zipPathFileName);//删除临时目录
log.info("目录树数据+文件数据已成功复制到目标目录。" + zipPathFileName);
}
// 删除 导出数据的 缓存目录
FileUtil.del(exportDataCachePath);
return dataCachePath + "/comac_exportData.comac";
}
/**
* 将解压后的数据导入到数据库中
* @param folderPath 解压后的数据目录
* @return
*/
public Map<String, List<String>> importData(String folderPath)
{
// 获取所有目录树节点数据
List<Category> categoryAllList = categoryRepository.getAllCategories();
Map<String, Category> categoryAllMap = new HashMap(); //所有目录数组map
for (Category category : categoryAllList)
{
categoryAllMap.put(category.getCategoryId(), category);
}
// 获取所有文件信息数据
EDDataParams parames = new EDDataParams();
List<EDDataInfo> fileInfoAllList = edDataRepository.getDataInfoList(parames);
Map<String, EDDataInfo> fileInfoAllMap = new HashMap(); //所有文件数组map
for (EDDataInfo fileInfo : fileInfoAllList)
{
fileInfoAllMap.put(fileInfo.getDataId(), fileInfo);
}
// 读取并反序列化目录树的JSON文件数据为List<Category>然后写入到数据库中
List<Category> categoryImportList = null;
{
// 读取文件数据
try {
// 创建ObjectMapper对象用于JSON处理
ObjectMapper objectMapper = new ObjectMapper();
// 指定要读取的JSON文件路径
File jsonFile = new File(folderPath + "/categoryList.json");
// 将JSON数据从文件中读取并反序列化为List<Person>
categoryImportList = objectMapper.readValue(jsonFile, objectMapper.getTypeFactory().constructCollectionType(List.class, Category.class));
if (categoryImportList == null) {
log.error("读取并反序列化JSON文件数据为List<Category>失败!");
return null;
}
} catch (IOException e) {
e.printStackTrace();
}
// 将目录树数据写入到数据库中
for (Category category : categoryImportList) {
if (categoryAllMap.get(category.getCategoryId()) == null) {
categoryRepository.createCategory(category);
}
}
}
// 读取并反序列化文件信息的JSON文件数据为List<EDDataInfo>然后写入到数据库中
List<EDDataInfo> fileInfoImportList = null;
{
// 读取文件数据
try {
// 创建ObjectMapper对象用于JSON处理
ObjectMapper objectMapper = new ObjectMapper();
// 指定要读取的JSON文件路径
File jsonFile = new File(folderPath + "/fileInfoList.json");
// 将JSON数据从文件中读取并反序列化为List<Person>
fileInfoImportList = objectMapper.readValue(jsonFile, objectMapper.getTypeFactory().constructCollectionType(List.class, Category.class));
if (fileInfoImportList == null) {
log.error("读取并反序列化JSON文件数据为List<EDDataInfo>失败!");
return null;
}
} catch (IOException e) {
e.printStackTrace();
}
// 将文件信息数据写入到数据库中
for (EDDataInfo fileInfo : fileInfoImportList) {
if (fileInfoAllMap.get(fileInfo.getDataId()) == null) {
edDataRepository.createDataInfo(fileInfo);
}
}
}
// 将解压后的文件夹和文件 移动到 上传文件的存储目录中
List<String> importFileSuccess = new ArrayList();
List<String> importFileFail = new ArrayList();
{
String dataStoragePath = getDataStoragePath();
if (!FileUtil.exist(dataStoragePath)){
FileUtil.mkdir(dataStoragePath);
}
String importDataCachePath = folderPath; //导入数据时 数据文件解压后的目录
String importFileCachePath = ""; //需要导入的文件的缓存路径
String importFileCacheFullPath = ""; //需要导入的文件的全路径
String fileStorageFolder = ""; //文件存储的文件夹
String fileStoragePath = ""; //文件存储的文件夹路径
String fileStorageFullPath = ""; //文件存储的文件全路径
try {
for (EDDataInfo fileInfo : fileInfoImportList) {
JSONObject newImplantJsonObject = JSONObject.parseObject(fileInfo.getImplantJson());
fileStorageFolder = newImplantJsonObject.getString("folderPath");
importFileCachePath = importDataCachePath + fileStorageFolder;
importFileCacheFullPath = importFileCachePath + File.separator + fileInfo.getDataName();
fileStoragePath = dataStoragePath + fileStorageFolder;
fileStorageFullPath = fileStoragePath + File.separator + fileInfo.getDataName();
if (fileInfoAllMap.get(fileInfo.getDataId()) == null
&& !fileStorageFolder.isEmpty()
&& FileUtil.exist(importFileCachePath)
&& FileUtil.exist(importFileCacheFullPath)
&& !FileUtil.exist(fileStorageFullPath)
)
{
if(!FileUtil.exist(fileStoragePath)){
FileUtil.mkdir(fileStoragePath);
}
Path source = Paths.get(importFileCacheFullPath);
Path target = Paths.get(fileStorageFullPath);
Files.move(source, target);
importFileSuccess.add(fileInfo.getDataId() + FOLDER_NAME_SEPARATOR + fileInfo.getDataName());
}
else
{
importFileFail.add(fileInfo.getDataId() + FOLDER_NAME_SEPARATOR + fileInfo.getDataName());
}
}
} catch (IOException e) {
e.printStackTrace();
}
}
Map<String, List<String>> result = new HashMap<>();
result.put("success", importFileSuccess);
result.put("fail", importFileFail);
return result;
}

View File

@ -2,7 +2,9 @@ package com.electromagnetic.industry.software.data.manage.domain.boardservice.us
import com.electromagnetic.industry.software.data.manage.domain.boardservice.category.model.Category;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
public interface CategoryRepository {
@ -30,4 +32,37 @@ public interface CategoryRepository {
*/
List<Category> selectChildCategories(Category category);
/**
* 创建目录树节点数据
* @return
*/
Boolean createCategory(Category category);
/**
* 通过 节点 获取到顶级节点的树路径 从指定的节点数组中
* @param category
* @param categoryMap
* @return
*/
List<Category> getTopCategoriesPathList(Category category, Map<String, Category> categoryMap);
/**
* 通过 节点 获取父节点 从指定的节点数组中
* @param category
* @param categoryList
* @return
*/
Category getParentCategories(Category category, List<Category> categoryList);
/**
* 获通过 节点ID 获取父节点 从指定的节点数组中
* @param categoryId
* @param categoryList
* @return
*/
Category getParentCategories(String categoryId, List<Category> categoryList);
}

View File

@ -3,6 +3,7 @@ package com.electromagnetic.industry.software.data.manage.domain.boardservice.us
import com.electromagnetic.industry.software.data.manage.domain.boardservice.category.model.Category;
import java.util.List;
import java.util.Map;
public interface CategoryService {
@ -32,7 +33,23 @@ public interface CategoryService {
List<Category> selectChildCategories(Category category);
/**
* 获取 指定节点的 父节点 从所有节点数组中
* 通过 节点 获取到顶级节点的树路径 从指定的节点数组中
* @param category
* @param categoryMap
* @return
*/
List<Category> getTopCategoriesPathList(Category category, Map<String, Category> categoryMap);
/**
* 通过 节点 获取父节点 从指定的节点数组中
* @param category
* @param categoryList
* @return
*/
Category getParentCategories(Category category, List<Category> categoryList);
/**
* 通过 节点ID 获取父节点 从指定的节点数组中
* @param categoryId
* @param categoryList
* @return

View File

@ -10,8 +10,10 @@ import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
@Service
public class CategoryServiceImpl implements CategoryService {
@ -102,24 +104,38 @@ public class CategoryServiceImpl implements CategoryService {
public List<Category> selectChildCategories(Category category) { return categoryRepository.selectChildCategories(category); }
/**
* 获取 指定节点的 父节点 从所有节点数组中
* @param categoryId
* 通过 节点 获取到顶级节点的树路径 从指定的节点数组中
* @param category
* @param categoryMap
* @return
*/
public List<Category> getTopCategoriesPathList(Category category, Map<String, Category> categoryMap)
{
Category categoryParent = categoryMap.get(category.getParentId());
if(categoryParent != null)
{
List<Category> topCategoriesPathList = getTopCategoriesPathList(categoryParent, categoryMap);
if(topCategoriesPathList == null)
{
topCategoriesPathList = new ArrayList();
}
topCategoriesPathList.add(0, categoryParent);
return topCategoriesPathList;
}
return null;
}
/**
* 通过 节点 获取父节点 从指定的节点数组中
* @param category
* @param categoryList
* @return
*/
public Category getParentCategories(String categoryId, List<Category> categoryList)
public Category getParentCategories(Category category, List<Category> categoryList)
{
Category category = null;
Category categoryParent = null;
for(Category iter : categoryList)
{
if(category == null && iter.getCategoryId().equals(categoryId))
{
category = iter;
break;
}
}
for(Category iter : categoryList)
{
if(categoryParent == null && iter.getCategoryId().equals(category.getParentId()))
{
@ -127,8 +143,29 @@ public class CategoryServiceImpl implements CategoryService {
break;
}
}
return categoryParent;
}
/**
* 获通过 节点ID 获取父节点 从指定的节点数组中
* @param categoryId
* @param categoryList
* @return
*/
public Category getParentCategories(String categoryId, List<Category> categoryList)
{
Category category = null;
for(Category iter : categoryList)
{
if(category == null && iter.getCategoryId().equals(categoryId))
{
category = iter;
break;
}
}
return getParentCategories(category, categoryList);
}
}

View File

@ -75,6 +75,11 @@
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
</dependency>
<dependency>
<groupId>net.lingala.zip4j</groupId>
<artifactId>zip4j</artifactId>
<version>2.11.5</version>
</dependency>
</dependencies>
</project>

View File

@ -0,0 +1,149 @@
package electromagnetic.data.framework.share.util;
import cn.hutool.core.util.StrUtil;
import net.lingala.zip4j.ZipFile;
import net.lingala.zip4j.exception.ZipException;
import net.lingala.zip4j.model.ZipParameters;
import net.lingala.zip4j.model.enums.AesKeyStrength;
import net.lingala.zip4j.model.enums.CompressionLevel;
import net.lingala.zip4j.model.enums.CompressionMethod;
import net.lingala.zip4j.model.enums.EncryptionMethod;
import java.io.File;
import java.util.ArrayList;
import java.util.List;
/**
* @desc zip解压缩工具
* @author: wcy
* @date: 2024/1/12
* @version: 1.0
*/
public class EleZipUtil {
/**
* 压缩指定路径的文件
* @param srcFilePath 待压缩文件路径
* @param zipPathFileName zip文件全路径名
* @param password 加密密码
* @return
*/
public static boolean zipFile(String srcFilePath, String zipPathFileName, String password){
try {
// 生成的压缩文件
ZipFile zipFile = new ZipFile(zipPathFileName);
if (StrUtil.isNotEmpty(password)) {
zipFile.setPassword(password.toCharArray());
}
ZipParameters parameters = new ZipParameters();
// 压缩级别
parameters.setCompressionMethod(CompressionMethod.DEFLATE);
parameters.setCompressionLevel(CompressionLevel.NORMAL);
if(StrUtil.isNotEmpty(password)){
parameters.setEncryptFiles(true);
parameters.setEncryptionMethod(EncryptionMethod.AES);
parameters.setAesKeyStrength(AesKeyStrength.KEY_STRENGTH_256);
}
// 要打包的文件夹
File file = new File(srcFilePath);
if (file.isDirectory()) {
zipFile.addFolder(file, parameters);
} else {
zipFile.addFile(file, parameters);
}
return true;
} catch (ZipException e) {
e.printStackTrace();
return false;
}
}
/**
* @param zipFileFullName zip文件所在的路径名
* @param filePath 解压到的路径
* @param password 需要解压的密码
* @return
*/
public static boolean unZipFile(String zipFileFullName, String filePath, String password) {
try {
ZipFile zipFile = new ZipFile(zipFileFullName);
// 如果解压需要密码
if(StrUtil.isNotEmpty(password) && zipFile.isEncrypted()) {
zipFile.setPassword(password.toCharArray());
}
zipFile.extractAll(filePath);
return true;
} catch (ZipException e) {
e.printStackTrace();
return false;
}
}
/**
* 添加文件到压缩文件中
* @param zipFullFileName zip文件所在路径及全名
* @param fullFileNameList 待添加的文件全路径集合
* @param rootFolderInZip 在压缩文件里的文件夹名
* @return
*/
public static boolean addFilesToZip(String zipFullFileName, List<String> fullFileNameList, String rootFolderInZip) {
try {
ZipFile zipFile = new ZipFile(zipFullFileName);
ArrayList<File> addFiles = new ArrayList<>();
for (String fileName : fullFileNameList) {
addFiles.add(new File(fileName));
}
ZipParameters parameters = new ZipParameters();
parameters.setCompressionMethod(CompressionMethod.DEFLATE);
parameters.setCompressionLevel(CompressionLevel.NORMAL);
if(StrUtil.isNotEmpty(rootFolderInZip)){
if(!rootFolderInZip.endsWith("/")){
rootFolderInZip = rootFolderInZip+"/";
}
parameters.setRootFolderNameInZip(rootFolderInZip);
}
zipFile.addFiles(addFiles, parameters);
return true;
} catch (ZipException e) {
e.printStackTrace();
// log.error("添加文件失败:\n"+e.getMessage());
return false;
}
}
/**
* 从压缩文件中删除路径
* @param zipFullFileName
* @param fileName
* @return
*/
public static boolean deleteFileInZip(String zipFullFileName, String fileName) {
try {
ZipFile zipFile = new ZipFile(zipFullFileName);
zipFile.removeFile(fileName);
return true;
} catch (ZipException e) {
e.printStackTrace();
return false;
}
}
// public static void main(String[] args) throws ZipException {
// String srcFilePath = "G:/comac_desk_app";
// String desFilePath = "C:\\Users\\test\\Desktop\\project\\file";
// String zipPathFileName = "G:/tmp/comac_desk_app.zip";
// String password = "123456";
// long begin = System.currentTimeMillis();
// boolean zipResult = zipFile(srcFilePath, zipPathFileName, password);
// long end = System.currentTimeMillis();
// System.out.println("压缩结果:" + zipResult + ",耗时:" + (end - begin) + "ms");
//
// boolean unZipResult = unZipFile(zipPathFileName, desFilePath, password);
// long end2 = System.currentTimeMillis();
// System.out.println("解压结果:" + unZipResult + ",耗时:" + (end2 - end) + "ms");
// System.out.println(unZipResult);
// }
}

View File

@ -1,9 +1,16 @@
package com.electromagnetic.industry.software.data.manage.facade;
import com.electromagnetic.industry.software.data.manage.request.indicator.EDDataRequest;
import com.electromagnetic.industry.software.data.manage.request.indicator.FileChunkDTO;
import com.electromagnetic.industry.software.data.manage.request.indicator.FileChunkResultDTO;
import com.electromagnetic.industry.software.data.manage.request.indicator.FileDownloadDTO;
import com.electromagnetic.industry.software.data.manage.response.indicator.EDDataPageResponse;
import electromagnetic.data.framework.share.model.ElectromagneticResult;
import org.springframework.core.io.InputStreamResource;
import org.springframework.http.ResponseEntity;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;
import java.util.Map;
@ -43,27 +50,53 @@ public interface EDDataFacade {
* @param request
* @return
*/
ElectromagneticResult<Boolean> upload(EDDataRequest request);
ElectromagneticResult<Boolean> uploadFile(EDDataRequest request);
/**
* 下载
* @param request
*
* @param fileDownloadDTO
* @return
*/
ElectromagneticResult<Map<String, List<String>>> download(EDDataRequest request);
ResponseEntity<InputStreamResource> download(FileDownloadDTO fileDownloadDTO, HttpServletResponse response) throws IOException;
/**
* 导出
* @param request
* @return
*/
ElectromagneticResult<String> batchExport(EDDataRequest request);
ElectromagneticResult<Map<String, String>> batchExport(EDDataRequest request);
/**
* 导入
* @param request
* @param fileChunkDTO
* @return
*/
ElectromagneticResult<String> batchImport(EDDataRequest request);
ElectromagneticResult<?> batchImport(FileChunkDTO fileChunkDTO);
/**
* 获取已经上传的分片
* @param identifier
* @return
*/
ElectromagneticResult<List<Integer>> getUploadedChunkNums(String identifier);
/**
* 合并分片
* @param identifier
* @param fileName
* @param totalChunks
* @return
*/
ElectromagneticResult<?> mergeChunks(String identifier, String fileName, Integer totalChunks);
/**
* 检查分片是否存在
* @param fileChunkDTO
* @return
*/
ElectromagneticResult<FileChunkResultDTO> checkChunkExist(FileChunkDTO fileChunkDTO);
}

View File

@ -0,0 +1,18 @@
package com.electromagnetic.industry.software.data.manage.request.indicator;
import lombok.Data;
import org.springframework.web.multipart.MultipartFile;
@Data
public class FileChunkDTO {
private String identifier;
private long totalSize;
private MultipartFile file;
private String fileName;
private Integer chunkNumber;
private Long chunkSize;
private Long currentChunkSize;
private Integer totalChunks;
}

View File

@ -0,0 +1,15 @@
package com.electromagnetic.industry.software.data.manage.request.indicator;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import java.util.Set;
@Data
@AllArgsConstructor
@NoArgsConstructor
public class FileChunkResultDTO {
private Boolean skipUpload;
private Set<Integer> uploaded;
}

View File

@ -0,0 +1,8 @@
package com.electromagnetic.industry.software.data.manage.request.indicator;
import lombok.Data;
@Data
public class FileDownloadDTO {
private String dataId;
}

View File

@ -31,4 +31,11 @@ public interface CategoryMapper {
* @return
*/
List<Category> selectChildCategories(Category category);
/**
* 创建目录树节点数据
* @return
*/
Boolean createCategory(Category category);
}

View File

@ -6,7 +6,9 @@ import com.electromagnetic.industry.software.data.manage.repository.dao.Category
import org.springframework.stereotype.Repository;
import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
@Repository
public class CategoryRepositoryImpl implements CategoryRepository {
@ -44,4 +46,76 @@ public class CategoryRepositoryImpl implements CategoryRepository {
@Override
public List<Category> selectChildCategories(Category category) { return categoryMapper.selectChildCategories(category); }
/**
* 创建目录树节点数据
* @return
*/
@Override
public Boolean createCategory(Category category) { return categoryMapper.createCategory(category); }
/**
* 通过 节点 获取到顶级节点的树路径 从指定的节点数组中
* @param category
* @param categoryMap
* @return
*/
public List<Category> getTopCategoriesPathList(Category category, Map<String, Category> categoryMap)
{
Category categoryParent = categoryMap.get(category.getParentId());
if(categoryParent != null)
{
List<Category> topCategoriesPathList = getTopCategoriesPathList(categoryParent, categoryMap);
if(topCategoriesPathList == null)
{
topCategoriesPathList = new ArrayList();
}
topCategoriesPathList.add(0, categoryParent);
return topCategoriesPathList;
}
return null;
}
/**
* 通过 节点 获取父节点 从指定的节点数组中
* @param category
* @param categoryList
* @return
*/
public Category getParentCategories(Category category, List<Category> categoryList)
{
Category categoryParent = null;
for(Category iter : categoryList)
{
if(categoryParent == null && iter.getCategoryId().equals(category.getParentId()))
{
categoryParent = iter;
break;
}
}
return categoryParent;
}
/**
* 获通过 节点ID 获取父节点 从指定的节点数组中
* @param categoryId
* @param categoryList
* @return
*/
public Category getParentCategories(String categoryId, List<Category> categoryList)
{
Category category = null;
for(Category iter : categoryList)
{
if(category == null && iter.getCategoryId().equals(categoryId))
{
category = iter;
break;
}
}
return getParentCategories(category, categoryList);
}
}

View File

@ -71,18 +71,5 @@ public class EDDataRepositoryImpl implements EDDataRepository {
return edDataMapper.updateFileInfo(parames);
}
/**
* 下载
* @param parames
* @return
*/
@Override
public Boolean download(EDDataParams parames)
{
//return edDataMapper.download(parames);
return Boolean.TRUE;
}
}

View File

@ -36,13 +36,32 @@
<select id="selectCategories" resultMap="CategoryResultMap"
parameterType="com.electromagnetic.industry.software.data.manage.domain.boardservice.category.model.Category">
<include refid="selectUserVo" />
where category_id = categoryId and category_status="available"
where category_id = #{categoryId,jdbcType=VARCHAR} and category_status="available"
</select>
<select id="selectChildCategories" resultMap="CategoryResultMap"
parameterType="com.electromagnetic.industry.software.data.manage.domain.boardservice.category.model.Category">
<include refid="selectUserVo" />
where parent_id = parentId and category_status="available"
where parent_id = #{parentId,jdbcType=VARCHAR} and category_status="available"
</select>
<insert id="createCategory" parameterType="com.electromagnetic.industry.software.data.manage.domain.boardservice.category.model.Category">
<selectKey keyProperty="id" order="AFTER" resultType="java.lang.Long">
SELECT LAST_INSERT_ID()
</selectKey>
insert into ed_data_info (id,
category_type_id, parent_id, category_id,
category_name, category_status,
creator, creator_name, gmt_create,
modifier, modifier_name, gmt_modified,
effect_flag
)
values (#{id,jdbcType=VARCHAR}, #{categoryTypeId,jdbcType=VARCHAR},
#{parentId,jdbcType=VARCHAR}, #{categoryId,jdbcType=VARCHAR},
#{categoryName,jdbcType=VARCHAR}, #{categoryStatus,jdbcType=VARCHAR},
#{creator,jdbcType=VARCHAR}, #{creatorName,jdbcType=VARCHAR}, now(), #{modifier,jdbcType=VARCHAR},
#{modifierName,jdbcType=VARCHAR},now(),1
)
</insert>
</mapper>

View File

@ -255,6 +255,7 @@
</dependencyManagement>
<build>
<finalName>electromagnetic</finalName>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>

View File

@ -1,18 +1,21 @@
package com.electromagnetic.industry.software.data.manage.service.aop;
import com.alibaba.fastjson.JSON;
import electromagnetic.data.framework.share.exception.LoggerConstant;
import electromagnetic.data.framework.share.log.LogUtils;
import electromagnetic.data.framework.share.log.ServiceResultHandleUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.catalina.connector.ResponseFacade;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.Signature;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Component;
import org.springframework.util.StopWatch;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.multipart.MultipartFile;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import java.util.ArrayList;
import java.util.List;
/**
* service 通用日志切面
*
@ -21,33 +24,56 @@ import org.springframework.stereotype.Component;
*/
@Aspect
@Component
@Slf4j
public class ServiceAspect {
private static final Logger LOGGER = LoggerFactory.getLogger(LoggerConstant.DOMAIN_SERVICE);
/**
* 日志切面
*/
@Around("execution(* com.electromagnetic.industry.software.data.manage.facade..*.*(..)))")
public Object logAspect(ProceedingJoinPoint pjd) {
long startTime = System.currentTimeMillis();
Object[] args = pjd.getArgs();
Object arg = args.length < 1 ? "" : args[0];
Signature signature = pjd.getSignature();
String declaringTypeName = signature.getDeclaringTypeName();
String logPattern = declaringTypeName.substring(declaringTypeName.lastIndexOf(".") + 1) + "@" + signature.getName() + " request={0}, response={1}, cost={2}ms";
Object proceed = null;
try {
proceed = pjd.proceed(args);
} catch (Throwable t) {
LogUtils.error(t,LOGGER, "Executing " + logPattern+",Throwable:"+t,
arg == null ? "" : JSON.toJSONString(arg), JSON.toJSONString(proceed), System.currentTimeMillis() - startTime);
return ServiceResultHandleUtil.serviceError(t);
}
public Object process(ProceedingJoinPoint jp) throws Throwable {
String methodInfo = jp.getTarget().getClass().getSimpleName() + "."
+ jp.getSignature().getName();
Object[] args = jp.getArgs();
String paramInfo = "";
if (args != null && args.length > 0) {
if (args[0] != null && args[0].getClass() != ResponseFacade.class) {
try {
List<Object> list = new ArrayList<>();
for (Object obj : jp.getArgs()) {
// 正常返回日志
LogUtils.info(LOGGER, "End " + logPattern,
arg == null ? "" : JSON.toJSONString(arg), JSON.toJSONString(proceed), System.currentTimeMillis() - startTime);
return proceed;
if (obj instanceof ServletResponse) {
log.info("参数中有response");
} else if (obj instanceof ServletRequest) {
log.info("参数中有request");
} else if (obj instanceof MultipartFile) {
//文件不输出
MultipartFile obj1 = (MultipartFile) obj;
log.info("参数中文件;文件名:{},文件大小:{}", obj1.getName(), obj1.getSize());
break;
} else {
list.add(obj);
}
}
paramInfo = JSON.toJSONString(list);
} catch (Exception e) {
log.warn("切面异常", e.getMessage());
}
}
}
log.info("请求接口开始:{},参数:{}", methodInfo, paramInfo);
long startTime = System.currentTimeMillis();
StopWatch stopwatch = new StopWatch();
stopwatch.start("接口:" + methodInfo);
Object rvt = jp.proceed();
if (rvt instanceof ResponseEntity) {
return rvt;
}
String returnInfo = JSON.toJSONString(rvt);
log.info("请求接口结束:{},返回参数:{},接口耗时:{}", methodInfo, returnInfo, (System.currentTimeMillis() - startTime) + "毫秒");
stopwatch.stop();
log.debug(stopwatch.prettyPrint());
return rvt;
}
}

View File

@ -1,40 +1,62 @@
package com.electromagnetic.industry.software.data.manage.service.facade;
import cn.hutool.core.io.FileUtil;
import cn.hutool.core.io.IoUtil;
import cn.hutool.core.lang.Assert;
import cn.hutool.core.util.ZipUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.electromagnetic.industry.software.data.manage.domain.boardservice.category.model.Category;
import com.electromagnetic.industry.software.data.manage.domain.boardservice.indicator.model.EDDataInfo;
import com.electromagnetic.industry.software.data.manage.domain.boardservice.indicator.model.EDDataPage;
import com.electromagnetic.industry.software.data.manage.domain.boardservice.indicator.parames.EDDataParams;
import com.electromagnetic.industry.software.data.manage.domain.boardservice.indicator.service.EDDataService;
import com.electromagnetic.industry.software.data.manage.domain.boardservice.user.service.CategoryService;
import com.electromagnetic.industry.software.data.manage.facade.EDDataFacade;
import com.electromagnetic.industry.software.data.manage.request.indicator.EDDataRequest;
import com.electromagnetic.industry.software.data.manage.request.indicator.FileChunkDTO;
import com.electromagnetic.industry.software.data.manage.request.indicator.FileChunkResultDTO;
import com.electromagnetic.industry.software.data.manage.request.indicator.FileDownloadDTO;
import com.electromagnetic.industry.software.data.manage.response.indicator.EDDataPageResponse;
import com.electromagnetic.industry.software.data.manage.service.mappers.EDDataMappers;
import electromagnetic.data.framework.share.id.IdWorker;
import electromagnetic.data.framework.share.model.ElectromagneticResult;
import electromagnetic.data.framework.share.model.ElectromagneticResultUtil;
import org.springframework.http.HttpStatus;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.io.FileSystemResource;
import org.springframework.core.io.InputStreamResource;
import org.springframework.http.*;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;
import javax.annotation.Resource;
import java.io.File;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.io.IOException;
import java.util.*;
import java.util.stream.Collectors;
@Service
@Slf4j
public class EDDataFacadeImpl implements EDDataFacade {
@Resource
private EDDataService edDataService;
private CategoryService categoryService;
@Value("${data.windows.path}")
private String uploadFilePath;
@Value("${data.windows.path}")
private String windowsDir;
@Value("${data.file.cache.dir}")
private String fileCacheDir;
@Value("${data.file.storage.dir}")
private String fileStorageDir;
@Value("${data.upload.cache.dir}")
private String uploadCacheDir;
@Value("${data.export.cache.dir}")
private String exportCacheDir;
private static final String UPLOAD_FILE_CHUNK_SUFFIX = ".part";
/**
* 创建文件夹
@ -52,8 +74,6 @@ public class EDDataFacadeImpl implements EDDataFacade {
if(Boolean.TRUE) {
if (request.getUserId() == null || request.getUserId().isEmpty()) request.setUserId(IdWorker.getSnowFlakeIdString());
if (request.getUserName() == null || request.getUserName().isEmpty()) request.setUserName("user");
if (request.getName() == null || request.getName().isEmpty()) request.setName("testFolder");
//if (request.getParentId() == null || request.getParentId().isEmpty()) request.setParentId(IdWorker.getSnowFlakeIdString());
}
edDataInfo.setCreator(request.getUserId());
@ -70,51 +90,8 @@ public class EDDataFacadeImpl implements EDDataFacade {
edDataInfo.setVersion("1.0.0");
edDataInfo.setDataStatus("publish");
String parentFolderPath = "";
// 获取上级目录的名称
if(!request.getParentId().isEmpty()) {
EDDataParams parames = new EDDataParams();
parames.setDataId(request.getParentId());
EDDataInfo edDataInfoParent = edDataService.getDataInfo(parames);
if(edDataInfoParent == null) {
ElectromagneticResultUtil.fail(HttpStatus.BAD_REQUEST.toString(),"上级文件夹为空!");
}
String folderNameParent = edDataInfoParent.getDataName();
String folderIdParent = edDataInfoParent.getDataId();
// 记录当前目录的文件夹路径和文件夹id路径
JSONObject implantJsonObject = JSON.parseObject(edDataInfoParent.getImplantJson());
if(implantJsonObject == null) {
implantJsonObject = new JSONObject();
parentFolderPath = folderNameParent;
implantJsonObject.put("folderPath", parentFolderPath);
implantJsonObject.put("folderIdPath", folderIdParent);
}
else
{
String folderPath = implantJsonObject.getString("folderPath");
String folderIdPath = implantJsonObject.getString("folderIdPath");
parentFolderPath = folderPath + "/" + folderNameParent;
implantJsonObject.put("folderPath", parentFolderPath);
implantJsonObject.put("folderIdPath", folderIdPath + "/" + folderIdParent);
}
String newImplantJson = implantJsonObject.toJSONString();
edDataInfo.setImplantJson(newImplantJson);
}
boolean isSuccess = edDataService.createDataInfo(edDataInfo);
if(isSuccess)
{
// 获取文件存储的文件夹路径
String storageDirectory = "D:/fileTemp/" + parentFolderPath + "/" + edDataInfo.getDataName();
if(!edDataService.checkFolderIsExist(storageDirectory)) {
edDataService.createMultiLevelDirectory(storageDirectory);
}
}
return ElectromagneticResultUtil.success(Boolean.TRUE);
Boolean isSuccess = edDataService.createDataInfo(edDataInfo);
return ElectromagneticResultUtil.success(isSuccess);
}
@ -174,184 +151,52 @@ public class EDDataFacadeImpl implements EDDataFacade {
* @param request
* @return
*/
public ElectromagneticResult<Boolean> upload(EDDataRequest request)
public ElectromagneticResult<Boolean> uploadFile(EDDataRequest request)
{
EDDataParams parames= EDDataMappers.INSTANCE.getEDDataParames(request);
try {
// 获取目录编码ID
String categoryId = request.getParentId();
// 获取要上传的文件
MultipartFile fileInput = request.getFileData();
// 检查文件是否为空
if (fileInput == null || fileInput.isEmpty()) {
return ElectromagneticResultUtil.fail(HttpStatus.BAD_REQUEST.toString(),"上传的文件为空");
}
// 获取文件名
String fileFullName = fileInput.getOriginalFilename();
// 获取文件类型
String fileType = "";
// 获取文件名称
String fileName = "";
if (fileFullName.lastIndexOf(".") != -1 && fileFullName.lastIndexOf(".") != 0) {
fileType = fileFullName.substring(fileFullName.lastIndexOf(".") + 1);
fileName = fileFullName.substring(fileFullName.lastIndexOf("."));
}
// 获取文件存储的文件夹路径
String storageDirectory = "D:/fileTemp";
if(!edDataService.checkFolderIsExist(storageDirectory)) {
edDataService.createMultiLevelDirectory(storageDirectory);
}
String parentFolderPath = ""; //上级文件夹路径
String parentFolderIdPath = ""; //上级文件夹ID路径
String parentFolderName = ""; //上级文件夹名称
// 获取上级文件夹路径
{
EDDataParams folderParames = new EDDataParams();
folderParames.setDataId(categoryId);
EDDataInfo edDataInfoParent = edDataService.getDataInfo(folderParames);
if(edDataInfoParent == null) {
ElectromagneticResultUtil.fail(HttpStatus.BAD_REQUEST.toString(),"上级文件夹为空!");
}
JSONObject implantJsonObject = JSON.parseObject(edDataInfoParent.getImplantJson());
parentFolderPath = implantJsonObject.getString("folderPath");
parentFolderIdPath = implantJsonObject.getString("folderIdPath");
if(!parentFolderPath.isEmpty())
parentFolderPath += "/" ;
if(!parentFolderIdPath.isEmpty())
parentFolderIdPath += "/" ;
parentFolderPath += edDataInfoParent.getDataName();
parentFolderIdPath += edDataInfoParent.getDataId();
parentFolderName = edDataInfoParent.getDataName();
}
// 文件保存目录路径
String fileSavePath = storageDirectory + "/" + parentFolderPath;
String treeName = "目录树名称";
//String newFileName = treeName + "," + parentFolderName + "," + fileFullName;
String newFileName = fileFullName;
// 文件数据信息写到数据库
{
// 临时数据设置
if(Boolean.TRUE) {
if (request.getUserId() == null || request.getUserId().isEmpty()) request.setUserId(IdWorker.getSnowFlakeIdString());
if (request.getUserName() == null || request.getUserName().isEmpty()) request.setUserName("user");
//if (request.getName() == null || request.getName().isEmpty()) request.setName("testFolder");
if (request.getParentId() == null || request.getParentId().isEmpty()) request.setParentId(IdWorker.getSnowFlakeIdString());
}
// 创建新文件数据
EDDataInfo edDataInfo = new EDDataInfo();
edDataInfo.setCreator(request.getUserId());
edDataInfo.setCreatorName(request.getUserName());
edDataInfo.setModifier(request.getUserId());
edDataInfo.setModifierName(request.getUserName());
edDataInfo.setCategoryId(request.getParentId());
edDataInfo.setDataName(newFileName);
edDataInfo.setNote(request.getNote());
edDataInfo.setFileType(fileType);
edDataInfo.setGmtBatchUpload(request.getGmtBatchUpload());
edDataInfo.setDataId(IdWorker.getSnowFlakeIdString());
edDataInfo.setDataNo(edDataInfo.getDataId());
edDataInfo.setDataType("file");
edDataInfo.setVersion("1.0.0");
edDataInfo.setDataStatus("publish");
edDataInfo.setSaveStatus("saving");
JSONObject newImplantJsonObject = new JSONObject();
newImplantJsonObject.put("folderPath", parentFolderPath);
newImplantJsonObject.put("folderIdPath", parentFolderIdPath);
edDataInfo.setImplantJson(newImplantJsonObject.toJSONString());
boolean isSuccess = edDataService.createDataInfo(edDataInfo);
String userHome = System.getProperty("user.home");
File cacheDirectory = new File(userHome + "\\AppData\\Local\\Temp\\EDData\\Upload");
if (!cacheDirectory.exists()) {
cacheDirectory.mkdirs();
}
String cacheFolder = cacheDirectory.getAbsolutePath();
String cachePath = cacheFolder + "/" + newFileName;
System.out.println("文件缓存路径为: " + cacheDirectory.getAbsolutePath());
fileSavePath += "/" + newFileName;
// 这里可以添加将文件保存到本地磁盘或其他存储介质的逻辑
File saveFile = new File(cachePath);//fileSavePath
// 将文件保存到硬盘
fileInput.transferTo(saveFile);
EDDataParams fileParames = new EDDataParams();
fileParames.setDataId(edDataInfo.getDataId());
if(fileInput.getSize() == saveFile.length())
{
Path source = Paths.get(cachePath);
Path target = Paths.get(fileSavePath);
Files.move(source, target);
fileParames.setSaveStatus("success");
}
else
{
saveFile.delete();
fileParames.setSaveStatus("failure");
}
isSuccess = edDataService.updateFileInfo(fileParames);
}
// 如果文件上传成功返回成功消息
return ElectromagneticResultUtil.success(Boolean.TRUE);
} catch (Exception e) {
// 如果出现异常返回异常消息
return ElectromagneticResultUtil.fail(HttpStatus.INTERNAL_SERVER_ERROR.toString(),"文件上传失败" + e.getMessage());
}
Boolean isSuccess = edDataService.uploadFile(parames);
return ElectromagneticResultUtil.success(isSuccess);
}
/**
* 下载
* WISDOM_DOWNLOAD
* /data/ed/file/download
* @param request
*
* @param fileDownloadDTO
* @return
*/
public ElectromagneticResult<Map<String, List<String>>> download(EDDataRequest request)
public ResponseEntity<InputStreamResource> download(FileDownloadDTO fileDownloadDTO, HttpServletResponse response) throws IOException
{
//EDDataParams parames= EDDataMappers.INSTANCE.getEDDataParames(request);
// 获取文件存储的文件夹路径
String storageDirectory = "D:/fileTemp";
String osName = System.getProperty("os.name").toLowerCase();
String storageFilePath = osName.startsWith("win") ? uploadFilePath + fileStorageDir : fileStorageDir;
EDDataParams parames = new EDDataParams();
List<String> dataPathArr = new ArrayList();
for(String dataId : request.getDataIdArr())
{
parames.setDataId(dataId);
EDDataInfo edDataInfo = edDataService.getDataInfo(parames);
if(edDataInfo != null)
{
JSONObject implantJsonObject = JSON.parseObject(edDataInfo.getImplantJson());
String filePath = implantJsonObject.getString("folderPath");
filePath = storageDirectory + "/" + filePath + "/" + edDataInfo.getDataName();
dataPathArr.add(filePath);
}
}
String dataId = fileDownloadDTO.getDataId();
parames.setDataId(dataId);
EDDataInfo edDataInfo = edDataService.getDataInfo(parames);
Assert.isTrue(edDataInfo != null, "没有找到该下载文件");
JSONObject implantJsonObject = JSON.parseObject(edDataInfo.getImplantJson());
String filePath = storageFilePath + "/" + implantJsonObject.getString("folderPath") + "/" + edDataInfo.getDataName();
Map<String, List<String>> result = new HashMap<>();
result.put("urlArr", dataPathArr);
Assert.isTrue(FileUtil.exist(filePath), "下载文件不存在。");
File file = new File(filePath);
FileSystemResource fileSystemResource = new FileSystemResource(file);
HttpHeaders headers = new HttpHeaders();
headers.add("Cache-Control", "no-cache, no-store, must-revalidate");
headers.add("Content-Disposition", String.format("attachment; filename=\"%s\"", fileSystemResource.getFilename()));
headers.add("Pragma", "no-cache");
headers.add("Expires", "0");
// 构建响应实体(可以返回<byte[]或Resource返回类型取决body入参类型)
return ResponseEntity
.ok()
.headers(headers)
.contentLength(fileSystemResource.contentLength())
.contentType(MediaType.parseMediaType("application/octet-stream"))
.body(new InputStreamResource(fileSystemResource.getInputStream()));
return ElectromagneticResultUtil.success(result);
}
/**
@ -360,94 +205,149 @@ public class EDDataFacadeImpl implements EDDataFacade {
* @return
*/
@Override
public ElectromagneticResult<String> batchExport(EDDataRequest request) {
public ElectromagneticResult<Map<String, String>> batchExport(EDDataRequest request) {
//1根据用户选择层级树编码查出所有文件和文件夹list
//2循环list将每个文件复制到新建目录并进行重命名命名规则目录树编码+,+文件夹编码有则填写无则为空+,+文件编码
//3打包新建为zip并根据生产下载地址域名+文件路径+文件
//4返回前端下载的地址
// **********在导出的过程中可能会出现有用户上传文件的情况
// 获取目录编码ID
String categoryId = request.getParentId();
// 获取缓存文件夹的绝对路径
String userHome = System.getProperty("user.home");
File cacheDirectory = new File(userHome + "\\AppData\\Local\\Temp\\EDData\\Export");
if (!cacheDirectory.exists()) {
cacheDirectory.mkdirs();
}
String cacheFolder = cacheDirectory.getAbsolutePath(); // 缓存文件夹的绝对路径
List<Category> categoryList = categoryService.selectAllCategories();
{
// 遍历客户端上传的
EDDataParams folderParames = new EDDataParams();
for (String dataId : request.getDataIdArr()) {
Category category = new Category();
category.setCategoryId(dataId);
categoryService.selectCategories(category);
//folderParames.setDataId(dataId);
//EDDataInfo edDataInfo = edDataService.getDataInfo(folderParames);
//if(edDataInfo != null)
//{
// JSONObject implantJsonObject = JSON.parseObject(edDataInfo.getImplantJson());
// String filePath = implantJsonObject.getString("folderPath");
// filePath = storageDirectory + "/" + filePath + "/" + edDataInfo.getDataName();
// dataPathArr.add(filePath);
//}
}
}
String parentFolderPath = ""; //上级文件夹路径
String parentFolderIdPath = ""; //上级文件夹ID路径
String parentFolderName = ""; //上级文件夹名称
// 获取上级文件夹路径
{
EDDataParams folderParames = new EDDataParams();
folderParames.setDataId(categoryId);
EDDataInfo edDataInfoParent = edDataService.getDataInfo(folderParames);
if(edDataInfoParent == null) {
ElectromagneticResultUtil.fail(HttpStatus.BAD_REQUEST.toString(),"上级文件夹为空!");
}
JSONObject implantJsonObject = JSON.parseObject(edDataInfoParent.getImplantJson());
parentFolderPath = implantJsonObject.getString("folderPath");
parentFolderIdPath = implantJsonObject.getString("folderIdPath");
if(!parentFolderPath.isEmpty())
parentFolderPath += "/" ;
if(!parentFolderIdPath.isEmpty())
parentFolderIdPath += "/" ;
parentFolderPath += edDataInfoParent.getDataName();
parentFolderIdPath += edDataInfoParent.getDataId();
parentFolderName = edDataInfoParent.getDataName();
}
return null;
EDDataParams parames= EDDataMappers.INSTANCE.getEDDataParames(request);
String fileUrl = edDataService.exportData(parames);
Map<String, String> result = new HashMap<>();
result.put("url", fileUrl);
return ElectromagneticResultUtil.success(result);
}
/**
* 导入
* @param request
* @param fileChunkDTO
* @return
*/
@Override
public ElectromagneticResult<String> batchImport(EDDataRequest request) {
//1上传文件到指定目录并重命名
//2接下压缩包
//3扫码解压文件夹的所有文件
//4循环处理文件读取文件名称根据名称规则进行数据库查询匹配层级树文件夹和文件是否跳过
//5如果不存在则新建文件记录
//6并移动文件到上传目录
//7处理完成返回成功
return null;
public ElectromagneticResult<?> batchImport(FileChunkDTO fileChunkDTO) {
String identifier = fileChunkDTO.getIdentifier();
String fileName = fileChunkDTO.getFileName();
// 首先检查文件是否存在如果存在则不允许重复上传
String destZipPath = uploadFilePath + identifier + File.separator + fileName;
boolean existFile = FileUtil.exist(new File(destZipPath));
if (existFile) {
return ElectromagneticResultUtil.fail("-1", "文件已经存在,请勿重复上传。");
}
// 检查该分片有没被上传过
String destChunkPath = uploadFilePath + identifier + File.separator + fileChunkDTO.getChunkNumber() + UPLOAD_FILE_CHUNK_SUFFIX;
boolean existChunk = FileUtil.exist(new File(destChunkPath));
if (existChunk) {
return ElectromagneticResultUtil.success(true);
}
File dir = new File(uploadFilePath + identifier + File.separator);
if (!dir.exists()) {
dir.mkdir();
}
try (
InputStream inputStream = fileChunkDTO.getFile().getInputStream();
FileOutputStream fileOutputStream = new FileOutputStream(destChunkPath);
) {
IoUtil.copy(inputStream, fileOutputStream);
} catch (IOException ioException) {
log.error("上传文件错误...", ioException);
}
return ElectromagneticResultUtil.success(fileChunkDTO.getIdentifier());
}
@Override
public ElectromagneticResult<List<Integer>> getUploadedChunkNums(String identifier) {
return ElectromagneticResultUtil.success(getUploadedChunks(identifier));
}
private List<Integer> getUploadedChunks(String identifier) {
String destPath = uploadFilePath + identifier;
if (!FileUtil.exist(new File(destPath))) {
return new ArrayList<>();
}
List<Integer> uploadedFileChunkNums = FileUtil.listFileNames(destPath)
.stream()
.filter(e -> !e.endsWith(".zip"))
.map(e -> e.replace(UPLOAD_FILE_CHUNK_SUFFIX, ""))
.map(Integer::parseInt)
.collect(Collectors.toList());
return uploadedFileChunkNums;
}
// TODO 需要验证如果一个分片上传一半网络断开则该分片的存储情况
@Override
public ElectromagneticResult<?> mergeChunks(String identifier, String fileName, Integer totalChunks) {
// 检查所有分片是否已经上传完成分片编号从1开始
for (int i = 1; i <= totalChunks; i++) {
String tmpPath = uploadFilePath + identifier + File.separator + i + UPLOAD_FILE_CHUNK_SUFFIX;
if (!FileUtil.exist(new File(tmpPath))) {
log.error("第{}个分片没有上传完成,请上传完成后再合并。", i);
ElectromagneticResultUtil.fail("-1", "文件尚未上传完成。");
}
}
// 合并分片
String destZipPath = uploadFilePath + identifier + File.separator + fileName;
File mergedFile = new File(destZipPath);
try {
RandomAccessFile targetFile = new RandomAccessFile(mergedFile, "rw");
byte[] buffer = new byte[1024];
for (int i = 1; i <= totalChunks; i++) {
String tmpPath = uploadFilePath + identifier + File.separator + i + UPLOAD_FILE_CHUNK_SUFFIX;
RandomAccessFile tmp = new RandomAccessFile(new File(tmpPath), "r");
int len;
while ((len = tmp.read(buffer)) != -1) {
targetFile.write(buffer, 0, len);
}
tmp.close();
}
targetFile.close();
} catch (IOException ioException) {
ElectromagneticResultUtil.fail("-1", "文件合并失败");
}
// 删除分片
for (int i = 1; i <= totalChunks; i++) {
String tmpPath = uploadFilePath + identifier + File.separator + i + UPLOAD_FILE_CHUNK_SUFFIX;
FileUtil.del(tmpPath);
}
// 检验文件的MD5值
// 解压文件
ZipUtil.unzip(destZipPath, uploadFilePath + identifier + File.separator);
// 文件信息存入数据库
Map<String, List<String>> result = edDataService.importData(destZipPath);
return ElectromagneticResultUtil.success(result);
}
@Override
public ElectromagneticResult<FileChunkResultDTO> checkChunkExist(FileChunkDTO fileChunkDTO) {
FileChunkResultDTO res = new FileChunkResultDTO();
// 首先判断zip文件是否存在如果不存在则表示还没有上传完成
String identifier = fileChunkDTO.getIdentifier();
String fileName = fileChunkDTO.getFileName();
String destZipPath = uploadFilePath + identifier + File.separator + fileName;
boolean existFile = FileUtil.exist(new File(destZipPath));
if (existFile) {
return ElectromagneticResultUtil.success(new FileChunkResultDTO(true, new HashSet<>()));
}
List<Integer> uploadedChunks = getUploadedChunks(identifier);
return ElectromagneticResultUtil.success(new FileChunkResultDTO(false, new HashSet<>(uploadedChunks)));
}
}