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

๋ฐ˜์‘ํ˜•

# ํŒŒ์ผ ์—…๋กœ๋“œ๋ฅผ ํ•˜๋Š” 2๊ฐ€์ง€ ๋ฐฉ๋ฒ•

  1. ์›น์„œ๋ฒ„์— ์ €์žฅํ•˜๊ธฐ
  2. DB์— (byteํ˜•ํƒœ๋กœ) ์ €์žฅํ•˜๊ธฐ

์‹ค๋ฌด, ๋Œ€๊ทœ๋ชจ ์‚ฌ์ดํŠธ ๋“ฑ์—์„œ ๋งŽ์ด ์‚ฌ์šฉํ•˜๋Š” ๋‘๋ฒˆ์งธ ๋ฐฉ๋ฒ•์„ ์‚ฌ์šฉํ•œ๋‹ค.

 

 

# ํŒŒ์ผ ์—…๋กœ๋“œ๋ฅผ ์œ„ํ•ด์„œ ํ•ด์•ผํ•  ์ผ

1. pom.xml ์— ๋ชจ๋“ˆ ์ถ”๊ฐ€ commons-fileupload

<!-- ํŒŒ์ผ ์—…๋กœ๋“œ๋ฅผ ์œ„ํ•œ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ --> 
<dependency> 
	<groupId>commons-fileupload</groupId>
    <artifactId>commons-fileupload</artifactId>
    <version>1.3.1</version>
</dependency>

2. DB ์„ค์ • jdbc.properties ์—์„œ ๊ณ„์ • ๋น„๋ฐ€๋ฒˆํ˜ธ๊ฐ€ ๋งž๋Š”์ง€ ํ™•์ธ

3. ์„ค์ • ํŒŒ์ผ mvc-config.xml์—์„œ ํŒŒ์ผ ์—…๋กœ๋“œ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ ๋นˆ๋“ฑ๋ก ํ•˜๊ธฐ

<!-- [ํŒŒ์ผ ์—…๋กœ๋“œ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ ]-->
<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
	<property name="maxUploadSize"> 
    	<value>50000000</value> 
    </property>
</bean>

4. ํ…Œ์ด๋ธ” ์ƒ์„ฑ sql

 

 

ํ…Œ์ด๋ธ” ํ•„๋“œ

  • ํŒŒ์ผ ์‹๋ณ„ ์•„์ด๋”” user_img_no
  • ์–ด๋–ค ์œ ์ €์˜ ํŒŒ์ผ์ธ์ง€ user_no
  • ํ”„๋กœํ•„ ์˜ค๋ฆฌ์ง€๋„ ํŒŒ์ผ๋ช… profile_name
  • ํŒŒ์ผ ์‚ฌ์ด์ฆˆ profile_size -> ๋ฐ”์ดํŠธ ์ˆ˜๊ฐ€ ์–ผ๋งˆ์ธ๊ฐ€
  • ํŒŒ์ผ ํ™•์žฅ์ž profile_content_type
  • ์‹ค์ œ ํŒŒ์ผ ๋ฐ์ดํ„ฐ profile_data -> longblob(16๊ธฐ๊ฐ€๊นŒ์ง€ ์ €์žฅ๊ฐ€๋Šฅ)

 

 

 

 

Controller


UsersController

//ํ”„๋กœํ•„ ์‚ฌ์ง„ ๋“ฑ๋ก ์š”์ฒญ
@PostMapping("/profile/upload")
public ModelAndView photoUpload(UserImgsVO imgFile, HttpSession session) throws Exception {
	logger.info("/upload : Post์š”์ฒญ ๋ฐœ์ƒ!!!!");
	logger.info("ํŒŒ์ผ ํ™•์ธ : " + imgFile);
	
	
	MultipartFile mfile = imgFile.getFile();
	
	imgFile.setProfileName(mfile.getOriginalFilename());
	imgFile.setProfileSize(mfile.getSize());
	imgFile.setProfileContentType(mfile.getContentType());
	imgFile.setProfileData(mfile.getBytes());
	
	usersService.insertUserImg(imgFile);
	
	
	//์œ ์ € ๋ฒˆํ˜ธ๋กœ ํšŒ์› ์ •๋ณด ์ „๋ถ€ ์กฐํšŒ
	UsersVO modifyUser = usersService.inquiryOfUserByUserNo(imgFile.getUserNo());
	//๋‹ค์‹œ ๋กœ๊ทธ์ธ ์„ธ์…˜์— ์ €์žฅ
	session.setAttribute("login", modifyUser);
	//์„ธ์…˜ ํ™•์ธ
	System.out.println("์ˆ˜์ • ๋œ ๋กœ๊ทธ์ธ ์„ธ์…˜  : " + session.getAttribute("login"));
		
	return new ModelAndView ("redirect:/user/modify");
}

ํ”„๋ก ํŠธ์—์„œ ๋ฐ›์€ ํšŒ์› ์ด๋ฏธ์ง€ ์ •๋ณด๋ฅผ ๊ฐ€์ ธ์™€์„œ mfile์— ์ €์žฅ ํ›„ ๋‹ค์‹œ imsFile์— ๊ฐ’์„ ์ง€์ •ํ•ด์ค€๋‹ค.

mfile์ •๋ณด๋ฅผ ๋‹ค์‹œ ์ถ”์ถœํ•ด์„œ VO์— ์ฑ„์›Œ์ค€ ํ›„ ๋‹ค์‹œ DB์— ๋“ฑ๋กํ•œ๋‹ค.

์ •๋ณด๊ฐ€ ๋ณ€๊ฒฝ๋˜์—ˆ์œผ๋ฏ€๋กœ ๋กœ๊ทธ์ธ ์„ธ์…˜๋„ ๋ณ€๊ฒฝํ•ด์ค€๋‹ค. 

 

//ํŒŒ์ผ ๋ถˆ๋Ÿฌ์˜ค๊ธฐ ์š”์ฒญ
@RequestMapping("/profile/{userNo}")
public ResponseEntity<byte[]> getProfile(@PathVariable int userNo) throws Exception {
		
	logger.info("/user/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);
	
}

/profile/์œ ์ €์˜ ๋ฒˆํ˜ธ -> ์š”์ฒญ์ด ๋“ค์–ด์˜ค๋ฉด ํ”„๋กœํ•„์„ ์กฐํšŒํ•œ๋‹ค.

view์—์„œ ํ”„๋กœํ•„ ์‚ฌ์ง„ ์กฐํšŒ

 

//ํ”„๋กœํ•„ ์‚ฌ์ง„ ์‚ญ์ œ ์š”์ฒญ
@PostMapping("/profile/delete")
public ModelAndView photoDelete(int userNo, HttpSession session) throws Exception {
	
	logger.info("ํ”„๋กœํ•„ ์‚ฌ์ง„ ์‚ญ์ œ ์š”์ฒญ /profile/delete POST์š”์ฒญ");
	
	usersService.deleteUserImg(userNo);
	
	//์œ ์ € ๋ฒˆํ˜ธ๋กœ ํšŒ์› ์ •๋ณด ์ „๋ถ€ ์กฐํšŒ
	UsersVO modifyUser = usersService.inquiryOfUserByUserNo(userNo);
	//๋‹ค์‹œ ๋กœ๊ทธ์ธ ์„ธ์…˜์— ์ €์žฅ
	session.setAttribute("login", modifyUser);
	//์„ธ์…˜ ํ™•์ธ
	System.out.println("์ˆ˜์ • ๋œ ๋กœ๊ทธ์ธ ์„ธ์…˜  : " + session.getAttribute("login"));
	
	return new ModelAndView("redirect:/user/modify");
}

 

 

 

 

Service


UsersService

//ํ”„๋กœํ•„ ์‚ฌ์ง„ ๋“ฑ๋ก
@Transactional
@Override
public void insertUserImg(UserImgsVO file) throws Exception {
	//์‚ญ์ œ ํ•˜๊ณ  ๋“ฑ๋กํ•ด์•ผํ•จ!
	usersDAO.deleteUserImg(file.getUserNo());
	usersDAO.insertUserImg(file);
}
//์ฒจ๋ถ€ํŒŒ์ผ์„ DB์—์„œ ๋ถˆ๋Ÿฌ์˜ค๊ธฐ
@Override
public UserImgsVO getProfile(int userNo) throws Exception {
	return usersDAO.getProfile(userNo);
}
//ํ”„๋กœํ•„ ์‚ฌ์ง„ ์‚ญ์ œ
@Override
public void deleteUserImg(int userNo) throws Exception {
	usersDAO.deleteUserImg(userNo);
}

ํ”„๋กœํ•„ ์‚ฌ์ง„ ๋“ฑ๋ก์‹œ ๋“ฑ๋ก๋˜์–ด์žˆ๋Š” ์‚ฌ์ง„์„ ์‚ญ์ œํ•˜๊ณ  ๋“ฑ๋กํ•ด์•ผํ•œ๋‹ค -> ํŠธ๋žœ์ ์…˜(์—ฌ๋Ÿฌ๊ฐœ์˜ SQL์ด ์ž‘๋™ํ•  ๋•Œ ํ•˜๋‚˜์˜ ๋ฌถ์Œ, ํ•˜๋‚˜์˜ ๊ฑฐ๋ž˜๋กœ ์ธ์‹๋˜๋Š”๊ฒƒ. ์—ฌ๋Ÿฌ๊ฐœ์˜ SQL์ด ์ •์ƒ ์‹คํ–‰๋˜๋ฉด commit, ํ•˜๋‚˜๋ผ๋„ ์‹คํŒจํ•˜๋ฉด rollbackํ•œ๋‹ค.)

 


pom.xml ํŠธ๋žœ์ ์…˜ ๋ชจ๋“ˆ ์„ค์น˜ 

<!-- ์Šคํ”„๋ง tx ๋ชจ๋“ˆ -->
<dependency>
	<groupId>org.springframework</groupId>
	<artifactId>spring-tx</artifactId>
	<version>${org.springframework-version}</version>
</dependency>

mvc-config.cml ํŠธ๋žœ์ ์…˜ ์„ค์ •

<!-- [ํŠธ๋žœ์žญ์…˜ ๋งค๋‹ˆ์ € ๋นˆ ๋“ฑ๋ก] -->
<bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
	<property name="dataSource" ref="ds" />
</bean>

servlet-config.xml

<!-- ํŠธ๋žœ์žญ์…˜ ๊ด€๋ จ ์•„๋…ธํ…Œ์ด์…˜ ๋™์ž‘ ์„ค์ • -->
<tx:annotation-driven transaction-manager="txManager"/>

 

 

//์•„์ด๋””๋กœ ํšŒ์›์˜ ๋ชจ๋“  ์ •๋ณด ์กฐํšŒ
@Override
public UsersVO inquiryOfUserById(String id) throws Exception {
	return usersDAO.inquiryOfUserById(id);
}
//ํšŒ์›๋ฒˆํ˜ธ๋กœ ํšŒ์›์˜ ๋ชจ๋“  ์ •๋ณด ์กฐํšŒ
@Override
public UsersVO inquiryOfUserByUserNo(int userNo) throws Exception {
	return usersDAO.inquiryOfUserByUserNo(userNo);
}

 

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