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

This commit is contained in:
sungjin.choi
2026-07-20 16:17:20 +09:00
parent 618f1e714c
commit 12ab492a71
@@ -3,53 +3,58 @@ package company.specialsource.controller;
import jakarta.servlet.http.HttpServletRequest; import jakarta.servlet.http.HttpServletRequest;
import org.springframework.http.ResponseEntity; import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController; import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import java.io.InputStream;
import java.io.FileOutputStream;
import java.io.File; import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
@RestController @RestController
public class FileSyncController { public class FileSyncController {
// 🟢 @RequestParam을 추가하여 마이크로드로이드가 보내는 'filename' 값을 전달받습니다.
@PostMapping("/api/sync/upload") @PostMapping("/api/sync/upload")
public ResponseEntity<?> uploadFile(HttpServletRequest request) { public ResponseEntity<?> uploadFile(
String contentType = request.getContentType(); HttpServletRequest request,
@RequestParam(value = "filename", required = false) String originalFileName) {
try { try {
// 1. 기존 웹 프론트엔드(Vue)에서 보낸 폼 데이터(Multipart) 처리 // 1. 저장할 파일명 결정 로직
if (contentType != null && contentType.startsWith("multipart/form-data")) { String fileName;
MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request; if (originalFileName != null && !originalFileName.isEmpty()) {
MultipartFile multipartFile = multipartRequest.getFile("file"); // 마이크로드로이드가 이름을 보내준 경우 그 이름(확장자 포함)을 그대로 사용
fileName = originalFileName;
if (multipartFile != null && !multipartFile.isEmpty()) { } else {
// 💾 기존에 사용하시던 파일 저장 로직을 여기에 그대로 수행하면 됩니다. // 이름을 찾지 못한 경우의 예비용 이름 (확장자를 알 수 없으므로 .dat로 임시 지정)
// 예: multipartFile.transferTo(new File("기존 저장 경로")); fileName = "unknown_file_" + System.currentTimeMillis() + ".dat";
System.out.println("웹 프론트엔드로부터 멀티파트 파일 수신 완료: " + multipartFile.getOriginalFilename());
}
}
// 2. MacroDroid 매크로에서 보낸 순수 바이너리 스트림(Octet-Stream 등) 처리
else {
// 저장될 임시 파일명 정의 (타임스탬프 조합)
String fileName = "macro_sync_" + System.currentTimeMillis() + ".jpg";
// ⚠️ 유저님의 나스 환경에 맞는 실제 업로드 절대 경로를 입력해 주세요.
File targetFile = new File("/volume1/docker/upload_dir/" + fileName);
try (InputStream is = request.getInputStream();
FileOutputStream fos = new FileOutputStream(targetFile)) {
byte[] buffer = new byte[4096];
int bytesRead;
while ((bytesRead = is.read(buffer)) != -1) {
fos.write(buffer, 0, bytesRead);
}
}
System.out.println("MacroDroid로부터 바이너리 파일 수신 및 저장 완료: " + fileName);
} }
return ResponseEntity.ok("Upload Success"); // 2. 도커 내부의 상대 경로로 폴더 지정
String uploadDirPath = "./upload/";
File uploadDir = new File(uploadDirPath);
// 3. 폴더가 존재하지 않으면 자동으로 생성
if (!uploadDir.exists()) {
uploadDir.mkdirs();
}
// 4. 결정된 파일명으로 최종 저장 경로 생성
File targetFile = new File(uploadDir, fileName);
// 5. 파일 데이터 수신 및 쓰기
try (InputStream is = request.getInputStream();
FileOutputStream fos = new FileOutputStream(targetFile)) {
byte[] buffer = new byte[4096];
int bytesRead;
while ((bytesRead = is.read(buffer)) != -1) {
fos.write(buffer, 0, bytesRead);
}
}
System.out.println("✅ 파일 업로드 성공: " + targetFile.getAbsolutePath());
return ResponseEntity.ok("Upload Success: " + fileName);
} catch (Exception e) { } catch (Exception e) {
e.printStackTrace(); e.printStackTrace();