From 77518b3f97776e7f983d4b50687906e9bbb8d6fc Mon Sep 17 00:00:00 2001 From: liumangmang Date: Wed, 18 Mar 2026 23:46:05 +0800 Subject: [PATCH] =?UTF-8?q?Fix:=20=E4=BF=AE=E5=A4=8D=20Docker=20=E4=B8=8A?= =?UTF-8?q?=E4=BC=A0=E7=9B=AE=E5=BD=95=E8=A7=A3=E6=9E=90=E9=94=99=E8=AF=AF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 将 multipart 上传目录改为基于 DATA_DIR 的绝对路径,避免 Tomcat 在容器内把相对路径解析到临时目录。同步让上传控制器复用该配置并补充错误日志,确保本地文件在异步上传期间可用。 --- .../com/sshmanager/controller/SftpController.java | 15 +++++++++++---- backend/src/main/resources/application.yml | 2 +- docker/Dockerfile | 4 ++-- 3 files changed, 14 insertions(+), 7 deletions(-) diff --git a/backend/src/main/java/com/sshmanager/controller/SftpController.java b/backend/src/main/java/com/sshmanager/controller/SftpController.java index 6800891..7b21da6 100644 --- a/backend/src/main/java/com/sshmanager/controller/SftpController.java +++ b/backend/src/main/java/com/sshmanager/controller/SftpController.java @@ -9,6 +9,7 @@ import com.sshmanager.service.ConnectionService; import com.sshmanager.service.SftpService; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import org.springframework.beans.factory.annotation.Value; import org.springframework.http.HttpHeaders; import org.springframework.http.MediaType; import org.springframework.http.ResponseEntity; @@ -43,6 +44,7 @@ public class SftpController { private final ConnectionService connectionService; private final UserRepository userRepository; private final SftpService sftpService; + private final String uploadTempLocation; private final Map sessions = new ConcurrentHashMap<>(); private final Map sessionLocks = new ConcurrentHashMap<>(); @@ -53,10 +55,12 @@ public class SftpController { public SftpController(ConnectionService connectionService, UserRepository userRepository, - SftpService sftpService) { + SftpService sftpService, + @Value("${spring.servlet.multipart.location:/app/data/upload-temp}") String uploadTempLocation) { this.connectionService = connectionService; this.userRepository = userRepository; this.sftpService = sftpService; + this.uploadTempLocation = uploadTempLocation; } private Long getCurrentUserId(Authentication auth) { @@ -297,9 +301,9 @@ public class SftpController { String taskKey = uploadTaskKey(userId, taskId); // Save file to persistent location before async processing - java.io.File uploadTempDir = new java.io.File("./data/upload-temp"); - if (!uploadTempDir.exists()) { - uploadTempDir.mkdirs(); + java.io.File uploadTempDir = new java.io.File(uploadTempLocation); + if (!uploadTempDir.exists() && !uploadTempDir.mkdirs()) { + throw new IOException("Failed to create upload temp directory: " + uploadTempDir.getAbsolutePath()); } tempFile = new java.io.File(uploadTempDir, taskId + "_" + file.getOriginalFilename()); file.transferTo(tempFile); @@ -352,6 +356,8 @@ public class SftpController { } }); } catch (Exception e) { + log.error("Async upload failed, taskId={}, connectionId={}, tempFile={}", + taskId, connectionId, savedFile.getAbsolutePath(), e); status.markError(e.getMessage() != null ? e.getMessage() : "Upload failed"); // Clean up temp file on error if (savedFile.exists()) { @@ -366,6 +372,7 @@ public class SftpController { result.put("message", "Upload started"); return ResponseEntity.ok(result); } catch (Exception e) { + log.error("Failed to prepare upload temp file, connectionId={}", connectionId, e); // Clean up temp file if initial save failed if (tempFile != null && tempFile.exists()) { tempFile.delete(); diff --git a/backend/src/main/resources/application.yml b/backend/src/main/resources/application.yml index f0c1db3..f1c97c8 100644 --- a/backend/src/main/resources/application.yml +++ b/backend/src/main/resources/application.yml @@ -9,7 +9,7 @@ spring: multipart: max-file-size: 2048MB max-request-size: 2048MB - location: ./data/upload-temp # 持久化临时目录,避免 Docker 容器重启丢失 + location: ${DATA_DIR:/app/data}/upload-temp # 使用容器数据目录,避免被解析为 Tomcat 工作目录 file-size-threshold: 0 # 立即写入磁盘,不使用内存缓冲 datasource: url: jdbc:h2:file:./data/sshmanager;DB_CLOSE_DELAY=-1 diff --git a/docker/Dockerfile b/docker/Dockerfile index fea8b30..9483347 100644 --- a/docker/Dockerfile +++ b/docker/Dockerfile @@ -40,8 +40,8 @@ WORKDIR /app COPY --from=backend /build/target/*.jar app.jar -ENV DATA_DIR=/app/data -RUN mkdir -p ${DATA_DIR} +ENV DATA_DIR=/app/data +RUN mkdir -p ${DATA_DIR}/upload-temp EXPOSE 48080