add(req-2080):生成临时文件名称

This commit is contained in:
胡朝飞 2024-01-22 14:56:03 +08:00
parent 8871d1ca75
commit c82b0e3d52

View File

@ -35,6 +35,8 @@ import java.io.FileOutputStream;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import java.io.OutputStream; import java.io.OutputStream;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.*; import java.util.*;
import java.util.concurrent.CompletableFuture; import java.util.concurrent.CompletableFuture;
import java.util.stream.Collectors; import java.util.stream.Collectors;
@ -642,17 +644,19 @@ public class FileServiceImpl implements FileService {
input = file.getInputStream(); input = file.getInputStream();
// 指定目标文件路径及文件名 // 指定目标文件路径及文件名
String fileName = file.getOriginalFilename(); String fileName = file.getOriginalFilename();
assert fileName != null;
String tempFileName = Utility.getUUID() + fileName.substring(fileName.lastIndexOf("."));
String osName = System.getProperty("os.name"); String osName = System.getProperty("os.name");
String rootDir = null; String rootDir;
if (Objects.nonNull(osName) && osName.toLowerCase().startsWith("linux")) { if (Objects.nonNull(osName) && osName.toLowerCase().startsWith("linux")) {
rootDir = "/mnt/team/obs"; rootDir = "/mnt/temp/obs";
} else { } else {
ClassLoader contextClassLoader = Thread.currentThread().getContextClassLoader(); ClassLoader contextClassLoader = Thread.currentThread().getContextClassLoader();
String classPath = Objects.requireNonNull(contextClassLoader.getResource("")).getPath(); String classPath = Objects.requireNonNull(contextClassLoader.getResource("")).getPath();
rootDir = classPath.substring(0, classPath.indexOf("/target/classes/")); rootDir = classPath.substring(0, classPath.indexOf("/target/classes/"));
} }
String filePath = rootDir + SEPARATOR + "temp_directory"; String filePath = rootDir + SEPARATOR + "temp_directory";
String absolutePath = filePath + SEPARATOR + fileName; String absolutePath = filePath + SEPARATOR + tempFileName;
// 创建临时文件夹 // 创建临时文件夹
java.io.File tempDirectory = new java.io.File(filePath); java.io.File tempDirectory = new java.io.File(filePath);
@ -663,17 +667,17 @@ public class FileServiceImpl implements FileService {
} else { } else {
log.info("临时文件夹创建失败,{}", flag); log.info("临时文件夹创建失败,{}", flag);
// 兜底 // 兜底
absolutePath = rootDir + SEPARATOR + fileName; absolutePath = rootDir + SEPARATOR + tempFileName;
} }
} }
if (!tempDirectory.exists()) { if (!tempDirectory.exists()) {
// 兜底 // 兜底
absolutePath = rootDir + SEPARATOR + fileName; absolutePath = rootDir + SEPARATOR + tempFileName;
} }
log.info("临时文件绝对路径:{}", absolutePath); log.info("临时文件绝对路径:{}", absolutePath);
// 创建输出流并写入数据 // 创建输出流并写入数据
OutputStream output = new FileOutputStream(absolutePath); OutputStream output = Files.newOutputStream(Paths.get(absolutePath));
byte[] buffer = new byte[1024]; byte[] buffer = new byte[1024];
int length; int length;
while ((length = input.read(buffer)) > 0) { while ((length = input.read(buffer)) > 0) {