.yml
file:
upload-dir: src/main/resources/static/profileIMG
Controller
@PostMapping("/signup")
public ResponseEntity<String> handleFileUpload(@RequestParam("profile_img_path") MultipartFile file,
@RequestParam("id") String id,
@RequestParam("password") String password,
@RequestParam("name") String name,
@RequestParam("nickname") String nickname,
@RequestParam("age") int age,
@RequestParam("gender") String gender,
@RequestParam("tel") String tel) {
MemberDTO memberDTO = new MemberDTO();
memberDTO.setId(id);
memberDTO.setPwd(password);
memberDTO.setName(name);
memberDTO.setNickname(nickname);
memberDTO.setAge(age);
memberDTO.setGender(gender);
memberDTO.setTel(tel);
// 파일이 비어있는지 확인
if (file.isEmpty()) {
return ResponseEntity.status(HttpStatus.BAD_REQUEST).body("파일을 선택해 주세요");
}
try {
// 절대 경로 설정
Path currentPath = Paths.get(System.getProperty("user.dir")).toAbsolutePath();
Path uploadPath = currentPath.resolve("src/main/resources/static").resolve(uploadDir);
if (!Files.exists(uploadPath)) {
Files.createDirectories(uploadPath);
}
// 고유한 파일명 생성
String uniqueFileName = UUID.randomUUID().toString().substring(0, 8) + "_" + file.getOriginalFilename();
Path filePath = uploadPath.resolve(uniqueFileName);
// 디버깅을 위한 경로 출력
System.out.println("파일 저장 경로: " + filePath.toString());
Files.copy(file.getInputStream(), filePath);
// 파일명을 MemberDTO에 설정
memberDTO.setProfileIMG(uniqueFileName);
// 회원 정보와 파일 이름을 데이터베이스에 저장
memberService.insertMember(memberDTO);
} catch (IOException e) {
e.printStackTrace();
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("파일 업로드 실패");
}
return ResponseEntity.ok("회원 가입 완료");
}
DB is saved and the path output becomes normal. And it prints out that the save was successful.
But the file is not saved.
What’s the problem?
I am a beginner.
I’m a talking potato 🙁
I don’t know what to touch.
I would appreciate it if you wrote it in an easy-to-understand way.
I am a beginner.
Thank you for reading the article.
New contributor
Shin-Hyeoncheol is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.