fix - 파일 업로드 수정
SpecialSource Backend CI/CD / build-and-deploy (push) Successful in 52s

This commit is contained in:
sungjin.choi
2026-07-20 16:25:23 +09:00
parent 12ab492a71
commit 0536a7fd89
@@ -13,36 +13,30 @@ import java.io.InputStream;
@RestController
public class FileSyncController {
// 🟢 @RequestParam을 추가하여 마이크로드로이드가 보내는 'filename' 값을 전달받습니다.
@PostMapping("/api/sync/upload")
public ResponseEntity<?> uploadFile(
HttpServletRequest request,
@RequestParam(value = "filename", required = false) String originalFileName) {
try {
// 1. 저장할 파일명 결정 로직
String fileName;
if (originalFileName != null && !originalFileName.isEmpty()) {
// 마이크로드로이드가 이름을 보내준 경우 그 이름(확장자 포함)을 그대로 사용
fileName = originalFileName;
} else {
// 이름을 찾지 못한 경우의 예비용 이름 (확장자를 알 수 없으므로 .dat로 임시 지정)
fileName = "unknown_file_" + System.currentTimeMillis() + ".dat";
// 1. 🟢 파일명 검증: 원래 명칭이 비어있거나 누락되었다면 요청을 거부합니다.
if (originalFileName == null || originalFileName.trim().isEmpty()) {
return ResponseEntity.badRequest().body("Error: 원래 파일명이 전달되지 않았습니다.");
}
// 2. 도커 내부의 상대 경로로 폴더 지
String uploadDirPath = "./upload/";
// 2. 유저님이 지정하신 나스의 실제 절대 경로 고
String uploadDirPath = "/volume1/docker/upload_dir/";
File uploadDir = new File(uploadDirPath);
// 3. 폴더가 존재하지 않으면 자동으로 생성
// 디렉토리가 없으면 안전하게 자동 생성
if (!uploadDir.exists()) {
uploadDir.mkdirs();
}
// 4. 결정된 파일명으로 최종 저장 경로 생성
File targetFile = new File(uploadDir, fileName);
// 3. 🟢 파일명 가공 없이 원래 명칭 그대로 저장 파일 객체 정의
File targetFile = new File(uploadDir, originalFileName);
// 5. 파일 데이터 수신 및 쓰기
// 4. 바이너리 스트림을 읽어 파일 작성 진행
try (InputStream is = request.getInputStream();
FileOutputStream fos = new FileOutputStream(targetFile)) {
@@ -53,8 +47,8 @@ public class FileSyncController {
}
}
System.out.println("파일 업로드 성공: " + targetFile.getAbsolutePath());
return ResponseEntity.ok("Upload Success: " + fileName);
System.out.println("원래 명칭 그대로 업로드 완료: " + targetFile.getAbsolutePath());
return ResponseEntity.ok("Upload Success: " + originalFileName);
} catch (Exception e) {
e.printStackTrace();