diff --git a/laws-modules/src/main/java/com/jero/modules/laws/common/service/impl/LawsCommonServiceImpl.java b/laws-modules/src/main/java/com/jero/modules/laws/common/service/impl/LawsCommonServiceImpl.java index 17541939..48a7974c 100644 --- a/laws-modules/src/main/java/com/jero/modules/laws/common/service/impl/LawsCommonServiceImpl.java +++ b/laws-modules/src/main/java/com/jero/modules/laws/common/service/impl/LawsCommonServiceImpl.java @@ -34,6 +34,7 @@ import com.jero.modules.personal.entity.LawsBrowsingHistory; import com.jero.modules.personal.entity.LawsStandardCollection; import com.jero.modules.personal.service.ILawsBrowsingHistoryService; import com.jero.modules.personal.service.ILawsStandardCollectionService; +import com.jero.modules.split.common.FileUnZip; import com.jero.modules.split.entity.SarFileSplitInfoEO; import com.jero.modules.split.service.ISarFileSplitInfoService; import com.jero.modules.sys.utils.UserUtils; @@ -51,6 +52,7 @@ import com.jero.modules.tag.enums.TableNameEnum; import com.jero.modules.tag.service.ILawsAreaService; import lombok.extern.slf4j.Slf4j; import org.apache.commons.collections4.CollectionUtils; +import org.apache.commons.io.FileUtils; import org.apache.commons.lang3.StringUtils; import org.apache.poi.ss.usermodel.*; import org.apache.poi.ss.util.CellRangeAddress; @@ -66,7 +68,6 @@ import javax.annotation.Resource; import javax.servlet.http.HttpServletResponse; import java.io.*; import java.net.URLEncoder; -import java.nio.charset.Charset; import java.nio.file.Files; import java.nio.file.Path; import java.text.SimpleDateFormat; @@ -74,7 +75,6 @@ import java.time.LocalDate; import java.util.*; import java.util.stream.Collectors; import java.util.zip.ZipEntry; -import java.util.zip.ZipFile; import java.util.zip.ZipOutputStream; /** @@ -1352,84 +1352,92 @@ public class LawsCommonServiceImpl implements ILawsCommonService { if (!Objects.requireNonNull(file.getOriginalFilename()).toLowerCase().endsWith(".zip")) { throw new JeroBootException(MessageUtils.getMessage(ResultCommon.PLEASE_UPLOAD_ZIP)); } - Path tempDirWithPrefix = Files.createTempDirectory("temp"); - File tempFile = new File(tempDirWithPrefix.toFile(), Objects.requireNonNull(file.getOriginalFilename())); - file.transferTo(tempFile); - ZipEntry targetExcelEntry = null; - Map fileMap = new HashMap<>(); - try (ZipFile zipFile = new ZipFile(tempFile, Charset.forName("GBK"))) { - Enumeration entries = zipFile.entries(); - // 文件数量校验 - boolean foundExcel = false; // 初始化标志以检测是否存在Excel文件 - - // 文件数量校验 - while (entries.hasMoreElements()) { - ZipEntry entry = entries.nextElement(); - String entryName = entry.getName(); - boolean isRootLevel = !entryName.contains("/"); // '/' 作为目录分隔符 - - // 检查根目录下的Excel文件数量 - if (!entry.isDirectory() && isRootLevel && (entryName.endsWith(".xlsx") || entryName.endsWith(".xls"))) { - if (foundExcel) { // 如果已找到Excel文件,则添加错误信息 - throw new JeroBootException("压缩包根目录下仅能存在一个 Excel 为导入数据"); - } else { - foundExcel = true; // 找到Excel文件,更新标志 - targetExcelEntry = entry; - } - } - } - if (targetExcelEntry == null) { - throw new JeroBootException(MessageUtils.getMessage(ResultCommon.IMPORT_TEMPLATE_ERROR)); - } - - // 数据内容格式校验 - try (Workbook workbook = new XSSFWorkbook(zipFile.getInputStream(targetExcelEntry))) { - // 检测是否是空Excel - Sheet sheet = workbook.getSheetAt(0); - // 验证前三行是否有数据 - esUtil.isEmptyExcel(sheet); - // 获取标题行中文 转换为LawsTag对象 - Row hearderRow = sheet.getRow(0); - // 获取全部字段 - List fieldList = lawsCommonMapper.getFieldList(tableName); - // 筛选表单显示的字段 - // 找到所有标题Map - Map headerMap = fieldList.stream().filter(lawsTag -> YesOrNoEnum.YES.getValue() - .equals(lawsTag.getIsShowForm().toString())).collect(Collectors.toMap( - LawsTag::getDbFieldTxt, lawsTag -> lawsTag - )); - // 按照实际顺序创建headerList - List headerList = new ArrayList<>(); - for (Cell cell : hearderRow) { - headerList.add(headerMap.get(cell.toString().replace("*", ""))); - } - // 如果是企标的话手动添加稽查内容字段 - if (TableNameEnum.ENTERPRISE_STANDARDS.getTableName().equals(tableName)) { - LawsTag inspectContentTag = new LawsTag(); - inspectContentTag.setDbFieldTxt("稽查内容"); - inspectContentTag.setFieldShowType(FieldShowTypeEnum.INSPECT_CONTENT_FILE_UPLOAD.getValue()); - inspectContentTag.setDbFieldName(FieldCommon.CHECK_LIST); - inspectContentTag.setFieldMustInput(YesOrNoEnum.NO.getValue()); - headerList.remove(headerList.size() - 1); - headerList.add(inspectContentTag); - } - // 从数据行开始遍历 - for (int i = 3; i < sheet.getPhysicalNumberOfRows(); i++) { - Map parameterMap = new HashMap<>(); - for (int j = 0; j < headerList.size(); j++) { - // 获取当前单元格 - Cell cell = sheet.getRow(i).getCell(j); - // 获取当前单元格内容对应的字段 - LawsTag tag = headerList.get(j); - // 调用导入工具类的校验方法 - errMsgList.addAll(importUtil.allColumnValid(cell, tag, i, j, parameterMap, fileMap)); - } - insertDataList.add(parameterMap); - } - } - return errMsgList; + Path path = Files.createTempDirectory("temp"); + File saveDirectory = new File(path.toUri()); + if (!saveDirectory.isDirectory()) { + boolean mkdir = saveDirectory.mkdir(); } + FileUtils.copyInputStreamToFile(file.getInputStream(), new File(path + File.separator + file.getOriginalFilename())); + // 解压缩 + String zipEntryName = FileUnZip.unZipFiles(path + File.separator + file.getOriginalFilename(), String.valueOf(path)); + + // 判断压缩包下是否只有一个文件夹 + File fileTemp = new File(path.toUri()); + int length = Objects.requireNonNull(fileTemp.listFiles()).length; + if (length > 2) { + FileUnZip.deleteDir(saveDirectory); + throw new JeroBootException(MessageUtils.getMessage(ResultCommon.IMPORT_TEMPLATE_ERROR)); + } + File fileNew = new File(zipEntryName); + List fileList = new ArrayList<>(); + for (File file1 : Objects.requireNonNull(fileNew.listFiles())) { + if (file1.getName().contains(".xls") || file1.getName().contains(".xlsx")) { + fileList.add(file1); + } + } + if (fileList.size() == 0) { + FileUnZip.deleteDir(saveDirectory); + throw new JeroBootException(MessageUtils.getMessage(ResultCommon.IMPORT_TEMPLATE_ERROR)); + } else if (fileList.size() > 1) { + FileUnZip.deleteDir(saveDirectory); + throw new JeroBootException(MessageUtils.getMessage(ResultCommon.IMPORT_TEMPLATE_ERROR)); + } + List excelfilelist = FileUnZip.readImpExcelFile(zipEntryName); + System.gc(); + if (excelfilelist.size() != 1) { + FileUnZip.deleteDir(saveDirectory); + throw new JeroBootException(MessageUtils.getMessage(ResultCommon.IMPORT_TEMPLATE_ERROR)); + } + + // 数据内容格式校验 + Map fileMap = new HashMap<>(); + try (Workbook workbook = new XSSFWorkbook(Files.newInputStream(excelfilelist.get(0).toPath()))) { + // 检测是否是空Excel + Sheet sheet = workbook.getSheetAt(0); + // 验证前三行是否有数据 + esUtil.isEmptyExcel(sheet); + // 获取标题行中文 转换为LawsTag对象 + Row hearderRow = sheet.getRow(0); + // 获取全部字段 + List fieldList = lawsCommonMapper.getFieldList(tableName); + // 筛选表单显示的字段 + // 找到所有标题Map + Map headerMap = fieldList.stream().filter(lawsTag -> YesOrNoEnum.YES.getValue() + .equals(lawsTag.getIsShowForm().toString())).collect(Collectors.toMap( + LawsTag::getDbFieldTxt, lawsTag -> lawsTag + )); + // 按照实际顺序创建headerList + List headerList = new ArrayList<>(); + for (Cell cell : hearderRow) { + headerList.add(headerMap.get(cell.toString().replace("*", ""))); + } + // 如果是企标的话手动添加稽查内容字段 + if (TableNameEnum.ENTERPRISE_STANDARDS.getTableName().equals(tableName)) { + LawsTag inspectContentTag = new LawsTag(); + inspectContentTag.setDbFieldTxt("稽查内容"); + inspectContentTag.setFieldShowType(FieldShowTypeEnum.INSPECT_CONTENT_FILE_UPLOAD.getValue()); + inspectContentTag.setDbFieldName(FieldCommon.CHECK_LIST); + inspectContentTag.setFieldMustInput(YesOrNoEnum.NO.getValue()); + headerList.remove(headerList.size() - 1); + headerList.add(inspectContentTag); + } + // 从数据行开始遍历 + for (int i = 3; i < sheet.getPhysicalNumberOfRows(); i++) { + Map parameterMap = new HashMap<>(); + for (int j = 0; j < headerList.size(); j++) { + // 获取当前单元格 + Cell cell = sheet.getRow(i).getCell(j); + // 获取当前单元格内容对应的字段 + LawsTag tag = headerList.get(j); + // 调用导入工具类的校验方法 + errMsgList.addAll(importUtil.allColumnValid(cell, tag, i, j, parameterMap, fileMap)); + } + insertDataList.add(parameterMap); + } + } + return errMsgList; + } /**