롯데ON 커뮤니티는 공지사항, 자유게시판, 자료공유, 묻고답하기 4개 카테고리별 게시판을 지원합니다.
사용자가 카테고리를 선택해 글을 작성하면, 선택된 게시판에 맞는 저장 로직과 파일 업로드가 자동으로 처리되며 작성자 IP도 함께 저장됩니다.
[Controller]
└─ GET /Community/write{1~4} → 작성 폼 이동
└─ POST /Community/write{1~4}
├─ 사용자 IP 추출
├─ Multipart 파일 업로드 처리
├─ 게시글 저장 → 글 번호 반환
└─ 파일 메타데이터 DB 저장
[Service]
└─ register1(), register2() → DTO → Entity 변환 + User 관계 설정
└─ FileService.uploadFile1()/uploadFile2() → 저장 디렉토리 + UUID 파일명 생성
└─ FileService.save() → FileDTO → File Entity 변환 및 저장
@PostMapping("/Community/write1")
public String write1(Community1DTO communityDTO, HttpServletRequest req) {
String regip = req.getRemoteAddr();
communityDTO.setRegip(regip); // 사용자 IP 저장
List<FileDTO> files = fileService.uploadFile1(communityDTO); // 파일 업로드
communityDTO.setFile(files.size()); // 첨부파일 수 저장
int no = communityService.register1(communityDTO); // 게시글 저장
for(FileDTO fileDTO : files) {
fileDTO.setAno(no); // 파일의 외래키(게시글 번호) 설정
fileService.save(fileDTO); // 파일 메타데이터 저장
}
return "redirect:/Community/" + communityDTO.getCate(); // 카테고리별 목록으로 이동
}
public int register1(Community1DTO communityDTO) {
User user = User.builder().id(communityDTO.getWriter()).build();
Community1 community = modelMapper.map(communityDTO, Community1.class);
community.setUser(user);
return community1Repository.save(community).getNo(); // 글 번호 반환
}
@Value("${spring.servlet.multipart.location}")
private String uploadDir;
uploadFile1()public List<FileDTO> uploadFile1(Community1DTO community1DTO) {
java.io.File fileUploadDir = new java.io.File(uploadDir);
if (!fileUploadDir.exists()) fileUploadDir.mkdirs(); // 디렉토리 생성
List<MultipartFile> multipartFiles = community1DTO.getMultipartFiles();
List<FileDTO> fileDTOList = new ArrayList<>();
for (MultipartFile multipartFile : multipartFiles) {
if (!multipartFile.isEmpty()) {
String oName = multipartFile.getOriginalFilename();
String ext = oName.substring(oName.lastIndexOf("."));
String sName = UUID.randomUUID().toString() + ext;
try {
multipartFile.transferTo(new java.io.File(fileUploadDir, sName));
} catch (IOException e) {
log.error(e.getMessage());
}
FileDTO fileDTO = FileDTO.builder()
.oName(oName)
.sName(sName)
.build();
fileDTOList.add(fileDTO);
}
}
return fileDTOList;
}
uploadFile2()도 동일한 구조