This commit is contained in:
zt
2025-04-17 09:07:39 +08:00
parent 3451d32ef5
commit 871fa55af1

View File

@ -30,7 +30,7 @@ import org.springframework.web.multipart.MultipartFile;
import javax.validation.constraints.NotNull;
import java.io.*;
import java.nio.charset.StandardCharsets;
import java.nio.charset.Charset;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
@ -58,44 +58,44 @@ public class UploadZipController {
private final IBgtProjectRecruitApplyService recruitApplyService;
private static final String TEMP_DIR = "ruoyi/uploadPath/temporaryZip"; //临时解压
private static final String SAVE_DIR = "ruoyi/uploadPath/recruit"; //保存目录
private static final String RECORD_DIR = "ruoyi/uploadPath/record"; //记录目录
private static final String SAVE_DIR = "ruoyi/uploadPath/recruit"; //保存目录
private static final String RECORD_DIR = "ruoyi/uploadPath/record"; //记录目录
@ApiOperation("上传压缩文件")
@PostMapping("/upload-zip")
public AjaxResult<Void> uploadZipFile(@RequestParam("file") MultipartFile file
, @RequestParam("recruitId")@NotNull(message = "招工不能为空") Long recruitId
, @RequestParam("userId")@NotNull(message = "用户不能为空") Long userId) {
, @RequestParam("recruitId") @NotNull(message = "招工不能为空") Long recruitId
, @RequestParam("userId") @NotNull(message = "用户不能为空") Long userId) {
if (file.isEmpty()) {
throw new BaseException("上传的文件为空!");
throw new BaseException("上传的文件为空!");
}
String originalFilename = file.getOriginalFilename();
if (originalFilename == null || !originalFilename.toLowerCase().endsWith(".zip")) {
throw new BaseException("上传的文件不是有效的 ZIP 文件!");
throw new BaseException("上传的文件不是有效的 ZIP 文件!");
}
String[] split = originalFilename.split("_");
if(split.length != 2 || !split[0].equals(recruitId.toString())){
throw new BaseException("文件名与所选择招工不匹配");
if (split.length != 2 || !split[0].equals(recruitId.toString())) {
throw new BaseException("文件名与所选择招工不匹配");
}
BgtProjectRecruit recruit = recruitService.queryById(recruitId);
if( recruit== null){
throw new BaseException("招工信息不存在!");
if (recruit == null) {
throw new BaseException("招工信息不存在!");
}
BgtUser bgtUser = bgtUserService.selectUserByUserId(userId);
if (bgtUser == null){
throw new BaseException("当前用户不存在!");
if (bgtUser == null) {
throw new BaseException("当前用户不存在!");
}
String username = bgtUser.getUsername();
String firstLevelFolderName ="";
String firstLevelFolderName = "";
try {
// 保存上传的压缩文件
String s = DigestUtil.md5Hex(recruit.getRecruitName());
firstLevelFolderName = recruitId + "_" + s;
File zipFile = new File(TEMP_DIR, firstLevelFolderName);
File zipFile = new File(TEMP_DIR, firstLevelFolderName + ".zip");
ensureDirectoryExists(zipFile.getParentFile());
try (OutputStream os = new FileOutputStream(zipFile)) {
os.write(file.getBytes());
@ -113,32 +113,35 @@ public class UploadZipController {
moveFilesToSaveDir(extractDir, recruitId, username);
// 异步执行 RECORD_DIR 操作和删除临时文件操作
asyncProcessRecordAndDeleteTemp(extractDir, zipFile, recruitId,username,userId);
asyncProcessRecordAndDeleteTemp(extractDir, zipFile, recruitId, username, userId);
return AjaxResult.success("文件上传并处理成功");
} catch (Exception e) {
// 删除临时文件和文件夹
File extractDir = new File(TEMP_DIR, firstLevelFolderName);
deleteFolder(extractDir);
File zipFile = new File(TEMP_DIR, firstLevelFolderName + ".zip");
deleteFolder(zipFile);
e.printStackTrace();
return AjaxResult.error("文件处理过程中出现错误");
}
}
@Async
public CompletableFuture<Void> asyncProcessRecordAndDeleteTemp(File extractDir, File zipFile, Long recruitId,String username,Long userId) {
public CompletableFuture<Void> asyncProcessRecordAndDeleteTemp(File extractDir, File zipFile, Long recruitId, String username, Long userId) {
return CompletableFuture.runAsync(() -> {
try {
// 移动到 RECORD_DIR
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyyMMddHHmmss");
String timeStamp = LocalDateTime.now().format(formatter);
BgtProjectRecruit recruit = recruitService.queryById(recruitId);
String firstLevelFolderName = recruit.getId() + "_" + recruit.getRecruitName();
String s = DigestUtil.md5Hex(recruit.getRecruitName());
String firstLevelFolderName = recruitId + "_" + s;
File recordDestDir = new File(RECORD_DIR, firstLevelFolderName);
ensureDirectoryExists(recordDestDir);
List<AnnexRecord> annexRecordList = new ArrayList<>();
moveFilesToRecordDirRecursively(extractDir, recordDestDir, timeStamp, annexRecordList, recruitId,username,userId);
moveFilesToRecordDirRecursively(extractDir, recordDestDir, timeStamp, annexRecordList, recruitId, username, userId);
if (CollectionUtil.isNotEmpty(annexRecordList)) {
annexRecordService.saveBatch(annexRecordList);
@ -164,13 +167,18 @@ public class UploadZipController {
}
private void extractZipFile(File zipFile, File extractDir) throws IOException {
try (ZipInputStream zis = new ZipInputStream(new FileInputStream(zipFile), StandardCharsets.UTF_8)) {
try (ZipInputStream zis = new ZipInputStream(new FileInputStream(zipFile), Charset.forName("ISO-8859-15"))) {
ZipEntry zipEntry;
while ((zipEntry = zis.getNextEntry()) != null) {
File newFile = newFile(extractDir, zipEntry);
if (zipEntry.isDirectory()) {
ensureDirectoryExists(newFile);
// 是目录,不重命名,直接创建目录
File newDir = new File(extractDir, zipEntry.getName());
ensureDirectoryExists(newDir);
} else {
// 是文件,进行重命名
String originalFileName = zipEntry.getName();
String newFileName = renameFile(originalFileName);
File newFile = new File(extractDir, newFileName);
// 为文件创建父目录
ensureDirectoryExists(newFile.getParentFile());
@ -185,6 +193,28 @@ public class UploadZipController {
}
}
}
}
private String renameFile(String originalFileName) {
int lastIndexOfSlash = originalFileName.lastIndexOf('/');
int lastIndexOfDot = originalFileName.lastIndexOf('.');
// 获取文件所在的目录路径
String directoryPath = lastIndexOfSlash != -1 ? originalFileName.substring(0, lastIndexOfSlash + 1) : "";
// 获取文件扩展名
String fileExtension = lastIndexOfDot != -1 ? originalFileName.substring(lastIndexOfDot) : "";
// 获取文件名(不包含扩展名)
String fileNameWithoutExtension = lastIndexOfSlash != -1 && lastIndexOfDot != -1 ?
originalFileName.substring(lastIndexOfSlash + 1, lastIndexOfDot) :
originalFileName;
// 将 MD5 值转换为一个整数
String s = DigestUtil.md5Hex(fileNameWithoutExtension);
return directoryPath + s + fileExtension;
}
private File newFile(File destinationDir, ZipEntry zipEntry) throws IOException {
@ -218,12 +248,12 @@ public class UploadZipController {
String[] split = firstLevelFolderName.split("_");
String card = split[1];
WgzUser wgzUser = wgzUserService.findByIdentityCard(card);
if(wgzUser == null){
throw new BaseException("文件格式错误");
if (wgzUser == null) {
throw new BaseException("文件格式错误");
}
BgtProjectRecruitApply oneByUserIdAndRecruitId = recruitApplyService.getOneByUserIdAndRecruitId(wgzUser.getUserId(), recruitId);
if(oneByUserIdAndRecruitId == null){
throw new BaseException("状态不对");
if (oneByUserIdAndRecruitId == null) {
throw new BaseException("状态不对");
}
recruitApplyIds.add(oneByUserIdAndRecruitId.getId());
if (firstLevelFile.isDirectory()) {
@ -250,20 +280,21 @@ public class UploadZipController {
}
}
if (CollectionUtil.isNotEmpty(insurance)) {
annexService.deleteByUserIdAndRecruitIdAndType(insurance, recruitId, "2",recruitApplyIds);
annexService.deleteByUserIdAndRecruitIdAndType(insurance, recruitId, "2", recruitApplyIds);
}
if (CollectionUtil.isNotEmpty(contract)) {
annexService.deleteByUserIdAndRecruitIdAndType(contract, recruitId, "1",recruitApplyIds);
annexService.deleteByUserIdAndRecruitIdAndType(contract, recruitId, "1", recruitApplyIds);
}
}
private void moveFilesToSaveDir(File sourceDir, Long recruitId,String username) throws IOException {
private void moveFilesToSaveDir(File sourceDir, Long recruitId, String username) throws IOException {
// 移动到 SAVE_DIR
File saveDestDir = new File(SAVE_DIR);
ensureDirectoryExists(saveDestDir);
BgtProjectRecruit recruit = recruitService.queryById(recruitId);
String firstLevelFolderName = recruit.getId() + "_" + recruit.getRecruitName();
String s = DigestUtil.md5Hex(recruit.getRecruitName());
String firstLevelFolderName = recruit.getId() + "_" + s;
File firstLevelDestDir = new File(saveDestDir, firstLevelFolderName);
ensureDirectoryExists(firstLevelDestDir);
@ -274,24 +305,24 @@ public class UploadZipController {
if (firstLevelFile.isDirectory()) {
String firstLevelName = firstLevelFile.getName(); //解压目录
File secondLevelDestDir = new File(firstLevelDestDir, firstLevelName); //保存目录
File secondLevelDestDir = new File(firstLevelDestDir, firstLevelName); //保存目录
File[] secondLevelFiles = firstLevelFile.listFiles();
if (secondLevelFiles != null) {
for (File secondLevelfile : secondLevelFiles) {
if (secondLevelfile.isDirectory()) {
String secondLevelName = secondLevelfile.getName(); //解压目录
File thirdLevelDestDir = new File(secondLevelDestDir, secondLevelName); //保存目录
File thirdLevelDestDir = new File(secondLevelDestDir, secondLevelName); //保存目录
File[] thirdLevelFiles = secondLevelfile.listFiles();
if(thirdLevelFiles != null && thirdLevelFiles.length > 0){
if (thirdLevelFiles != null && thirdLevelFiles.length > 0) {
deleteFolder(thirdLevelDestDir);
}
}
}
}
moveFilesRecursively(firstLevelFile, firstLevelDestDir, annexList, recruitId,username);
moveFilesRecursively(firstLevelFile, firstLevelDestDir, annexList, recruitId, username);
}
}
}
@ -300,14 +331,14 @@ public class UploadZipController {
}
}
private void moveFilesRecursively(File source, File destination, List<Annex> annexList, Long recruitId,String username) throws IOException {
private void moveFilesRecursively(File source, File destination, List<Annex> annexList, Long recruitId, String username) throws IOException {
if (source.isDirectory()) {
File newDir = new File(destination, source.getName());
ensureDirectoryExists(newDir);
File[] files = source.listFiles();
if (files != null) {
for (File file : files) {
moveFilesRecursively(file, newDir, annexList, recruitId,username);
moveFilesRecursively(file, newDir, annexList, recruitId, username);
}
}
} else {