From c82b0e3d525c0038761cc493ff2c633bd68df141 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=83=A1=E6=9C=9D=E9=A3=9E?= Date: Mon, 22 Jan 2024 14:56:03 +0800 Subject: [PATCH] =?UTF-8?q?add(req-2080):=E7=94=9F=E6=88=90=E4=B8=B4?= =?UTF-8?q?=E6=97=B6=E6=96=87=E4=BB=B6=E5=90=8D=E7=A7=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../axzo/oss/service/impl/FileServiceImpl.java | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/oss-service/src/main/java/cn/axzo/oss/service/impl/FileServiceImpl.java b/oss-service/src/main/java/cn/axzo/oss/service/impl/FileServiceImpl.java index 6f563ee..60ece14 100644 --- a/oss-service/src/main/java/cn/axzo/oss/service/impl/FileServiceImpl.java +++ b/oss-service/src/main/java/cn/axzo/oss/service/impl/FileServiceImpl.java @@ -35,6 +35,8 @@ import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; +import java.nio.file.Files; +import java.nio.file.Paths; import java.util.*; import java.util.concurrent.CompletableFuture; import java.util.stream.Collectors; @@ -642,17 +644,19 @@ public class FileServiceImpl implements FileService { input = file.getInputStream(); // 指定目标文件路径及文件名 String fileName = file.getOriginalFilename(); + assert fileName != null; + String tempFileName = Utility.getUUID() + fileName.substring(fileName.lastIndexOf(".")); String osName = System.getProperty("os.name"); - String rootDir = null; + String rootDir; if (Objects.nonNull(osName) && osName.toLowerCase().startsWith("linux")) { - rootDir = "/mnt/team/obs"; + rootDir = "/mnt/temp/obs"; } else { ClassLoader contextClassLoader = Thread.currentThread().getContextClassLoader(); String classPath = Objects.requireNonNull(contextClassLoader.getResource("")).getPath(); rootDir = classPath.substring(0, classPath.indexOf("/target/classes/")); } 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); @@ -663,17 +667,17 @@ public class FileServiceImpl implements FileService { } else { log.info("临时文件夹创建失败,{}", flag); // 兜底 - absolutePath = rootDir + SEPARATOR + fileName; + absolutePath = rootDir + SEPARATOR + tempFileName; } } if (!tempDirectory.exists()) { // 兜底 - absolutePath = rootDir + SEPARATOR + fileName; + absolutePath = rootDir + SEPARATOR + tempFileName; } log.info("临时文件绝对路径:{}", absolutePath); // 创建输出流并写入数据 - OutputStream output = new FileOutputStream(absolutePath); + OutputStream output = Files.newOutputStream(Paths.get(absolutePath)); byte[] buffer = new byte[1024]; int length; while ((length = input.read(buffer)) > 0) {