Compare commits

...

3 Commits

8 changed files with 329 additions and 299 deletions

View File

@ -68,8 +68,8 @@ public class EDDataController {
@ApiOperation(value = "下载",notes = "")
@RequestMapping(value = "/download",method = RequestMethod.GET)
public ResponseEntity<InputStreamResource> download(@RequestBody FileDownloadDTO fileDownloadDTO, HttpServletResponse response) throws IOException {
return edDataFacade.download(fileDownloadDTO, response);
public ResponseEntity<InputStreamResource> 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<InputStreamResource> batchExport(@RequestParam String dataIdArr, HttpServletResponse response) throws IOException {
return edDataFacade.batchExport(dataIdArr, response);
}
@ApiOperation(value = "获取已经上传的分片",notes = "")

View File

@ -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

View File

@ -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<String> dataIdList);
/**
* 解压后的数据目录

View File

@ -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<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 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<EDDataInfo> 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<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
@ -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<String> dataIdList)
{
String zipPathFileName = "";
String exportDataCachePath = "";
String enCodeZipPathFileName = "";
// 获取文件夹编码ID数组
List<String> categoryIdInputList = Arrays.asList(parames.getDataIdArr());
try {
// 获取文件夹编码ID数组
List<String> 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<Category> categoryAllList = categoryRepository.getAllCategories();
Map<String, Category> categoryAllMap = new HashMap(); //所有目录数组map
for (Category category : categoryAllList) {
categoryAllMap.put(category.getCategoryId(), category);
}
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);
}
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);
List<Category> categoryInputList = new ArrayList(); //客户端输入的目录数组
Map<String, Category> 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<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();
}
String jsonStringCategory = JSONUtil.toJsonStr(categoryInputList);
String categoryListFileFullPath = exportDataCachePath + "/categoryList.json";
FileUtil.writeString(jsonStringCategory, categoryListFileFullPath, "utf-8");
log.info("目录树数据已成功导出为JSON文件。" + categoryListFileFullPath);
}
// 导出文件信息数据和文件数据
{
// 保存文件信息数据为json文件
// 导出文件信息数据和文件数据
List<EDDataInfo> fileExportList = new ArrayList(); //需要导出的文件信息数据数组
Map<String, EDDataInfo> 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<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)
{
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<Category> categoryAllList = categoryRepository.getAllCategories();
Map<String, Category> 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<EDDataInfo> fileInfoAllList = edDataRepository.getDataInfoList(parames);
Map<String, EDDataInfo> fileInfoAllMap = new HashMap(); //所有文件数组map
for (EDDataInfo fileInfo : fileInfoAllList)
{
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);
}
String jsonStringCategory = FileUtil.readString(folderPath + "/categoryList.json", StandardCharsets.UTF_8);
JSONArray jsonArrayCategory = JSONUtil.parseArray(jsonStringCategory);
List<Category> categoryImportList = JSONUtil.toList(jsonArrayCategory, Category.class);
if (categoryImportList == null) {
log.error("读取并反序列化JSON文件数据为List<Category>失败!");
return null;
}
// 将目录树数据写入到数据库中
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);
}
String jsonStringFile = FileUtil.readString(folderPath + "/fileInfoList.json", StandardCharsets.UTF_8);
JSONArray jsonArrayFile = JSONUtil.parseArray(jsonStringFile);
List<EDDataInfo> fileInfoImportList = JSONUtil.toList(jsonArrayFile, EDDataInfo.class);
if (fileInfoImportList == null) {
log.error("读取并反序列化JSON文件数据为List<EDDataInfo>失败!");
return null;
}
// 将文件信息数据写入到数据库中
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();
}
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<String, List<String>> result = new HashMap<>();
result.put("success", importFileSuccess);
result.put("fail", importFileFail);

View File

@ -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<InputStreamResource> download(FileDownloadDTO fileDownloadDTO, HttpServletResponse response) throws IOException;
ResponseEntity<InputStreamResource> download(String dataId, HttpServletResponse response) throws IOException;
/**
* 导出
* @param request
* 导出数据
* @param dataIdArr
* @return
*/
ElectromagneticResult<Map<String, String>> batchExport(EDDataRequest request);
ResponseEntity<InputStreamResource> batchExport(String dataIdArr, HttpServletResponse response) throws IOException;
/**
* 导入
* 导入数据
* @param fileChunkDTO
* @return
*/

View File

@ -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;
/**
* 创建人
*/

View File

@ -10,6 +10,7 @@
<result column="data_no" jdbcType="VARCHAR" property="dataNo" />
<result column="data_name" jdbcType="VARCHAR" property="dataName" />
<result column="data_type" jdbcType="VARCHAR" property="dataType" />
<result column="file_type" jdbcType="VARCHAR" property="fileType" />
<result column="version" jdbcType="VARCHAR" property="version" />
<result column="content" jdbcType="VARCHAR" property="content" />
<result column="implant_json" jdbcType="VARCHAR" property="implantJson" />
@ -37,7 +38,7 @@
<selectKey keyProperty="id" order="AFTER" resultType="java.lang.Long">
SELECT LAST_INSERT_ID()
</selectKey>
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},

View File

@ -1,11 +1,13 @@
package com.electromagnetic.industry.software.data.manage.service.facade;
import cn.hutool.core.codec.Base64;
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 +16,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 +33,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 +57,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 +88,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,60 +170,84 @@ public class EDDataFacadeImpl implements EDDataFacade {
* WISDOM_DOWNLOAD
* /data/ed/file/download
*
* @param fileDownloadDTO
* @param dataId
* @return
*/
public ResponseEntity<InputStreamResource> download(FileDownloadDTO fileDownloadDTO, HttpServletResponse response) throws IOException
public ResponseEntity<InputStreamResource> 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);
FileSystemResource fileSystemResource = new FileSystemResource(filePath);
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");
String fileName = Base64.encode(fileSystemResource.getFilename());
response.setHeader("content-disposition","attachment;filename=" + fileName);
// 构建响应实体(可以返回<byte[]或Resource返回类型取决body入参类型)
return ResponseEntity
.ok()
.headers(headers)
.contentLength(fileSystemResource.contentLength())
.contentType(MediaType.parseMediaType("application/octet-stream"))
.contentType(MediaType.parseMediaType("application/octet-stream;charset=UTF-8"))
.body(new InputStreamResource(fileSystemResource.getInputStream()));
}
/**
* 导出
* @param request
* @param dataIdArr
* @return
*/
@Override
public ElectromagneticResult<Map<String, String>> batchExport(EDDataRequest request) {
public ResponseEntity<InputStreamResource> 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<String, String> result = new HashMap<>();
result.put("url", fileUrl);
return ElectromagneticResultUtil.success(result);
List<String> 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"));
// 构建响应实体(可以返回<byte[]或Resource返回类型取决body入参类型)
return ResponseEntity
.ok()
.headers(headers)
.contentLength(fileSystemResource.contentLength())
.contentType(MediaType.parseMediaType("application/octet-stream"))
.body(new InputStreamResource(fileSystemResource.getInputStream()));
}
return null;
}
/**
@ -230,21 +261,24 @@ public class EDDataFacadeImpl implements EDDataFacade {
String identifier = fileChunkDTO.getIdentifier();
String fileName = fileChunkDTO.getFileName();
String dataCachePath = edDataService.getDataCachePath();
String importDataCachePath = dataCachePath + importCacheDir;
// 首先检查文件是否存在如果存在则不允许重复上传
String destZipPath = uploadFilePath + identifier + File.separator + fileName;
String destZipPath = importDataCachePath + 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;
String destChunkPath = importDataCachePath + 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);
File dir = new File(importDataCachePath + identifier + File.separator);
if (!dir.exists()) {
dir.mkdir();
}
@ -266,7 +300,9 @@ public class EDDataFacadeImpl implements EDDataFacade {
}
private List<Integer> 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 +320,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 +333,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 +354,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<String, List<String>> result = edDataService.importData(destZipPath);
Map<String, List<String>> result = edDataService.importData(importDataCachePath + identifier);
// 删除 解压数据的 缓存目录
FileUtil.del(unzipFileOutputPath);
return ElectromagneticResultUtil.success(result);
}
@Override
public ElectromagneticResult<FileChunkResultDTO> 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) {