fix: 78275

企标--导入--带附件导入报错
This commit is contained in:
2023-11-07 16:08:05 +08:00
parent 3f901d8796
commit e1b5f67984
2 changed files with 36 additions and 12 deletions
@@ -61,6 +61,7 @@ import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;
@@ -69,6 +70,7 @@ 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;
@@ -76,6 +78,7 @@ 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;
/**
@@ -87,6 +90,9 @@ import java.util.zip.ZipOutputStream;
@Component
public class LawsCommonServiceImpl implements ILawsCommonService {
@Value(value = "${jero.path.upload}")
private String uploadPath;
@Autowired
private LawsCommonMapper lawsCommonMapper;
@Autowired
@@ -1422,18 +1428,10 @@ public class LawsCommonServiceImpl implements ILawsCommonService {
}
FileUtils.copyInputStreamToFile(file.getInputStream(), new File(path + File.separator + file.getOriginalFilename()));
// 解压缩
String zipEntryName = FileUnZip.unZipFiles(path + File.separator + file.getOriginalFilename(), String.valueOf(path));
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.FOLDER_ONLY_ONE));
}
File fileNew = new File(zipEntryName);
List<File> fileList = new ArrayList<>();
for (File file1 : Objects.requireNonNull(fileNew.listFiles())) {
for (File file1 : Objects.requireNonNull(saveDirectory.listFiles())) {
if (file1.getName().contains(".xls") || file1.getName().contains(".xlsx")) {
fileList.add(file1);
}
@@ -1445,7 +1443,7 @@ public class LawsCommonServiceImpl implements ILawsCommonService {
FileUnZip.deleteDir(saveDirectory);
throw new JeroBootException(MessageUtils.getMessage(ResultCommon.IMPORT_TEMPLATE_ERROR));
}
List<File> excelfilelist = FileUnZip.readImpExcelFile(zipEntryName);
List<File> excelfilelist = FileUnZip.readImpExcelFile(saveDirectory.getPath());
System.gc();
if (excelfilelist.size() != 1) {
FileUnZip.deleteDir(saveDirectory);
@@ -1475,6 +1473,9 @@ public class LawsCommonServiceImpl implements ILawsCommonService {
}
// 数据内容格式校验
Map<String, File> fileMap = new HashMap<>();
// 遍历saveDirectory下的所有文件
populateFileMap(saveDirectory, saveDirectory, fileMap);
try (Workbook workbook = new XSSFWorkbook(Files.newInputStream(excelfilelist.get(0).toPath()))) {
// 检测是否是空Excel
Sheet sheet = workbook.getSheetAt(0);
@@ -1517,6 +1518,23 @@ public class LawsCommonServiceImpl implements ILawsCommonService {
return errMsgList;
}
private static void populateFileMap(File rootDir, File currentDir, Map<String, File> fileMap) {
File[] files = currentDir.listFiles(); // 列出所有文件和子目录
if (files != null) {
for (File file : files) {
// 对于每个文件/子目录,获取相对于rootDir的相对路径
Path relativePath = rootDir.toPath().relativize(file.toPath());
if (file.isDirectory()) {
// 如果是目录,递归调用
populateFileMap(rootDir, file, fileMap);
} else {
// 否则,将它加入到map中
fileMap.put(relativePath.toString().replace("\\", "/"), file);
}
}
}
}
/**
* 设置导入模板的第三行提示内容
*
@@ -687,7 +687,13 @@ public class ImportUtil {
if (!(lowerCasePath.endsWith(".pdf")
|| lowerCasePath.endsWith(".doc") || lowerCasePath.endsWith(".docx")
|| lowerCasePath.endsWith(".xls") || lowerCasePath.endsWith(".xlsx")
|| lowerCasePath.endsWith(".png") || lowerCasePath.endsWith(".jpg"))) {
|| lowerCasePath.endsWith(".ppt") || lowerCasePath.endsWith(".pptx")
|| lowerCasePath.endsWith(".rar") || lowerCasePath.endsWith(".zip")
|| lowerCasePath.endsWith(".jpg") || lowerCasePath.endsWith(".jpeg")
|| lowerCasePath.endsWith(".png") || lowerCasePath.endsWith(".avi")
|| lowerCasePath.endsWith(".wmv") || lowerCasePath.endsWith(".mov")
|| lowerCasePath.endsWith(".rm") || lowerCasePath.endsWith(".mp4")
|| lowerCasePath.endsWith(".cad"))) {
return false;
}
}