fix: 导入模板校验问题

This commit is contained in:
2023-11-03 15:41:24 +08:00
parent a277c29b43
commit c9c2a515ec
@@ -34,6 +34,7 @@ import com.jero.modules.personal.entity.LawsBrowsingHistory;
import com.jero.modules.personal.entity.LawsStandardCollection; import com.jero.modules.personal.entity.LawsStandardCollection;
import com.jero.modules.personal.service.ILawsBrowsingHistoryService; import com.jero.modules.personal.service.ILawsBrowsingHistoryService;
import com.jero.modules.personal.service.ILawsStandardCollectionService; 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.entity.SarFileSplitInfoEO;
import com.jero.modules.split.service.ISarFileSplitInfoService; import com.jero.modules.split.service.ISarFileSplitInfoService;
import com.jero.modules.sys.utils.UserUtils; 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 com.jero.modules.tag.service.ILawsAreaService;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils; import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.io.FileUtils;
import org.apache.commons.lang3.StringUtils; import org.apache.commons.lang3.StringUtils;
import org.apache.poi.ss.usermodel.*; import org.apache.poi.ss.usermodel.*;
import org.apache.poi.ss.util.CellRangeAddress; import org.apache.poi.ss.util.CellRangeAddress;
@@ -66,7 +68,6 @@ import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpServletResponse;
import java.io.*; import java.io.*;
import java.net.URLEncoder; import java.net.URLEncoder;
import java.nio.charset.Charset;
import java.nio.file.Files; import java.nio.file.Files;
import java.nio.file.Path; import java.nio.file.Path;
import java.text.SimpleDateFormat; import java.text.SimpleDateFormat;
@@ -74,7 +75,6 @@ import java.time.LocalDate;
import java.util.*; import java.util.*;
import java.util.stream.Collectors; import java.util.stream.Collectors;
import java.util.zip.ZipEntry; import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;
import java.util.zip.ZipOutputStream; import java.util.zip.ZipOutputStream;
/** /**
@@ -1352,39 +1352,47 @@ public class LawsCommonServiceImpl implements ILawsCommonService {
if (!Objects.requireNonNull(file.getOriginalFilename()).toLowerCase().endsWith(".zip")) { if (!Objects.requireNonNull(file.getOriginalFilename()).toLowerCase().endsWith(".zip")) {
throw new JeroBootException(MessageUtils.getMessage(ResultCommon.PLEASE_UPLOAD_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; Path path = Files.createTempDirectory("temp");
Map<String, File> fileMap = new HashMap<>(); File saveDirectory = new File(path.toUri());
try (ZipFile zipFile = new ZipFile(tempFile, Charset.forName("GBK"))) { if (!saveDirectory.isDirectory()) {
Enumeration<? extends ZipEntry> entries = zipFile.entries(); boolean mkdir = saveDirectory.mkdir();
// 文件数量校验 }
boolean foundExcel = false; // 初始化标志以检测是否存在Excel文件 FileUtils.copyInputStreamToFile(file.getInputStream(), new File(path + File.separator + file.getOriginalFilename()));
// 解压缩
String zipEntryName = FileUnZip.unZipFiles(path + File.separator + file.getOriginalFilename(), String.valueOf(path));
// 文件数量校验 // 判断压缩包下是否只有一个文件夹
while (entries.hasMoreElements()) { File fileTemp = new File(path.toUri());
ZipEntry entry = entries.nextElement(); int length = Objects.requireNonNull(fileTemp.listFiles()).length;
String entryName = entry.getName(); if (length > 2) {
boolean isRootLevel = !entryName.contains("/"); // '/' 作为目录分隔符 FileUnZip.deleteDir(saveDirectory);
throw new JeroBootException(MessageUtils.getMessage(ResultCommon.IMPORT_TEMPLATE_ERROR));
// 检查根目录下的Excel文件数量 }
if (!entry.isDirectory() && isRootLevel && (entryName.endsWith(".xlsx") || entryName.endsWith(".xls"))) { File fileNew = new File(zipEntryName);
if (foundExcel) { // 如果已找到Excel文件,则添加错误信息 List<File> fileList = new ArrayList<>();
throw new JeroBootException("压缩包根目录下仅能存在一个 Excel 为导入数据"); for (File file1 : Objects.requireNonNull(fileNew.listFiles())) {
} else { if (file1.getName().contains(".xls") || file1.getName().contains(".xlsx")) {
foundExcel = true; // 找到Excel文件,更新标志 fileList.add(file1);
targetExcelEntry = entry;
} }
} }
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));
} }
if (targetExcelEntry == null) { List<File> excelfilelist = FileUnZip.readImpExcelFile(zipEntryName);
System.gc();
if (excelfilelist.size() != 1) {
FileUnZip.deleteDir(saveDirectory);
throw new JeroBootException(MessageUtils.getMessage(ResultCommon.IMPORT_TEMPLATE_ERROR)); throw new JeroBootException(MessageUtils.getMessage(ResultCommon.IMPORT_TEMPLATE_ERROR));
} }
// 数据内容格式校验 // 数据内容格式校验
try (Workbook workbook = new XSSFWorkbook(zipFile.getInputStream(targetExcelEntry))) { Map<String, File> fileMap = new HashMap<>();
try (Workbook workbook = new XSSFWorkbook(Files.newInputStream(excelfilelist.get(0).toPath()))) {
// 检测是否是空Excel // 检测是否是空Excel
Sheet sheet = workbook.getSheetAt(0); Sheet sheet = workbook.getSheetAt(0);
// 验证前三行是否有数据 // 验证前三行是否有数据
@@ -1429,7 +1437,7 @@ public class LawsCommonServiceImpl implements ILawsCommonService {
} }
} }
return errMsgList; return errMsgList;
}
} }
/** /**