优化
This commit is contained in:
@ -30,7 +30,7 @@ import org.springframework.web.multipart.MultipartFile;
|
|||||||
|
|
||||||
import javax.validation.constraints.NotNull;
|
import javax.validation.constraints.NotNull;
|
||||||
import java.io.*;
|
import java.io.*;
|
||||||
import java.nio.charset.StandardCharsets;
|
import java.nio.charset.Charset;
|
||||||
import java.time.LocalDateTime;
|
import java.time.LocalDateTime;
|
||||||
import java.time.format.DateTimeFormatter;
|
import java.time.format.DateTimeFormatter;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
@ -95,7 +95,7 @@ public class UploadZipController {
|
|||||||
// 保存上传的压缩文件
|
// 保存上传的压缩文件
|
||||||
String s = DigestUtil.md5Hex(recruit.getRecruitName());
|
String s = DigestUtil.md5Hex(recruit.getRecruitName());
|
||||||
firstLevelFolderName = recruitId + "_" + s;
|
firstLevelFolderName = recruitId + "_" + s;
|
||||||
File zipFile = new File(TEMP_DIR, firstLevelFolderName);
|
File zipFile = new File(TEMP_DIR, firstLevelFolderName + ".zip");
|
||||||
ensureDirectoryExists(zipFile.getParentFile());
|
ensureDirectoryExists(zipFile.getParentFile());
|
||||||
try (OutputStream os = new FileOutputStream(zipFile)) {
|
try (OutputStream os = new FileOutputStream(zipFile)) {
|
||||||
os.write(file.getBytes());
|
os.write(file.getBytes());
|
||||||
@ -120,6 +120,8 @@ public class UploadZipController {
|
|||||||
// 删除临时文件和文件夹
|
// 删除临时文件和文件夹
|
||||||
File extractDir = new File(TEMP_DIR, firstLevelFolderName);
|
File extractDir = new File(TEMP_DIR, firstLevelFolderName);
|
||||||
deleteFolder(extractDir);
|
deleteFolder(extractDir);
|
||||||
|
File zipFile = new File(TEMP_DIR, firstLevelFolderName + ".zip");
|
||||||
|
deleteFolder(zipFile);
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
return AjaxResult.error("文件处理过程中出现错误");
|
return AjaxResult.error("文件处理过程中出现错误");
|
||||||
}
|
}
|
||||||
@ -133,7 +135,8 @@ public class UploadZipController {
|
|||||||
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyyMMddHHmmss");
|
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyyMMddHHmmss");
|
||||||
String timeStamp = LocalDateTime.now().format(formatter);
|
String timeStamp = LocalDateTime.now().format(formatter);
|
||||||
BgtProjectRecruit recruit = recruitService.queryById(recruitId);
|
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);
|
File recordDestDir = new File(RECORD_DIR, firstLevelFolderName);
|
||||||
ensureDirectoryExists(recordDestDir);
|
ensureDirectoryExists(recordDestDir);
|
||||||
|
|
||||||
@ -164,13 +167,18 @@ public class UploadZipController {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private void extractZipFile(File zipFile, File extractDir) throws IOException {
|
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;
|
ZipEntry zipEntry;
|
||||||
while ((zipEntry = zis.getNextEntry()) != null) {
|
while ((zipEntry = zis.getNextEntry()) != null) {
|
||||||
File newFile = newFile(extractDir, zipEntry);
|
|
||||||
if (zipEntry.isDirectory()) {
|
if (zipEntry.isDirectory()) {
|
||||||
ensureDirectoryExists(newFile);
|
// 是目录,不重命名,直接创建目录
|
||||||
|
File newDir = new File(extractDir, zipEntry.getName());
|
||||||
|
ensureDirectoryExists(newDir);
|
||||||
} else {
|
} else {
|
||||||
|
// 是文件,进行重命名
|
||||||
|
String originalFileName = zipEntry.getName();
|
||||||
|
String newFileName = renameFile(originalFileName);
|
||||||
|
File newFile = new File(extractDir, newFileName);
|
||||||
// 为文件创建父目录
|
// 为文件创建父目录
|
||||||
ensureDirectoryExists(newFile.getParentFile());
|
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 {
|
private File newFile(File destinationDir, ZipEntry zipEntry) throws IOException {
|
||||||
@ -263,7 +293,8 @@ public class UploadZipController {
|
|||||||
ensureDirectoryExists(saveDestDir);
|
ensureDirectoryExists(saveDestDir);
|
||||||
|
|
||||||
BgtProjectRecruit recruit = recruitService.queryById(recruitId);
|
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);
|
File firstLevelDestDir = new File(saveDestDir, firstLevelFolderName);
|
||||||
ensureDirectoryExists(firstLevelDestDir);
|
ensureDirectoryExists(firstLevelDestDir);
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user