diff --git a/bootstrap/src/main/java/com/electromagnetic/industry/software/data/manage/controller/EDDataController.java b/bootstrap/src/main/java/com/electromagnetic/industry/software/data/manage/controller/EDDataController.java index 6327f96..db8002b 100644 --- a/bootstrap/src/main/java/com/electromagnetic/industry/software/data/manage/controller/EDDataController.java +++ b/bootstrap/src/main/java/com/electromagnetic/industry/software/data/manage/controller/EDDataController.java @@ -68,8 +68,8 @@ public class EDDataController { @ApiOperation(value = "下载",notes = "") @RequestMapping(value = "/download",method = RequestMethod.GET) - public ResponseEntity download(@RequestBody FileDownloadDTO fileDownloadDTO, HttpServletResponse response) throws IOException { - return edDataFacade.download(fileDownloadDTO, response); + public ResponseEntity download(@RequestParam String dataId, HttpServletResponse response) throws IOException { + return edDataFacade.download(dataId, response); } @@ -80,9 +80,9 @@ public class EDDataController { } @ApiOperation(value = "数据导出",notes = "") - @RequestMapping(value = "/batchExport",method = RequestMethod.POST) - public ElectromagneticResult batchExport(@RequestBody EDDataRequest request){ - return edDataFacade.batchExport(request); + @RequestMapping(value = "/batchExport",method = RequestMethod.GET) + public ResponseEntity batchExport(@RequestParam String dataIdArr, HttpServletResponse response) throws IOException { + return edDataFacade.batchExport(dataIdArr, response); } @ApiOperation(value = "获取已经上传的分片",notes = "") diff --git a/bootstrap/src/main/resources/application.properties b/bootstrap/src/main/resources/application.properties index 1381f03..357d3e0 100644 --- a/bootstrap/src/main/resources/application.properties +++ b/bootstrap/src/main/resources/application.properties @@ -39,3 +39,5 @@ data.file.storage.dir=/szsd/fileStorage/ data.upload.cache.dir=upload #导出数据时文件的缓存文件夹名称 data.export.cache.dir=export +#导入数据时文件的缓存文件夹名称 +data.import.cache.dir=import diff --git a/domain/src/main/java/com/electromagnetic/industry/software/data/manage/domain/boardservice/indicator/service/EDDataService.java b/domain/src/main/java/com/electromagnetic/industry/software/data/manage/domain/boardservice/indicator/service/EDDataService.java index b14df27..ee91a12 100644 --- a/domain/src/main/java/com/electromagnetic/industry/software/data/manage/domain/boardservice/indicator/service/EDDataService.java +++ b/domain/src/main/java/com/electromagnetic/industry/software/data/manage/domain/boardservice/indicator/service/EDDataService.java @@ -9,6 +9,25 @@ import java.util.Map; public interface EDDataService { + /** + * 获取数据存储目录 + * @return + */ + String getDataStoragePath(); + + /** + * 获取数据缓存目录 + * @return + */ + String getDataCachePath(); + + /** + * 获取文件的文件夹路径 + * @param categoryId + * @return + */ + String getFilePathOfFolder(String categoryId); + /** * 创建文件/文件夹数据信息 * @param edDataInfo @@ -44,13 +63,6 @@ public interface EDDataService { */ Integer getChildFileCount(EDDataParams parames); - /** - * 获取文件的文件夹路径 - * @param categoryId - * @return - */ - String getFilePathOfFolder(String categoryId); - /** * 文件上传 * @param parames @@ -67,10 +79,10 @@ public interface EDDataService { /** * 导出数据 - * @param parames + * @param dataIdList * @return */ - String exportData(EDDataParams parames); + String exportData(List dataIdList); /** * 解压后的数据目录 diff --git a/domain/src/main/java/com/electromagnetic/industry/software/data/manage/domain/boardservice/indicator/service/impl/EDDataServiceImpl.java b/domain/src/main/java/com/electromagnetic/industry/software/data/manage/domain/boardservice/indicator/service/impl/EDDataServiceImpl.java index 3ea899d..2478e20 100644 --- a/domain/src/main/java/com/electromagnetic/industry/software/data/manage/domain/boardservice/indicator/service/impl/EDDataServiceImpl.java +++ b/domain/src/main/java/com/electromagnetic/industry/software/data/manage/domain/boardservice/indicator/service/impl/EDDataServiceImpl.java @@ -7,6 +7,8 @@ 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.json.JSONArray; +import cn.hutool.json.JSONUtil; import cn.hutool.poi.excel.cell.CellSetter; import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.JSONObject; @@ -17,8 +19,10 @@ import com.electromagnetic.industry.software.data.manage.domain.boardservice.ind 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.JsonGenerator; import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.databind.ObjectMapper; +import com.fasterxml.jackson.databind.SerializationFeature; import com.github.pagehelper.PageHelper; import com.github.pagehelper.PageInfo; import electromagnetic.data.framework.share.exception.LoggerConstant; @@ -35,9 +39,11 @@ import org.springframework.web.multipart.MultipartFile; import javax.annotation.Resource; import java.io.*; +import java.nio.charset.StandardCharsets; import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.Paths; +import java.text.SimpleDateFormat; import java.util.*; /** @@ -100,6 +106,37 @@ public class EDDataServiceImpl implements EDDataService { return osName.startsWith("win") ? windowsDir + fileCacheDir : fileCacheDir; } + /** + * 获取文件的文件夹路径 + * @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 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 edDataInfo @@ -193,8 +230,7 @@ public class EDDataServiceImpl implements EDDataService { } EDDataInfo edDataInfo = edDataInfoList.get(0); - JSONObject implantJsonObject = JSON.parseObject(edDataInfo.getImplantJson()); - String filePathOfFolder = implantJsonObject.getString("folderPath"); + String filePathOfFolder = getFilePathOfFolder(edDataInfo.getCategoryId()); String fileStorageFullPath = dataStoragePath + filePathOfFolder + File.separator + edDataInfo.getDataName(); if (!FileUtil.exist(fileStorageFullPath)){ @@ -202,7 +238,9 @@ public class EDDataServiceImpl implements EDDataService { } String fileNameNew = parames.getName(); - FileUtil.rename(Paths.get(fileStorageFullPath) ,fileNameNew,true); + if(fileNameNew != null && fileNameNew != "" && !fileNameNew.equals(edDataInfo.getDataName())) { + FileUtil.rename(Paths.get(fileStorageFullPath) ,fileNameNew,true); + } edDataRepository.updateFileInfo(parames); @@ -219,6 +257,7 @@ public class EDDataServiceImpl implements EDDataService { Integer childFileCount = 0; List edDataInfoList = edDataRepository.getDataInfoList(parames); + parames.setDataId(null); for (EDDataInfo edDataInfo : edDataInfoList) { if(edDataInfo.getDataType().equals("folder")) { @@ -234,37 +273,6 @@ public class EDDataServiceImpl implements EDDataService { return childFileCount; } - /** - * 获取文件的文件夹路径 - * @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 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 @@ -330,11 +338,6 @@ public class EDDataServiceImpl implements EDDataService { edDataInfo.setDataStatus("publish"); edDataInfo.setSaveStatus("saving"); - JSONObject newImplantJsonObject = new JSONObject(); - newImplantJsonObject.put("folderPath", filePathOfFolder); - - edDataInfo.setImplantJson(newImplantJsonObject.toJSONString()); - boolean isSuccess = edDataRepository.createDataInfo(edDataInfo); } @@ -344,6 +347,9 @@ public class EDDataServiceImpl implements EDDataService { // 文件保存目录路径 String fileSavePath = dataStoragePath + File.separator + filePathOfFolder; String newFileName = edDataInfo.getDataId() + FOLDER_NAME_SEPARATOR + fileFullName; + if (!FileUtil.exist(fileSavePath)){ + FileUtil.mkdir(fileSavePath); + } String dataCachePath = getDataCachePath(); String uploadFileCachePath = dataCachePath + uploadCacheDir + FOLDER_NAME_SEPARATOR + getTimeStampString(); @@ -417,8 +423,7 @@ public class EDDataServiceImpl implements EDDataService { EDDataInfo edDataInfo = edDataRepository.getDataInfo(paramesFileInfo); if(edDataInfo != null) { - JSONObject implantJsonObject = JSON.parseObject(edDataInfo.getImplantJson()); - String filePathOfFolder = implantJsonObject.getString("folderPath"); + String filePathOfFolder = getFilePathOfFolder(dataId); String fileStoragePath = dataStoragePath + File.separator + filePathOfFolder; String fileStorageFullPath = fileStoragePath + File.separator + edDataInfo.getDataName(); if (FileUtil.exist(fileStoragePath)) { @@ -442,121 +447,88 @@ public class EDDataServiceImpl implements EDDataService { /** * 导出数据 - * @param parames + * @param dataIdList * @return */ - public String exportData(EDDataParams parames) + public String exportData(List dataIdList) { + String zipPathFileName = ""; + String exportDataCachePath = ""; + String enCodeZipPathFileName = ""; - // 获取文件夹编码ID数组 - List categoryIdInputList = Arrays.asList(parames.getDataIdArr()); + try { + // 获取文件夹编码ID数组 + List categoryIdInputList = dataIdList; - String dataCachePath = getDataCachePath(); + String dataCachePath = getDataCachePath(); - // 导出数据时的临时存放目录 - String exportDataCachePath = dataCachePath + exportCacheDir + FOLDER_NAME_SEPARATOR + getTimeStampString(); - if (!FileUtil.exist(exportDataCachePath)){ - FileUtil.mkdir(exportDataCachePath); - } + // 导出数据时的临时存放目录 + exportDataCachePath = dataCachePath + exportCacheDir + FOLDER_NAME_SEPARATOR + getTimeStampString(); + if (!FileUtil.exist(exportDataCachePath)) { + FileUtil.mkdir(exportDataCachePath); + } - List categoryAllList = categoryRepository.getAllCategories(); - Map categoryAllMap = new HashMap(); //所有目录数组map - for (Category category : categoryAllList) { - categoryAllMap.put(category.getCategoryId(), category); - } + List categoryAllList = categoryRepository.getAllCategories(); + Map categoryAllMap = new HashMap(); //所有目录数组map + for (Category category : categoryAllList) { + categoryAllMap.put(category.getCategoryId(), category); + } - EDDataParams folderParames = new EDDataParams(); - List fileAllList = edDataRepository.getDataInfoList(folderParames); //获取所有文件信息数据 - Map fileAllMap = new HashMap(); //所有文件数组map - for (EDDataInfo fileFinfo : fileAllList) { - fileAllMap.put(fileFinfo.getDataId(), fileFinfo); - } + EDDataParams folderParames = new EDDataParams(); + List fileAllList = edDataRepository.getDataInfoList(folderParames); //获取所有文件信息数据 + Map fileAllMap = new HashMap(); //所有文件数组map + for (EDDataInfo fileFinfo : fileAllList) { + fileAllMap.put(fileFinfo.getDataId(), fileFinfo); + } - List categoryInputList = new ArrayList(); //客户端输入的目录数组 - Map categoryInputMap = new HashMap(); //客户端输入的目录数组map - List fileInputList = new ArrayList(); //客户端输入的目录下的文件夹数组 - Map 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); + List categoryInputList = new ArrayList(); //客户端输入的目录数组 + Map categoryInputMap = new HashMap(); //客户端输入的目录数组map + for (String categoryId : categoryIdInputList) { + Category category = categoryAllMap.get(categoryId); + if (category != null) { + categoryInputList.add(category); + categoryInputMap.put(categoryId, category); } } - } - // 导出目录树数据为json文件 - { - - // 创建ObjectMapper对象,用于JSON处理 - ObjectMapper objectMapper = new ObjectMapper(); - // 将List转换为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(); - } + String jsonStringCategory = JSONUtil.toJsonStr(categoryInputList); + String categoryListFileFullPath = exportDataCachePath + "/categoryList.json"; + FileUtil.writeString(jsonStringCategory, categoryListFileFullPath, "utf-8"); + log.info("目录树数据已成功导出为JSON文件。" + categoryListFileFullPath); - } - - // 导出文件信息数据和文件数据 - { - // 保存文件信息数据为json文件 + // 导出文件信息数据和文件数据 List fileExportList = new ArrayList(); //需要导出的文件信息数据数组 + Map fileExportMap = new HashMap(); //需要导出的文件信息数据数组map 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); - } + if (categoryInputMap.get(fileFinfo.getCategoryId()) != null) { + fileExportList.add(fileFinfo); + fileExportMap.put(fileFinfo.getDataId(), fileFinfo); + } + } + // 找出子文件夹下的文件 + for (EDDataInfo fileFinfo : fileAllList) { + if (fileExportMap.get(fileFinfo.getCategoryId()) != null) { + fileExportList.add(fileFinfo); } } + // 将文件信息数据导出为json文件 + String jsonStringFile = JSONUtil.toJsonStr(fileExportList); + String fileListFileFullPath = exportDataCachePath + "/fileInfoList.json"; + FileUtil.writeString(jsonStringFile, fileListFileFullPath, "utf-8"); + log.info("文件数据已成功导出为JSON文件。" + fileListFileFullPath); - // 创建ObjectMapper对象,用于JSON处理 - ObjectMapper objectMapper = new ObjectMapper(); - - // 将List转换为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) - { + for (Category category : categoryInputList) { needExportfolder = dataStoragePath + category.getCategoryId() + FOLDER_NAME_SEPARATOR + category.getCategoryName(); - if (FileUtil.exist(needExportfolder)){ + if (FileUtil.exist(needExportfolder)) { Path source = Paths.get(needExportfolder); Path target = Paths.get(exportDataCachePath); FileUtil.copy(source, target); // StandardCopyOption.REPLACE_EXISTING @@ -564,19 +536,22 @@ public class EDDataServiceImpl implements EDDataService { } log.info("文件数据已成功复制到目标目录。"); - } - // 将目录树数据 和 文件夹及文件夹内数据 进行压缩打包 - { - String zipPathFileName = dataCachePath + "/comac_exportData1.comac"; - String enCodeZipPathFileName = dataCachePath + "/comac_exportData.comac"; + + // 将目录树数据 和 文件夹及文件夹内数据 进行压缩打包 + Date date = new Date(); + SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd-HHmmss"); + String dateString = sdf.format(date); + + zipPathFileName = dataCachePath + "/exportData_" + dateString; + enCodeZipPathFileName = dataCachePath + "/exportData_" + dateString + ".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); + try ( + InputStream inputStream = new FileInputStream(zipPathFileName); OutputStream outputStream = new FileOutputStream(enCodeZipPathFileName); ) { aes.encrypt(inputStream, outputStream, true); @@ -584,15 +559,19 @@ public class EDDataServiceImpl implements EDDataService { log.error("文件加密错误..", e); } - //FileUtil.del(zipPathFileName);//删除临时目录 - log.info("目录树数据+文件数据已成功复制到目标目录。" + zipPathFileName); } + catch (Exception e) { + log.error("导出数据异常..", e); + } + finally { + //删除临时文件 + //FileUtil.del(zipPathFileName); + // 删除 导出数据的 缓存目录 + FileUtil.del(exportDataCachePath); + } - // 删除 导出数据的 缓存目录 - FileUtil.del(exportDataCachePath); - - return dataCachePath + "/comac_exportData.comac"; + return enCodeZipPathFileName; } /** @@ -605,8 +584,7 @@ public class EDDataServiceImpl implements EDDataService { // 获取所有目录树节点数据 List categoryAllList = categoryRepository.getAllCategories(); Map categoryAllMap = new HashMap(); //所有目录数组map - for (Category category : categoryAllList) - { + for (Category category : categoryAllList) { categoryAllMap.put(category.getCategoryId(), category); } @@ -614,126 +592,90 @@ public class EDDataServiceImpl implements EDDataService { EDDataParams parames = new EDDataParams(); List fileInfoAllList = edDataRepository.getDataInfoList(parames); Map fileInfoAllMap = new HashMap(); //所有文件数组map - for (EDDataInfo fileInfo : fileInfoAllList) - { + for (EDDataInfo fileInfo : fileInfoAllList) { fileInfoAllMap.put(fileInfo.getDataId(), fileInfo); } // 读取并反序列化目录树的JSON文件数据为List;然后写入到数据库中。 - List categoryImportList = null; - { - // 读取文件数据 - - try { - // 创建ObjectMapper对象,用于JSON处理 - ObjectMapper objectMapper = new ObjectMapper(); - - // 指定要读取的JSON文件路径 - File jsonFile = new File(folderPath + "/categoryList.json"); - - // 将JSON数据从文件中读取并反序列化为List - categoryImportList = objectMapper.readValue(jsonFile, objectMapper.getTypeFactory().constructCollectionType(List.class, Category.class)); - if (categoryImportList == null) { - log.error("读取并反序列化JSON文件数据为List失败!"); - return null; - } - } catch (IOException e) { - e.printStackTrace(); - } - - // 将目录树数据写入到数据库中 - for (Category category : categoryImportList) { - if (categoryAllMap.get(category.getCategoryId()) == null) { - categoryRepository.createCategory(category); - } + String jsonStringCategory = FileUtil.readString(folderPath + "/categoryList.json", StandardCharsets.UTF_8); + JSONArray jsonArrayCategory = JSONUtil.parseArray(jsonStringCategory); + List categoryImportList = JSONUtil.toList(jsonArrayCategory, Category.class); + if (categoryImportList == null) { + log.error("读取并反序列化JSON文件数据为List失败!"); + return null; + } + // 将目录树数据写入到数据库中 + for (Category category : categoryImportList) { + if (categoryAllMap.get(category.getCategoryId()) == null) { + categoryRepository.createCategory(category); } } // 读取并反序列化文件信息的JSON文件数据为List;然后写入到数据库中。 - List fileInfoImportList = null; - { - // 读取文件数据 - - try { - // 创建ObjectMapper对象,用于JSON处理 - ObjectMapper objectMapper = new ObjectMapper(); - - // 指定要读取的JSON文件路径 - File jsonFile = new File(folderPath + "/fileInfoList.json"); - - // 将JSON数据从文件中读取并反序列化为List - fileInfoImportList = objectMapper.readValue(jsonFile, objectMapper.getTypeFactory().constructCollectionType(List.class, Category.class)); - if (fileInfoImportList == null) { - log.error("读取并反序列化JSON文件数据为List失败!"); - return null; - } - } catch (IOException e) { - e.printStackTrace(); - } - - // 将文件信息数据写入到数据库中 - for (EDDataInfo fileInfo : fileInfoImportList) { - if (fileInfoAllMap.get(fileInfo.getDataId()) == null) { - edDataRepository.createDataInfo(fileInfo); - } + String jsonStringFile = FileUtil.readString(folderPath + "/fileInfoList.json", StandardCharsets.UTF_8); + JSONArray jsonArrayFile = JSONUtil.parseArray(jsonStringFile); + List fileInfoImportList = JSONUtil.toList(jsonArrayFile, EDDataInfo.class); + if (fileInfoImportList == null) { + log.error("读取并反序列化JSON文件数据为List失败!"); + return null; + } + // 将文件信息数据写入到数据库中 + for (EDDataInfo fileInfo : fileInfoImportList) { + if (fileInfoAllMap.get(fileInfo.getDataId()) == null) { + edDataRepository.createDataInfo(fileInfo); } } // 将解压后的文件夹和文件 移动到 上传文件的存储目录中 List importFileSuccess = new ArrayList(); List 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(); - } + String dataStoragePath = getDataStoragePath(); + if (!FileUtil.exist(dataStoragePath)){ + FileUtil.mkdir(dataStoragePath); } + String importDataCachePath = folderPath; //导入数据时 数据文件解压后的目录 + String importFileCachePath = ""; //需要导入的文件的缓存路径 + String importFileCacheFullPath = ""; //需要导入的文件的全路径 + String fileStorageFolder = ""; //文件存储的文件夹 + String fileStoragePath = ""; //文件存储的文件夹路径 + String fileStorageFullPath = ""; //文件存储的文件全路径 + + for (EDDataInfo fileInfo : fileInfoImportList) { + fileStorageFolder = getFilePathOfFolder(fileInfo.getCategoryId()); + importFileCachePath = importDataCachePath + File.separator + fileStorageFolder; + importFileCacheFullPath = importFileCachePath + File.separator + fileInfo.getDataName(); + fileStoragePath = dataStoragePath + File.separator + 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(fileInfo.getDataType().equals("folder")) { + if (!FileUtil.exist(fileStorageFullPath)) { + FileUtil.mkdir(fileStorageFullPath); + } + } else if(fileInfo.getDataType().equals("file")) { + Path source = Paths.get(importFileCacheFullPath); + Path target = Paths.get(fileStorageFullPath); + FileUtil.move(source,target,true); + } + + importFileSuccess.add(fileInfo.getDataId() + FOLDER_NAME_SEPARATOR + fileInfo.getDataName()); + } + else + { + importFileFail.add(fileInfo.getDataId() + FOLDER_NAME_SEPARATOR + fileInfo.getDataName()); + } + } + + Map> result = new HashMap<>(); result.put("success", importFileSuccess); result.put("fail", importFileFail); diff --git a/facade/src/main/java/com/electromagnetic/industry/software/data/manage/facade/EDDataFacade.java b/facade/src/main/java/com/electromagnetic/industry/software/data/manage/facade/EDDataFacade.java index 721b5ed..2463609 100644 --- a/facade/src/main/java/com/electromagnetic/industry/software/data/manage/facade/EDDataFacade.java +++ b/facade/src/main/java/com/electromagnetic/industry/software/data/manage/facade/EDDataFacade.java @@ -3,7 +3,6 @@ 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; @@ -55,20 +54,20 @@ public interface EDDataFacade { /** * 下载 * - * @param fileDownloadDTO + * @param dataId * @return */ - ResponseEntity download(FileDownloadDTO fileDownloadDTO, HttpServletResponse response) throws IOException; + ResponseEntity download(String dataId, HttpServletResponse response) throws IOException; /** - * 导出 - * @param request + * 导出数据 + * @param dataIdArr * @return */ - ElectromagneticResult> batchExport(EDDataRequest request); + ResponseEntity batchExport(String dataIdArr, HttpServletResponse response) throws IOException; /** - * 导入 + * 导入数据 * @param fileChunkDTO * @return */ diff --git a/facade/src/main/java/com/electromagnetic/industry/software/data/manage/response/indicator/EDDataResponse.java b/facade/src/main/java/com/electromagnetic/industry/software/data/manage/response/indicator/EDDataResponse.java index 1e8d19a..28c6089 100644 --- a/facade/src/main/java/com/electromagnetic/industry/software/data/manage/response/indicator/EDDataResponse.java +++ b/facade/src/main/java/com/electromagnetic/industry/software/data/manage/response/indicator/EDDataResponse.java @@ -61,6 +61,22 @@ public class EDDataResponse implements Serializable { * 状态:publish:发布,occupy:占用 */ private String dataStatus; + /** + * 备注 + */ + private String note; + /** + * 编辑人 + */ + private String editor; + /** + * 批量上传时间 + */ + private Date gmtBatchUpload; + /** + * 保存状态 + */ + private String saveStatus; /** * 创建人 */ diff --git a/infrastructure/repository/src/main/resources/sqlmapper/EdDataInfoMapper.xml b/infrastructure/repository/src/main/resources/sqlmapper/EdDataInfoMapper.xml index 39c1094..3b7a41d 100644 --- a/infrastructure/repository/src/main/resources/sqlmapper/EdDataInfoMapper.xml +++ b/infrastructure/repository/src/main/resources/sqlmapper/EdDataInfoMapper.xml @@ -10,6 +10,7 @@ + @@ -37,7 +38,7 @@ SELECT LAST_INSERT_ID() - insert into ed_data_info (id, category_id, + insert into ed_data_info (category_id, data_id, data_no, data_name, data_type, file_type, version, content, implant_json, data_status, @@ -46,7 +47,7 @@ modifier, modifier_name, gmt_modified, effect_flag ) - values (#{id,jdbcType=VARCHAR}, #{categoryId,jdbcType=VARCHAR}, + values (#{categoryId,jdbcType=VARCHAR}, #{dataId,jdbcType=VARCHAR}, #{dataNo,jdbcType=VARCHAR}, #{dataName,jdbcType=VARCHAR}, #{dataType,jdbcType=VARCHAR}, #{fileType,jdbcType=VARCHAR},#{version,jdbcType=VARCHAR}, #{content,jdbcType=VARCHAR}, #{implantJson,jdbcType=VARCHAR}, #{dataStatus,jdbcType=VARCHAR}, diff --git a/service/src/main/java/com/electromagnetic/industry/software/data/manage/service/facade/EDDataFacadeImpl.java b/service/src/main/java/com/electromagnetic/industry/software/data/manage/service/facade/EDDataFacadeImpl.java index 4543d51..d692298 100644 --- a/service/src/main/java/com/electromagnetic/industry/software/data/manage/service/facade/EDDataFacadeImpl.java +++ b/service/src/main/java/com/electromagnetic/industry/software/data/manage/service/facade/EDDataFacadeImpl.java @@ -3,9 +3,10 @@ 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.text.StrFormatter; import cn.hutool.core.util.ZipUtil; -import com.alibaba.fastjson.JSON; -import com.alibaba.fastjson.JSONObject; +import cn.hutool.crypto.SecureUtil; +import cn.hutool.crypto.symmetric.AES; 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; @@ -14,7 +15,6 @@ 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; @@ -32,6 +32,8 @@ import javax.annotation.Resource; import javax.servlet.http.HttpServletResponse; import java.io.*; import java.io.IOException; +import java.net.URLEncoder; +import java.nio.charset.StandardCharsets; import java.util.*; import java.util.stream.Collectors; @@ -54,6 +56,8 @@ public class EDDataFacadeImpl implements EDDataFacade { private String uploadCacheDir; @Value("${data.export.cache.dir}") private String exportCacheDir; + @Value("${data.import.cache.dir}") + private String importCacheDir; private static final String UPLOAD_FILE_CHUNK_SUFFIX = ".part"; @@ -83,12 +87,14 @@ public class EDDataFacadeImpl implements EDDataFacade { edDataInfo.setCategoryId(request.getParentId()); edDataInfo.setDataName(request.getName()); edDataInfo.setNote(request.getNote()); + edDataInfo.setGmtBatchUpload(new Date()); edDataInfo.setDataId(IdWorker.getSnowFlakeIdString()); edDataInfo.setDataNo(edDataInfo.getDataId()); edDataInfo.setDataType("folder"); edDataInfo.setVersion("1.0.0"); edDataInfo.setDataStatus("publish"); + edDataInfo.setSaveStatus("success"); Boolean isSuccess = edDataService.createDataInfo(edDataInfo); return ElectromagneticResultUtil.success(isSuccess); @@ -163,32 +169,37 @@ public class EDDataFacadeImpl implements EDDataFacade { * WISDOM_DOWNLOAD * /data/ed/file/download * - * @param fileDownloadDTO + * @param dataId * @return */ - public ResponseEntity download(FileDownloadDTO fileDownloadDTO, HttpServletResponse response) throws IOException + public ResponseEntity download(String dataId, HttpServletResponse response) throws IOException { // 获取文件存储的文件夹路径 String osName = System.getProperty("os.name").toLowerCase(); String storageFilePath = osName.startsWith("win") ? uploadFilePath + fileStorageDir : fileStorageDir; EDDataParams parames = new EDDataParams(); - 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(); + String filePathOfFolder = edDataService.getFilePathOfFolder(edDataInfo.getCategoryId()); + String filePath = storageFilePath + "/" + filePathOfFolder + "/" + edDataInfo.getDataName(); 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())); + String fileName = fileSystemResource.getFilename(); + byte[] fileNameBytes = fileName.getBytes(StandardCharsets.UTF_8); + fileName = new String(fileNameBytes, 0, fileNameBytes.length, StandardCharsets.ISO_8859_1); + String attachment = StrFormatter.format("attachment;filename={}", fileName); + //headers.add("Content-Disposition", attachment); +// headers.set("attachment", fileName); headers.add("Pragma", "no-cache"); headers.add("Expires", "0"); - + response.setHeader("content-disposition","attachment;filename=" + URLEncoder.encode(fileName,"UTF-8")); +// response.setHeader("Content-Disposition", "attachment; filename=\"" + new String(fileSystemResource.getFilename().getBytes("UTF-8"), "ISO8859-1")); // 构建响应实体(可以返回> batchExport(EDDataRequest request) { + public ResponseEntity batchExport(String dataIdArr, HttpServletResponse response) throws IOException { //1、根据用户选择层级树编码查出所有文件和文件夹list //2、循环list将每个文件复制到新建目录并进行重命名,命名规则:目录树编码+,+文件夹编码(有则填写无则为空)+,+文件编码 //3、打包新建为zip,并根据生产下载地址(域名+文件路径+文件) //4、返回前端下载的地址 // **********在导出的过程中可能会出现有用户上传文件的情况 - EDDataParams parames= EDDataMappers.INSTANCE.getEDDataParames(request); - String fileUrl = edDataService.exportData(parames); + Map result = new HashMap<>(); - result.put("url", fileUrl); - return ElectromagneticResultUtil.success(result); + List dataIdList = Arrays.asList(dataIdArr.split(",")); + if(dataIdList.size() > 0) { + String filePath = edDataService.exportData(dataIdList); + + 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"); + String fileName = fileSystemResource.getFilename(); + byte[] fileNameBytes = fileName.getBytes(StandardCharsets.UTF_8); + fileName = new String(fileNameBytes, 0, fileNameBytes.length, StandardCharsets.ISO_8859_1); + String attachment = StrFormatter.format("attachment;filename={}", fileName); + headers.add("Pragma", "no-cache"); + headers.add("Expires", "0"); + response.setHeader("content-disposition","attachment;filename=" + URLEncoder.encode(fileName,"UTF-8")); + + // 构建响应实体(可以返回 getUploadedChunks(String identifier) { - String destPath = uploadFilePath + identifier; + String dataCachePath = edDataService.getDataCachePath(); + String importDataCachePath = dataCachePath + importCacheDir; + String destPath = importDataCachePath + identifier; if (!FileUtil.exist(new File(destPath))) { return new ArrayList<>(); @@ -284,10 +327,12 @@ public class EDDataFacadeImpl implements EDDataFacade { // TODO 需要验证如果一个分片上传一半网络断开,则该分片的存储情况。 @Override public ElectromagneticResult mergeChunks(String identifier, String fileName, Integer totalChunks) { + String dataCachePath = edDataService.getDataCachePath(); + String importDataCachePath = dataCachePath + importCacheDir; // 检查所有分片是否已经上传完成,分片编号从1开始 for (int i = 1; i <= totalChunks; i++) { - String tmpPath = uploadFilePath + identifier + File.separator + i + UPLOAD_FILE_CHUNK_SUFFIX; + String tmpPath = importDataCachePath + identifier + File.separator + i + UPLOAD_FILE_CHUNK_SUFFIX; if (!FileUtil.exist(new File(tmpPath))) { log.error("第{}个分片没有上传完成,请上传完成后再合并。", i); ElectromagneticResultUtil.fail("-1", "文件尚未上传完成。"); @@ -295,13 +340,13 @@ public class EDDataFacadeImpl implements EDDataFacade { } // 合并分片 - String destZipPath = uploadFilePath + identifier + File.separator + fileName; + String destZipPath = importDataCachePath + 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; + String tmpPath = importDataCachePath + identifier + File.separator + i + UPLOAD_FILE_CHUNK_SUFFIX; RandomAccessFile tmp = new RandomAccessFile(new File(tmpPath), "r"); int len; while ((len = tmp.read(buffer)) != -1) { @@ -316,28 +361,48 @@ public class EDDataFacadeImpl implements EDDataFacade { // 删除分片 for (int i = 1; i <= totalChunks; i++) { - String tmpPath = uploadFilePath + identifier + File.separator + i + UPLOAD_FILE_CHUNK_SUFFIX; + String tmpPath = importDataCachePath + identifier + File.separator + i + UPLOAD_FILE_CHUNK_SUFFIX; FileUtil.del(tmpPath); } // 检验文件的MD5值 + // 解密文件 + String decryptFilePath = destZipPath + "_decrypted"; + String password = "adknfhkj87654knd"; + AES aes = SecureUtil.aes(password.getBytes()); // aesKey是加密密钥 + try( + InputStream inputStream = new FileInputStream(destZipPath); + OutputStream outputStream = new FileOutputStream(decryptFilePath); + ) { + aes.decrypt(inputStream, outputStream, true); + } catch (Exception e) { + log.error("文件加密错误..", e); + } + // 解压文件 - ZipUtil.unzip(destZipPath, uploadFilePath + identifier + File.separator); + String unzipFileOutputPath = importDataCachePath + identifier + File.separator; + ZipUtil.unzip(decryptFilePath, unzipFileOutputPath); // 文件信息存入数据库 - Map> result = edDataService.importData(destZipPath); + Map> result = edDataService.importData(importDataCachePath + identifier); + + // 删除 解压数据的 缓存目录 + FileUtil.del(unzipFileOutputPath); return ElectromagneticResultUtil.success(result); } @Override public ElectromagneticResult checkChunkExist(FileChunkDTO fileChunkDTO) { + String dataCachePath = edDataService.getDataCachePath(); + String importDataCachePath = dataCachePath + importCacheDir; + FileChunkResultDTO res = new FileChunkResultDTO(); // 首先判断zip文件是否存在,如果不存在则表示还没有上传完成。 String identifier = fileChunkDTO.getIdentifier(); String fileName = fileChunkDTO.getFileName(); - String destZipPath = uploadFilePath + identifier + File.separator + fileName; + String destZipPath = importDataCachePath + identifier + File.separator + fileName; boolean existFile = FileUtil.exist(new File(destZipPath)); if (existFile) {