@GetMapping("/write")
public String write(@ModelAttribute("writeContentBean") ContentBean writeContentBean, @RequestParam("board_info_idx") int board_info_idx) {
writeContentBean.setContent_board_idx(board_info_idx);
return "board/write";
}
<form:hidden path="content_board_idx"/>
@Service
public class BoardService {
public void addContentInfo(ContentBean writeContentBean) {
System.out.println(writeContentBean.getContent_subject());
System.out.println(writeContentBean.getContent_text());
System.out.println(writeContentBean.getUpload_file());
}
}
@Autowired
private BoardService boardService;
@PostMapping("/write_pro")
//생략....
//db에 저장
boardService.addContentInfo(writeContentBean);
글쓰기부분을 입력하면 콘솔창에 출력되는걸 확인할 수 있다.
resource디렉토리안에 upload폴더 추가
폴더추가한걸 properties에 경로를 입력해준다.
path.upload = //Users//jeongsujin//springProject//.metadata//.plugins//org.eclipse.wst.server.core//tmp0/wtpwebapps//MiniProject//resources//upload
page.listcnt = 10
page.paginationcnt = 10
@Service
@PropertySource("/WEB-INF/properties/option.properties") //경로설정
public class BoardService {
@Value("${path.upload}")
private String path_upload;
//서버로 업로드 된 파일을 업로드 폴더에 저장하고 파일의 이름을 리턴하는 메서드
private String saveUploadFile(MultipartFile upload_file) {
//현재시간(밀리세컨드)을 이용해서 파일의 이름이 중복되지 않게 설정
String file_name = System.currentTimeMillis() + "_" + upload_file.getOriginalFilename();
try {
upload_file.transferTo(new File(path_upload + "/" + file_name));
}catch(Exception e) {
e.printStackTrace();
}
return file_name;
}
public void addContentInfo(ContentBean writeContentBean) {
MultipartFile upload_file = writeContentBean.getUpload_file();
if(upload_file.getSize() > 0 ) {
String file_name = saveUploadFile(upload_file);
writeContentBean.setContent_file(file_name);
}
// System.out.println(writeContentBean.getContent_subject());
// System.out.println(writeContentBean.getContent_text());
// System.out.println(writeContentBean.getUpload_file());
}
}
upload파일에 이미지가 잘 들어간걸 볼 수 있다.!