ํ‹ฐ์Šคํ† ๋ฆฌ ๋ทฐ

๋ฐ˜์‘ํ˜•

Controller


PostsController

//์—…๋กœ๋“œ ์‚ฌ์ง„ ํŒŒ์ผ ๋ถˆ๋Ÿฌ์˜ค๊ธฐ ์š”์ฒญ
@RequestMapping("/file/{postNo}")
public ResponseEntity<byte[]> getFile(@PathVariable int postNo) throws Exception {
	
	logger.info("/post/file/" + postNo + " -> POST ์š”์ฒญ ๋ฐœ์ƒ ! " );
	PostsVO file = postsService.getFile(postNo);
	logger.info("postNo๋กœ ์กฐํšŒํ•œ ํŒŒ์ผ file : " + file);
	
	//ํŒŒ์ผ์„ ํด๋ผ์ด์–ธํŠธ๋กœ ์ „์†กํ•˜๊ธฐ ์œ„ํ•ด ์ „์†ก์ •๋ณด๋ฅผ ๋‹ด์„ ํ—ค๋”๋ฅผ ์„ค์ •ํ•œ๋‹ค.
	HttpHeaders headers = new HttpHeaders();
	String[] ftypes = file.getFileContentType().split("/");
	
	//์ „์†กํ—ค๋”์— ํŒŒ์ผ ์ •๋ณด์™€ ํ™•์žฅ์ž๋ฅผ ์…‹ํŒ…ํ•œ๋‹ค.
	headers.setContentType(new MediaType(ftypes[0], ftypes[1]));
	
	//์ „์†กํ—ค๋”์— ํŒŒ์ผ ์šฉ๋Ÿ‰์„ ์…‹ํŒ…ํ•œ๋‹ค.
	headers.setContentLength(file.getFileSize());
	
	//์ „์†กํ—ค๋”์— ํŒŒ์ผ๋ช…์„ ์…‹ํŒ…ํ•œ๋‹ค.
	headers.setContentDispositionFormData("attachment", file.getFileName());
	
	return new ResponseEntity<byte[]>(file.getFileData(), headers, HttpStatus.OK);
}

//ํ”„๋กœํ•„ ์‚ฌ์ง„ ํŒŒ์ผ ๋ถˆ๋Ÿฌ์˜ค๊ธฐ ์š”์ฒญ
@RequestMapping("/profile/{userNo}")
public ResponseEntity<byte[]> getProfile(@PathVariable int userNo) throws Exception {
	
	logger.info("/post/profile/" + userNo + " -> POST ์š”์ฒญ ๋ฐœ์ƒ ! " );
	
	UserImgsVO profile = usersService.getProfile(userNo);
	logger.info("userNo๋กœ ์กฐํšŒํ•œ ํŒŒ์ผ profile : " + profile);
	
	//ํŒŒ์ผ์„ ํด๋ผ์ด์–ธํŠธ๋กœ ์ „์†กํ•˜๊ธฐ ์œ„ํ•ด ์ „์†ก์ •๋ณด๋ฅผ ๋‹ด์„ ํ—ค๋”๋ฅผ ์„ค์ •ํ•œ๋‹ค.
	HttpHeaders headers = new HttpHeaders();
	String[] ftypes = profile.getProfileContentType().split("/");
	
	//์ „์†กํ—ค๋”์— ํŒŒ์ผ ์ •๋ณด์™€ ํ™•์žฅ์ž๋ฅผ ์…‹ํŒ…ํ•œ๋‹ค.
	headers.setContentType(new MediaType(ftypes[0], ftypes[1]));
	
	//์ „์†กํ—ค๋”์— ํŒŒ์ผ ์šฉ๋Ÿ‰์„ ์…‹ํŒ…ํ•œ๋‹ค.
	headers.setContentLength(profile.getProfileSize());
	
	//์ „์†กํ—ค๋”์— ํŒŒ์ผ๋ช…์„ ์…‹ํŒ…ํ•œ๋‹ค.
	headers.setContentDispositionFormData("attachment", profile.getProfileName());
	
	return new ResponseEntity<byte[]>(profile.getProfileData(), headers, HttpStatus.OK);
		
}

๋ถˆ๋Ÿฌ์™€์•ผํ•˜๋Š” ์‚ฌ์ง„ ์ •๋ณด๊ฐ€ ๋‘๊ฐ€์ง€๊ฐ€ ์žˆ๋‹ค. ํ”„๋กœํ•„ ์‚ฌ์ง„ ์ •๋ณด์™€ ๊ฒŒ์‹œ๊ธ€ ์ •๋ณด

๊ฒŒ์‹œ๊ธ€์„ ๋ณด์—ฌ์•ผํ•˜๋Š” ํŽ˜์ด์ง€๋Š” list.jsp(๋ชจ๋“  ๊ฒŒ์‹œ๋ฌผ์ด ๋ณด์—ฌ์ง€๋Š” ๊ณณ)์™€ personal-list.jsp(ํŠน์ • ์œ ์ €์˜ ๊ฒŒ์‹œ๋ฌผ์ด ๋ณด์—ฌ์ง€๋Š” ๊ณณ)์œผ๋กœ ๋‘ ๊ฐ€์ง€๊ฐ€ ์žˆ๋‹ค. 

//list.jsp ํŽ˜์ด์ง€ ์—ด๋žŒ ์š”์ฒญ
@RequestMapping(value="/list", method=RequestMethod.GET)
public String list(Model model) throws Exception {
		
	//๋ชจ๋“ ๊ฒŒ์‹œ๋ฌผ ๊ฐ€์ ธ์™€์„œ ์ด๋ฆ„ post ๋ชจ๋ธ์— ๋‹ด์•„ ๋ณด๋‚ธ๋‹ค
	model.addAttribute("post", postsService.selectPostList());
	
	
	return "post/list";
}

//personal-list ์š”์ฒญ : ๊ฐœ์ธ ๊ฒŒ์‹œ๋ฌผ ๋ณด์ด๋Š” ํŽ˜์ด์ง€
@RequestMapping(value="/{id}", method=RequestMethod.GET)
public String personalList(@PathVariable String id, Model model, HttpSession session) throws Exception {
	
	
	//๊ฐœ์ธ ํŽ˜์ด์ง€ ์ฃผ์ธ ์ •๋ณด ๋‹ด๊ธฐ
	UsersVO user = usersService.inquiryOfUserById(id);
	//๋กœ๊ทธ์ธํ•œ ํšŒ์› (=๋‚˜) ์ •๋ณด ๋‹ด๊ธฐ
	Object object = session.getAttribute("login");
	UsersVO loginUser = (UsersVO)object;
	

	//๊ฐœ์ธํŽ˜์ด์ง€์˜ ์œ ์ € ๋ฒˆํ˜ธ ๊ฐ€์ ธ์˜ค๊ธฐ
	int userNo = user.getUserNo();
	//๋กœ๊ทธ์ธ ํšŒ์› ์œ ์ € ๋ฒˆํ˜ธ ๊ฐ€์ ธ์˜ค๊ธฐ
	int loginUserNo = loginUser.getUserNo();
	logger.info("ํ˜„์žฌ ํŽ˜์ด์ง€ ์ฃผ์ธ์˜ ๋ฒˆํ˜ธ : " + userNo + ", ๋กœ๊ทธ์ธ ์œ ์ €์˜ ๋ฒˆํ˜ธ : " + loginUserNo);
	
	
	
	//์‚ฌ์šฉ์ž ์•„์ด๋””๋กœ ์‚ฌ์šฉ์ž ๋ฒˆํ˜ธ(pk)๋ฅผ ์กฐํšŒํ•ด์„œ ๊ทธ ๋ฒˆํ˜ธ๋กœ ๊ฒŒ์‹œ๋ฌผ ๊ฐ€์ ธ์˜ค๊ธฐ
	model.addAttribute("post", postsService.selectPostListById(id));
	//์‚ฌ์šฉ์ž ์•„์ด๋””๋กœ ํšŒ์›์˜ ๋ชจ๋“  ์ •๋ณด ์กฐํšŒํ•˜๊ธฐ
	model.addAttribute("user", user);

	
	
	return "post/personal-list";
}

์ „์ฒด ๊ฒŒ์‹œ๋ฌผ์ด ๋ณด์—ฌ์ง€๋Š” ํŽ˜์ด์ง€์—์„œ๋Š” ๊ฐ„๋‹จํ•˜๊ฒŒ ๋ชจ๋“  ๊ฒŒ์‹œ๋ฌผ์„ ๊ฐ€์ ธ์™€์„œ ๋ชจ๋ธ์— ๋‹ด์•„ ๋ณด๋‚ด๋ฉด ๋˜์ง€๋งŒ ํŠน์ • ์œ ์ €์˜ ๊ฒŒ์‹œ๋ฌผ์ด ๋ณด์—ฌ์ง€๋Š” ํŽ˜์ด์ง€๋Š” view์—์„œ ์œ ์ €์˜ id ์ •๋ณด๋ฅผ ๋ฐ›์•„ ๊ทธ ์ •๋ณด๋กœ ํ•ด๋‹น ํšŒ์›์˜ ์ •๋ณด๋ฅผ ๋‹ด๋Š”๋‹ค. ๋‹ค๋ฅธ ์ •๋ณด์— ๋”ฐ๋ผ ๋‹ค๋ฅด๊ฒŒ ํ‘œ์‹œ๊ฐ€ ๋˜๋Š” ๊ฒฝ์šฐ ์š”์ฒญ url์„ { } ๊ด„ํ˜ธ์•ˆ์— ์ž‘์„ฑํ•˜๋„๋ก ํ•œ๋‹ค. ๊ฐ€์ ธ์˜จ ๊ฒŒ์‹œ๋ฌผ์˜ ์ •๋ณด๋ฅผ ๋ชจ๋ธ(post)์— ๋‹ด์•„์„œ ๋ณด๋‚ธ๋‹ค.

 

 

Service


PostsService

//๊ฒŒ์‹œ๋ฌผ ๋ชฉ๋ก ์กฐํšŒ ๊ธฐ๋Šฅ(๋ชจ๋“  ์‚ฌ์ง„)
@Override
public List<PostsVO> selectPostList() throws Exception {
	
	return postsDAO.selectPostList();
}
//์•„์ด๋””๋กœ ํšŒ์›๋ฒˆํ˜ธ ์กฐํšŒํ›„ ๋ฒˆํ˜ธ๋กœ ๊ฒŒ์‹œ๋ฌผ ๋ถˆ๋Ÿฌ์˜ค๊ธฐ
@Override
public List<PostsVO> selectPostListById(String id) throws Exception {
	return postsDAO.selectPostListById(id);
}
//์ฒจ๋ถ€ํŒŒ์ผ์„ DB์—์„œ ๋ถˆ๋Ÿฌ์˜ค๋Š” ๊ธฐ๋Šฅ
@Override
public PostsVO getFile(int postNo) throws Exception {
	return postsDAO.getFile(postNo);
}

 

 

 

 

View


list.jsp ํŽ˜์ด์ง€๋กœ ์ด๋™์‹œ
personal-list.jsp ๊ฐœ์ธ ํŽ˜์ด์ง€ - ๋กœ๊ทธ์ธ ์œ ์ €๋ž‘ ๊ฐ™์„ ์‹œ
personal-list.jsp ๊ฐœ์ธ ํŽ˜์ด์ง€ - ๋กœ๊ทธ์ธ ์œ ์ €๋ž‘ ๋‹ค๋ฅผ ์‹œ

 

๋ฐ˜์‘ํ˜•
๋Œ“๊ธ€