1.修改目录结构相关的接口代码逻辑。

This commit is contained in:
sxlong 2024-11-26 18:02:12 +08:00
parent c91f2cc8a9
commit 9709d6a691
12 changed files with 449 additions and 381 deletions

View File

@ -1,28 +0,0 @@
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 electromagnetic.data.framework.share.model.ElectromagneticResult;
import electromagnetic.data.framework.share.model.ElectromagneticResultUtil;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import javax.annotation.Resource;
import java.util.Date;
@RequestMapping("/data/ed/database")
@RestController
public class EDDataBaseController {
@Resource
private EDDataFacade edDataFacade;
@ApiOperation(value = "数据导出",notes = "")
@RequestMapping(value = "/export",method = RequestMethod.POST)
public ElectromagneticResult<?> export(@RequestBody EDDataRequest request){
return edDataFacade.batchExport(request);
}
}

View File

@ -62,7 +62,7 @@ public class EDDataController {
request.setParentId(parentId); request.setParentId(parentId);
request.setFileData(file); request.setFileData(file);
request.setGmtBatchUpload(new Date(gmtBatchUpload)); request.setGmtBatchUpload(new Date(gmtBatchUpload));
return edDataFacade.upload(request); return edDataFacade.uploadFile(request);
} }
@ -79,6 +79,12 @@ public class EDDataController {
return ElectromagneticResultUtil.success("复制"); 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 = "") @ApiOperation(value = "获取已经上传的分片",notes = "")
@RequestMapping(value = "/getUploadedChunkNums",method = RequestMethod.GET) @RequestMapping(value = "/getUploadedChunkNums",method = RequestMethod.GET)
public ElectromagneticResult<?> getUploadedChunkNums(@RequestParam String identifier) { public ElectromagneticResult<?> getUploadedChunkNums(@RequestParam String identifier) {

View File

@ -30,8 +30,12 @@ server.port=8888
zip.password=~~BYE6cWGBJJjsU9DczYtZIQ== zip.password=~~BYE6cWGBJJjsU9DczYtZIQ==
#windows文件存储目录 #windows文件存储目录
upload.file.dest.path=E:/comacFileStore/ data.windows.path=E:/comacFileStorage/
#文件缓存路径 #文件缓存路径
upload.file.cache.dir=/szsd/cache/ data.file.cache.dir=/szsd/cache/
#文件存储路径 #文件存储路径
upload.file.store.dir=/szsd/fileStore/ 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; private Integer pageSize = 10;
/**
* 操作用户id
*/
private String userId;
/**
* 用户名
*/
private String userName;
/** /**
* 上级文件夹数据码,为空是顶级 * 上级文件夹数据码,为空是顶级
*/ */

View File

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

View File

@ -31,6 +31,7 @@ import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value; import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.HttpStatus; import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;
import javax.annotation.Resource; import javax.annotation.Resource;
import java.io.*; import java.io.*;
@ -56,12 +57,48 @@ public class EDDataServiceImpl implements EDDataService {
@Value("${zip.password}") @Value("${zip.password}")
private String passwordZIP; private String passwordZIP;
@Value("${upload.file.dest.path}") @Value("${data.windows.path}")
private String uploadFilePath; private String windowsDir;
@Value("${upload.file.cache.dir}") @Value("${data.file.cache.dir}")
private String fileCacheDir; private String fileCacheDir;
@Value("${upload.file.store.dir}") @Value("${data.file.storage.dir}")
private String fileStoreDir; 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;
}
/** /**
* 创建文件/文件夹数据信息 * 创建文件/文件夹数据信息
@ -83,11 +120,9 @@ public class EDDataServiceImpl implements EDDataService {
// 获取新文件夹路径信息 // 获取新文件夹路径信息
categoryParent = categoryParentList.get(0); categoryParent = categoryParentList.get(0);
String osName = System.getProperty("os.name").toLowerCase(); String dataStoragePath = getDataStoragePath();
String storageDirectory = osName.startsWith("win") ? uploadFilePath + fileStoreDir : fileStoreDir; String folderParent = categoryParent.getCategoryId() + FOLDER_NAME_SEPARATOR + categoryParent.getCategoryName() ;
String folderNew = dataStoragePath + File.separator + folderParent + File.separator + edDataInfo.getDataName();
String folderParent = categoryParent.getCategoryId() + "_" + categoryParent.getCategoryName() ;
String folderNew = storageDirectory + "/" + folderParent + "/" + edDataInfo.getDataName();
// 判断文件夹名称是否存在 // 判断文件夹名称是否存在
EDDataParams folderParames = new EDDataParams(); EDDataParams folderParames = new EDDataParams();
@ -144,7 +179,33 @@ public class EDDataServiceImpl implements EDDataService {
*/ */
public Boolean updateFileInfo(EDDataParams parames) 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); edDataRepository.updateFileInfo(parames);
return Boolean.TRUE; return Boolean.TRUE;
} }
@ -174,41 +235,210 @@ 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 * @param parames
* @return * @return
*/ */
public Boolean download(EDDataParams parames) public Boolean uploadFile(EDDataParams parames)
{ {
edDataRepository.download(parames);
return Boolean.TRUE;
}
/** // 获取目录编码ID
* 检查文件夹是否存在 String categoryId = parames.getParentId();
* @param path
* @return
*/
public Boolean checkFolderIsExist(String path)
{
return Files.exists(Paths.get(path));
}
/** // 获取要上传的文件
* 创建多级目录 MultipartFile fileInput = parames.getFileData();
* @param path
* @return // 检查文件是否为空
*/ if (fileInput == null || fileInput.isEmpty()) {
public Boolean createMultiLevelDirectory(String path)
{
try {
Files.createDirectories(Paths.get(path));
return Boolean.TRUE;
} catch (IOException e) {
return Boolean.FALSE; 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;
}
/** /**
* 导出数据 * 导出数据
@ -221,116 +451,85 @@ public class EDDataServiceImpl implements EDDataService {
// 获取文件夹编码ID数组 // 获取文件夹编码ID数组
List<String> categoryIdInputList = Arrays.asList(parames.getDataIdArr()); List<String> categoryIdInputList = Arrays.asList(parames.getDataIdArr());
String osName = System.getProperty("os.name").toLowerCase(); String dataCachePath = getDataCachePath();
fileCacheDir = osName.startsWith("win") ? uploadFilePath + fileCacheDir : fileCacheDir; // 导出数据时的临时存放目录
String exportDataCachePath = dataCachePath + exportCacheDir + FOLDER_NAME_SEPARATOR + getTimeStampString();
String exportDataDirectory = ""; if (!FileUtil.exist(exportDataCachePath)){
FileUtil.mkdir(exportDataCachePath);
{
exportDataDirectory = fileCacheDir + "/ExportData";
File cacheDirectory = new File(exportDataDirectory);
if (!cacheDirectory.exists()) {
cacheDirectory.mkdirs();
}
} }
List<Category> categoryAllList = categoryRepository.getAllCategories(); List<Category> categoryAllList = categoryRepository.getAllCategories();
Map<String, Category> categoryAllMap = new HashMap(); //所有目录数组map Map<String, Category> categoryAllMap = new HashMap(); //所有目录数组map
List<Category> categoryInputList = new ArrayList(); //客户端输入的目录数组map for (Category category : categoryAllList) {
Map<String, Category> categoryInputMap = new HashMap(); //客户端输入的目录数组map
for (Category category : categoryAllList)
{
categoryAllMap.put(category.getCategoryId(), category); categoryAllMap.put(category.getCategoryId(), category);
} }
for (String categoryId : categoryIdInputList)
{ 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); Category category = categoryAllMap.get(categoryId);
categoryInputList.add(category); if(category != null) {
categoryInputMap.put(categoryId, category); categoryInputList.add(category);
} categoryInputMap.put(categoryId, category);
} else {
EDDataInfo folder = fileAllMap.get(categoryId);
List<Category> categoryHighestList = new ArrayList(); if(folder != null) {
List<Category> highestToTopCategoriesPathList = new ArrayList(); //输入的最高级目录到 所有目录的顶级目录的 路径目录数组 fileInputList.add(folder);
fileInputMap.put(categoryId, folder);
// 从客户端上传的输入目录数组中 找到最高级(不是最顶级)目录树节点
{
Category categoryHighestParent = null;
Category categoryHighestParentTemp = null;
String categoryIdParent = "";
for (Category category : categoryInputList)
{
// 从输入目录数组找到最高级父节点目录
categoryHighestParentTemp = category;
do{
categoryIdParent = categoryHighestParentTemp.getParentId();
categoryHighestParent = categoryHighestParentTemp;
categoryHighestParentTemp = categoryInputMap.get(categoryIdParent);
}
while(categoryHighestParentTemp != null);
categoryHighestList.add(categoryHighestParent);
}
for (Category category : categoryHighestList) {
List<Category> topCategoriesPathList = categoryRepository.getTopCategoriesPathList(category, categoryAllMap);
for (Category categoryPath : topCategoriesPathList) {
if(!highestToTopCategoriesPathList.contains(categoryPath)) {
highestToTopCategoriesPathList.add(categoryPath);
}
} }
} }
} }
// 导出目录树数据为json文件 // 导出目录树数据为json文件
{ {
List<Category> categoryExportList = new ArrayList(); //需要导出的目录数据数组
categoryExportList.addAll(highestToTopCategoriesPathList);
for (Category category : categoryInputList) {
if(!categoryExportList.contains(category)) {
categoryExportList.add(category);
}
}
// 创建ObjectMapper对象用于JSON处理 // 创建ObjectMapper对象用于JSON处理
ObjectMapper objectMapper = new ObjectMapper(); ObjectMapper objectMapper = new ObjectMapper();
// 将List<Person>转换为JSON字符串 // 将List<Category>转换为JSON字符串
String jsonString = "";
try { try {
jsonString = objectMapper.writeValueAsString(categoryExportList); 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) { } catch (JsonProcessingException e) {
e.printStackTrace(); e.printStackTrace();
} }
catch (IOException e) {
// 将JSON字符串写入文件
File jsonFile = new File(exportDataDirectory + "/categoryList.json");
try {
objectMapper.writeValue(jsonFile, jsonString);
} catch (IOException e) {
e.printStackTrace(); e.printStackTrace();
} }
System.out.println("目录树数据已成功导出为JSON文件。" + jsonFile.getAbsolutePath());
} }
// 导出文件信息数据和文件数据 // 导出文件信息数据和文件数据
{ {
// 保存文件信息数据为json文件 // 保存文件信息数据为json文件
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<EDDataInfo> fileExportList = new ArrayList(); //需要导出的文件信息数据数组 List<EDDataInfo> fileExportList = new ArrayList(); //需要导出的文件信息数据数组
for (EDDataInfo fileFinfo : fileAllList) for (EDDataInfo fileFinfo : fileAllList) {
{ if(fileFinfo.getDataType().equals("folder")) {
if(categoryInputMap.get(fileFinfo.getCategoryId()) != null) if(categoryInputMap.get(fileFinfo.getCategoryId()) != null) {
{ fileExportList.add(fileFinfo);
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);
}
} }
} }
@ -338,40 +537,42 @@ public class EDDataServiceImpl implements EDDataService {
// 创建ObjectMapper对象用于JSON处理 // 创建ObjectMapper对象用于JSON处理
ObjectMapper objectMapper = new ObjectMapper(); ObjectMapper objectMapper = new ObjectMapper();
// 将List<Person>转换为JSON字符串 // 将List<EDDataInfo>转换为JSON字符串
String jsonString = "";
try { try {
jsonString = objectMapper.writeValueAsString(fileExportList); 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) { } catch (JsonProcessingException e) {
e.printStackTrace(); e.printStackTrace();
}
// 将JSON字符串写入文件
File jsonFile = new File(exportDataDirectory + "/fileInfoList.json");
try {
objectMapper.writeValue(jsonFile, jsonString);
} catch (IOException e) { } catch (IOException e) {
e.printStackTrace(); e.printStackTrace();
} }
System.out.println("目录树数据已成功导出为JSON文件。" + jsonFile.getAbsolutePath());
// 将文件 复制到 数据导出的缓存目录中 // 将文件 复制到 数据导出的缓存目录中
//String storageDirectory = osName.startsWith("win") ? uploadFilePath + fileStoreDir : fileStoreDir; String dataStoragePath = getDataStoragePath();
//error Path source = Paths.get(storageDirectory + "/testFolder3"); String needExportfolder = "";
//Path target = Paths.get(exportDataDirectory); for (Category category : categoryInputList)
//FileUtil.copy(source, target); // StandardCopyOption.REPLACE_EXISTING {
//System.out.println("文件数据已成功复制到目标目录。"); 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 = fileCacheDir + "/comac_exportData1.comac"; String zipPathFileName = dataCachePath + "/comac_exportData1.comac";
String enCodeZipPathFileName = fileCacheDir + "/comac_exportData.comac"; String enCodeZipPathFileName = dataCachePath + "/comac_exportData.comac";
// EleZipUtil.zipFile(exportDataDirectory, zipPathFileName, passwordZIP); // EleZipUtil.zipFile(exportDataDirectory, zipPathFileName, passwordZIP);
ZipUtil.zip(exportDataDirectory, zipPathFileName); ZipUtil.zip(exportDataCachePath, zipPathFileName);
String password = "adknfhkj87654knd"; String password = "adknfhkj87654knd";
AES aes = SecureUtil.aes(password.getBytes()); // aesKey是加密密钥 AES aes = SecureUtil.aes(password.getBytes()); // aesKey是加密密钥
try( try(
@ -383,13 +584,15 @@ public class EDDataServiceImpl implements EDDataService {
log.error("文件加密错误..", e); log.error("文件加密错误..", e);
} }
System.out.println("目录树数据+文件数据已成功复制到目标目录。" + zipPathFileName); //FileUtil.del(zipPathFileName);//删除临时目录
log.info("目录树数据+文件数据已成功复制到目标目录。" + zipPathFileName);
} }
// 删除 导出数据的 缓存目录 // 删除 导出数据的 缓存目录
FileUtil.del(exportDataDirectory); FileUtil.del(exportDataCachePath);
return fileCacheDir + "/comac_exportData.comac"; return dataCachePath + "/comac_exportData.comac";
} }
/** /**
@ -397,7 +600,7 @@ public class EDDataServiceImpl implements EDDataService {
* @param folderPath 解压后的数据目录 * @param folderPath 解压后的数据目录
* @return * @return
*/ */
public Boolean importData(String folderPath) public Map<String, List<String>> importData(String folderPath)
{ {
// 获取所有目录树节点数据 // 获取所有目录树节点数据
List<Category> categoryAllList = categoryRepository.getAllCategories(); List<Category> categoryAllList = categoryRepository.getAllCategories();
@ -432,15 +635,14 @@ public class EDDataServiceImpl implements EDDataService {
// 将JSON数据从文件中读取并反序列化为List<Person> // 将JSON数据从文件中读取并反序列化为List<Person>
categoryImportList = objectMapper.readValue(jsonFile, objectMapper.getTypeFactory().constructCollectionType(List.class, Category.class)); categoryImportList = objectMapper.readValue(jsonFile, objectMapper.getTypeFactory().constructCollectionType(List.class, Category.class));
if (categoryImportList == null) { if (categoryImportList == null) {
System.out.println("读取并反序列化JSON文件数据为List<Category>失败!"); log.error("读取并反序列化JSON文件数据为List<Category>失败!");
return Boolean.FALSE; return null;
} }
} catch (IOException e) { } catch (IOException e) {
e.printStackTrace(); e.printStackTrace();
} }
// 将目录树数据写入到数据库中 // 将目录树数据写入到数据库中
for (Category category : categoryImportList) { for (Category category : categoryImportList) {
if (categoryAllMap.get(category.getCategoryId()) == null) { if (categoryAllMap.get(category.getCategoryId()) == null) {
categoryRepository.createCategory(category); categoryRepository.createCategory(category);
@ -463,15 +665,14 @@ public class EDDataServiceImpl implements EDDataService {
// 将JSON数据从文件中读取并反序列化为List<Person> // 将JSON数据从文件中读取并反序列化为List<Person>
fileInfoImportList = objectMapper.readValue(jsonFile, objectMapper.getTypeFactory().constructCollectionType(List.class, Category.class)); fileInfoImportList = objectMapper.readValue(jsonFile, objectMapper.getTypeFactory().constructCollectionType(List.class, Category.class));
if (fileInfoImportList == null) { if (fileInfoImportList == null) {
log.info("读取并反序列化JSON文件数据为List<EDDataInfo>失败!"); log.error("读取并反序列化JSON文件数据为List<EDDataInfo>失败!");
return Boolean.FALSE; return null;
} }
} catch (IOException e) { } catch (IOException e) {
e.printStackTrace(); e.printStackTrace();
} }
// 将文件信息数据写入到数据库中 // 将文件信息数据写入到数据库中
for (EDDataInfo fileInfo : fileInfoImportList) { for (EDDataInfo fileInfo : fileInfoImportList) {
if (fileInfoAllMap.get(fileInfo.getDataId()) == null) { if (fileInfoAllMap.get(fileInfo.getDataId()) == null) {
edDataRepository.createDataInfo(fileInfo); edDataRepository.createDataInfo(fileInfo);
@ -480,22 +681,51 @@ public class EDDataServiceImpl implements EDDataService {
} }
// 将解压后的文件夹和文件 移动到 上传文件的存储目录中 // 将解压后的文件夹和文件 移动到 上传文件的存储目录中
List<String> importFileSuccess = new ArrayList();
List<String> importFileFail = new ArrayList();
{ {
String osName = System.getProperty("os.name").toLowerCase(); String dataStoragePath = getDataStoragePath();
String storageFilePath = osName.startsWith("win") ? uploadFilePath + fileStoreDir : fileStoreDir; if (!FileUtil.exist(dataStoragePath)){
String cacheFilePath = folderPath; FileUtil.mkdir(dataStoragePath);
}
String importDataCachePath = folderPath; //导入数据时 数据文件解压后的目录
String importFileCachePath = ""; //需要导入的文件的缓存路径
String importFileCacheFullPath = ""; //需要导入的文件的全路径
String fileStorageFolder = ""; //文件存储的文件夹
String fileStoragePath = ""; //文件存储的文件夹路径
String fileStorageFullPath = ""; //文件存储的文件全路径
try { try {
for (EDDataInfo fileInfo : fileInfoImportList) { for (EDDataInfo fileInfo : fileInfoImportList) {
if (fileInfoAllMap.get(fileInfo.getDataId()) == null) { JSONObject newImplantJsonObject = JSONObject.parseObject(fileInfo.getImplantJson());
if (!FileUtil.exist(storageFilePath)){ fileStorageFolder = newImplantJsonObject.getString("folderPath");
FileUtil.mkdir(storageFilePath); 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(cacheFilePath); Path source = Paths.get(importFileCacheFullPath);
Path target = Paths.get(storageFilePath); Path target = Paths.get(fileStorageFullPath);
Files.move(source, target); 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) { } catch (IOException e) {
@ -504,7 +734,11 @@ public class EDDataServiceImpl implements EDDataService {
} }
return Boolean.TRUE; Map<String, List<String>> result = new HashMap<>();
result.put("success", importFileSuccess);
result.put("fail", importFileFail);
return result;
} }

View File

@ -50,7 +50,7 @@ public interface EDDataFacade {
* @param request * @param request
* @return * @return
*/ */
ElectromagneticResult<Boolean> upload(EDDataRequest request); ElectromagneticResult<Boolean> uploadFile(EDDataRequest request);
/** /**
* 下载 * 下载

View File

@ -4,5 +4,5 @@ import lombok.Data;
@Data @Data
public class FileDownloadDTO { public class FileDownloadDTO {
private String dataIdArr; private String dataId;
} }

View File

@ -73,18 +73,5 @@ public class EDDataRepositoryImpl implements EDDataRepository {
return edDataMapper.updateFileInfo(parames); 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,13 @@
<select id="selectCategories" resultMap="CategoryResultMap" <select id="selectCategories" resultMap="CategoryResultMap"
parameterType="com.electromagnetic.industry.software.data.manage.domain.boardservice.category.model.Category"> parameterType="com.electromagnetic.industry.software.data.manage.domain.boardservice.category.model.Category">
<include refid="selectUserVo" /> <include refid="selectUserVo" />
where category_id = categoryId and category_status="available" where category_id = #{categoryId,jdbcType=VARCHAR} and category_status="available"
</select> </select>
<select id="selectChildCategories" resultMap="CategoryResultMap" <select id="selectChildCategories" resultMap="CategoryResultMap"
parameterType="com.electromagnetic.industry.software.data.manage.domain.boardservice.category.model.Category"> parameterType="com.electromagnetic.industry.software.data.manage.domain.boardservice.category.model.Category">
<include refid="selectUserVo" /> <include refid="selectUserVo" />
where parent_id = parentId and category_status="available" where parent_id = #{parentId,jdbcType=VARCHAR} and category_status="available"
</select> </select>
<insert id="createCategory" parameterType="com.electromagnetic.industry.software.data.manage.domain.boardservice.category.model.Category"> <insert id="createCategory" parameterType="com.electromagnetic.industry.software.data.manage.domain.boardservice.category.model.Category">

View File

@ -1,4 +1,4 @@
package com.electromagnetic.industry.software.data.manage.service.facade; package com.electromagnetic.industry.software.data.manage.service.facade;
import cn.hutool.core.io.FileUtil; import cn.hutool.core.io.FileUtil;
import cn.hutool.core.io.IoUtil; import cn.hutool.core.io.IoUtil;
@ -27,13 +27,11 @@ import org.springframework.core.io.FileSystemResource;
import org.springframework.core.io.InputStreamResource; import org.springframework.core.io.InputStreamResource;
import org.springframework.http.*; import org.springframework.http.*;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;
import javax.annotation.Resource; import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpServletResponse;
import java.io.*; import java.io.*;
import java.io.IOException; import java.io.IOException;
import java.nio.file.*;
import java.util.*; import java.util.*;
import java.util.stream.Collectors; import java.util.stream.Collectors;
@ -44,16 +42,18 @@ public class EDDataFacadeImpl implements EDDataFacade {
@Resource @Resource
private EDDataService edDataService; private EDDataService edDataService;
@Value("${zip.password}") @Value("${data.windows.path}")
private String passwordZIP;
@Value("${upload.file.dest.path}")
private String uploadFilePath; private String uploadFilePath;
@Value("${upload.file.cache.dir}") @Value("${data.windows.path}")
private String windowsDir;
@Value("${data.file.cache.dir}")
private String fileCacheDir; private String fileCacheDir;
@Value("${upload.file.store.dir}") @Value("${data.file.storage.dir}")
private String fileStoreDir; 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"; private static final String UPLOAD_FILE_CHUNK_SUFFIX = ".part";
@ -74,8 +74,6 @@ public class EDDataFacadeImpl implements EDDataFacade {
if(Boolean.TRUE) { if(Boolean.TRUE) {
if (request.getUserId() == null || request.getUserId().isEmpty()) request.setUserId(IdWorker.getSnowFlakeIdString()); if (request.getUserId() == null || request.getUserId().isEmpty()) request.setUserId(IdWorker.getSnowFlakeIdString());
if (request.getUserName() == null || request.getUserName().isEmpty()) request.setUserName("user"); 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()); edDataInfo.setCreator(request.getUserId());
@ -153,150 +151,11 @@ public class EDDataFacadeImpl implements EDDataFacade {
* @param request * @param request
* @return * @return
*/ */
public ElectromagneticResult<Boolean> upload(EDDataRequest request) public ElectromagneticResult<Boolean> uploadFile(EDDataRequest request)
{ {
EDDataParams parames= EDDataMappers.INSTANCE.getEDDataParames(request); EDDataParams parames= EDDataMappers.INSTANCE.getEDDataParames(request);
Boolean isSuccess = edDataService.uploadFile(parames);
try { return ElectromagneticResultUtil.success(isSuccess);
// 获取目录编码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 osName = System.getProperty("os.name").toLowerCase();
String storageFilePath = osName.startsWith("win") ? uploadFilePath + fileStoreDir : fileStoreDir;
if(!edDataService.checkFolderIsExist(storageFilePath)) {
edDataService.createMultiLevelDirectory(storageFilePath);
}
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 = storageFilePath + "/" + 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 cacheUploadFilePath = osName.startsWith("win") ? uploadFilePath + fileCacheDir : fileCacheDir;
File cacheDirectory = new File(cacheUploadFilePath + "/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());
}
} }
/** /**
@ -309,16 +168,18 @@ public class EDDataFacadeImpl implements EDDataFacade {
*/ */
public ResponseEntity<InputStreamResource> download(FileDownloadDTO fileDownloadDTO, HttpServletResponse response) throws IOException public ResponseEntity<InputStreamResource> download(FileDownloadDTO fileDownloadDTO, HttpServletResponse response) throws IOException
{ {
// 获取文件存储的文件夹路径 // 获取文件存储的文件夹路径
String osName = System.getProperty("os.name").toLowerCase(); String osName = System.getProperty("os.name").toLowerCase();
String storageFilePath = osName.startsWith("win") ? uploadFilePath + fileStoreDir : fileStoreDir; String storageFilePath = osName.startsWith("win") ? uploadFilePath + fileStorageDir : fileStorageDir;
EDDataParams parames = new EDDataParams(); EDDataParams parames = new EDDataParams();
String dataId = fileDownloadDTO.getDataIdArr(); String dataId = fileDownloadDTO.getDataId();
parames.setDataId(dataId); parames.setDataId(dataId);
EDDataInfo edDataInfo = edDataService.getDataInfo(parames); EDDataInfo edDataInfo = edDataService.getDataInfo(parames);
Assert.isTrue(edDataInfo != null, "没有找到该下载文件"); Assert.isTrue(edDataInfo != null, "没有找到该下载文件");
JSONObject implantJsonObject = JSON.parseObject(edDataInfo.getImplantJson()); JSONObject implantJsonObject = JSON.parseObject(edDataInfo.getImplantJson());
String filePath = storageFilePath + "/" + implantJsonObject.getString("folderPath") + "/" + edDataInfo.getDataName(); String filePath = storageFilePath + "/" + implantJsonObject.getString("folderPath") + "/" + edDataInfo.getDataName();
Assert.isTrue(FileUtil.exist(filePath), "下载文件不存在。"); Assert.isTrue(FileUtil.exist(filePath), "下载文件不存在。");
File file = new File(filePath); File file = new File(filePath);
FileSystemResource fileSystemResource = new FileSystemResource(file); FileSystemResource fileSystemResource = new FileSystemResource(file);
@ -335,6 +196,7 @@ public class EDDataFacadeImpl implements EDDataFacade {
.contentLength(fileSystemResource.contentLength()) .contentLength(fileSystemResource.contentLength())
.contentType(MediaType.parseMediaType("application/octet-stream")) .contentType(MediaType.parseMediaType("application/octet-stream"))
.body(new InputStreamResource(fileSystemResource.getInputStream())); .body(new InputStreamResource(fileSystemResource.getInputStream()));
} }
/** /**
@ -464,7 +326,7 @@ public class EDDataFacadeImpl implements EDDataFacade {
ZipUtil.unzip(destZipPath, uploadFilePath + identifier + File.separator); ZipUtil.unzip(destZipPath, uploadFilePath + identifier + File.separator);
// 文件信息存入数据库 // 文件信息存入数据库
Boolean result = edDataService.importData(destZipPath); Map<String, List<String>> result = edDataService.importData(destZipPath);
return ElectromagneticResultUtil.success(result); return ElectromagneticResultUtil.success(result);
} }