Skip to content

Commit 2794d85

Browse files
File Upload completion
1 parent 549b761 commit 2794d85

23 files changed

Lines changed: 337 additions & 4 deletions

File tree

‎pom.xml‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,20 @@
7171
<version>1.5.4</version>
7272
</dependency>
7373

74+
<!-- 파일 업로드 관련 라이브러리 -->
75+
<dependency>
76+
<groupId>commons-fileupload</groupId>
77+
<artifactId>commons-fileupload</artifactId>
78+
<version>1.3.1</version>
79+
</dependency>
80+
81+
<!-- 이미지 축소 라이브러리 -->
82+
<dependency>
83+
<groupId>org.imgscalr</groupId>
84+
<artifactId>imgscalr-lib</artifactId>
85+
<version>4.2</version>
86+
</dependency>
87+
7488
<!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java -->
7589
<dependency>
7690
<groupId>mysql</groupId>

‎src/main/java/com/jh/controller/ReplyController.java‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@ public class ReplyController {
3131
@RequestMapping(value="", method= RequestMethod.POST)
3232
public ResponseEntity<String> register(@RequestBody ReplyVO vo){
3333

34+
//RequestBody의 경우 JSON으로 전송된 데이터를 ReplyVO 타입의 객체로 변환해 주는 역활,
35+
//이때의 데이터는 일반적 데이터가 아닌 JSON으로 구성된 문자열 데이터이다.
3436
ResponseEntity<String> entity = null;
3537
try {
3638
service.addReply(vo);

‎src/main/java/com/jh/controller/SearchBoardController.java‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,19 @@
11
package com.jh.controller;
22

3+
import java.util.List;
4+
35
import javax.inject.Inject;
46

57
import org.slf4j.Logger;
68
import org.slf4j.LoggerFactory;
79
import org.springframework.stereotype.Controller;
810
import org.springframework.ui.Model;
911
import org.springframework.web.bind.annotation.ModelAttribute;
12+
import org.springframework.web.bind.annotation.PathVariable;
1013
import org.springframework.web.bind.annotation.RequestMapping;
1114
import org.springframework.web.bind.annotation.RequestMethod;
1215
import org.springframework.web.bind.annotation.RequestParam;
16+
import org.springframework.web.bind.annotation.ResponseBody;
1317
import org.springframework.web.servlet.mvc.support.RedirectAttributes;
1418

1519
import com.jh.service.BoardService;
@@ -133,4 +137,11 @@ public String registPOST(BoardVO board, RedirectAttributes rttr) throws Exceptio
133137

134138
return "redirect:/sboard/list";
135139
}
140+
141+
@RequestMapping(value="/getAttach/{bno}")
142+
@ResponseBody
143+
public List<String> getAttach(@PathVariable("bno")Integer bno)throws Exception{
144+
145+
return service.getAttach(bno);
146+
}
136147
}

‎src/main/java/com/jh/dao/BoardDAO.java‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,15 @@ public interface BoardDAO {
4545
//게시물 조회수 증가 메서드
4646
public void updateViewCnt(Integer bno) throws Exception;
4747

48+
//첨부파일 정보를 저장하는 기능
49+
public void addAttach(String fullName)throws Exception;
4850

51+
//특정 게시물의 첨부파일을 시간 순서대로 가져오는 기능
52+
public List<String> getAttach(Integer bno)throws Exception;
4953

54+
//게시물 수정시 업로드한 파일 삭제하는 기능
55+
public void delAttach(Integer bno)throws Exception;
56+
57+
//게시물 수정시 업로드한 파일 수정하는 기능
58+
public void replaceAttach(String fullName, Integer bno)throws Exception;
5059
}

‎src/main/java/com/jh/dao/BoardDAOImpl.java‎

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,4 +111,29 @@ public void updateViewCnt(Integer bno) throws Exception {
111111
session.update(namespace+".updateViewCnt", bno);
112112
}
113113

114+
@Override
115+
public void addAttach(String fullName) throws Exception {
116+
session.insert(namespace+".addAttach",fullName);
117+
118+
}
119+
@Override
120+
public List<String> getAttach(Integer bno) throws Exception {
121+
122+
return session.selectList(namespace+".getAttach",bno);
123+
}
124+
@Override
125+
public void delAttach(Integer bno) throws Exception {
126+
127+
session.delete(namespace+".deleteAttach",bno);
128+
}
129+
@Override
130+
public void replaceAttach(String fullName, Integer bno) throws Exception {
131+
132+
Map<String, Object> paramMap = new HashMap<String, Object>();
133+
134+
paramMap.put("fullName",fullName);
135+
paramMap.put("bno",bno);
136+
session.insert(namespace+".replaceAttach", paramMap);
137+
}
138+
114139
}

‎src/main/java/com/jh/service/BoardService.java‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,4 +25,6 @@ public interface BoardService {
2525
public List<BoardVO> listSearchCriteria(SearchCriteria cri)throws Exception;
2626

2727
public int listSearchCount(SearchCriteria cri)throws Exception;
28+
29+
public List<String> getAttach(Integer bno)throws Exception;
2830
}

‎src/main/java/com/jh/service/BoardServiceImpl.java‎

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,25 @@ public class BoardServiceImpl implements BoardService{
2020
@Inject
2121
private BoardDAO dao;
2222

23+
@Transactional
2324
@Override
2425
public void regist(BoardVO board)throws Exception{
26+
27+
/*
28+
* 게시물 등록 시, 게시물을 먼저 만들고
29+
* 첨부파일의 이름의 배열을 이용해서 각각의 파일 이름을 데이터베이스에 추가하는 형식
30+
*/
2531
dao.create(board);
32+
33+
String [] files = board.getFiles();
34+
35+
if(files == null) {
36+
return;
37+
}
38+
39+
for(String fileName: files) {
40+
dao.addAttach(fileName);
41+
}
2642
}
2743

2844
@Transactional(isolation=Isolation.READ_COMMITTED)
@@ -38,14 +54,40 @@ public BoardVO read(Integer bno) throws Exception{
3854
return dao.read(bno);
3955
}
4056

57+
@Transactional
4158
@Override
4259
public void modify(BoardVO board) throws Exception{
60+
/*
61+
* 1. 원래 게시물 수정
62+
* 2. 기존 첨부파일 목록 삭제
63+
* 3. 새로운 첨부파일 정보 입력
64+
*/
4365
dao.update(board);
66+
67+
Integer bno = board.getBno();
68+
69+
dao.delAttach(bno);
70+
71+
String[] files = board.getFiles();
72+
73+
if(files == null) return;
74+
75+
for(String fileName : files) {
76+
dao.replaceAttach(fileName, bno);
77+
}
4478
}
4579

80+
@Transactional
4681
@Override
4782
public void remove(Integer bno) throws Exception{
83+
/*
84+
* 1. 첨부파일 먼저 삭제해야한다.
85+
* 2. 게시글 삭제한다.
86+
*
87+
*/
88+
dao.delAttach(bno);
4889
dao.delete(bno);
90+
4991
}
5092

5193
@Override
@@ -75,4 +117,9 @@ public int listSearchCount(SearchCriteria cri) throws Exception {
75117

76118
return dao.listSearchCount(cri);
77119
}
120+
@Override
121+
public List<String> getAttach(Integer bno) throws Exception {
122+
123+
return dao.getAttach(bno);
124+
}
78125
}

‎src/main/java/com/jh/vo/BoardVO.java‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,14 @@ public class BoardVO {
1111
private Date regdate;
1212
private int viewcnt;
1313
private int replycnt;
14+
private String[] files;
15+
16+
public String[] getFiles() {
17+
return files;
18+
}
19+
public void setFiles(String[] files) {
20+
this.files = files;
21+
}
1422
public Integer getBno() {
1523
return bno;
1624
}

‎src/main/resources/mappers/boardMapper.xml‎

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,4 +133,21 @@
133133
</if>
134134
</sql>
135135

136+
<!-- mysql의 경우 tbl_board에 저장된 pk를 tbl_attach에 bno로 저장하기 위해(fk)
137+
즉, 바로 직전에 생성된 PK를 알아내기 위해서 -->
138+
<insert id="addAttach">
139+
insert into tbl_attach(fullName, bno) values (#{fullName}, LAST_INSERT_ID())
140+
</insert>
141+
142+
<select id="getAttach" resultType="string">
143+
select fullName from tbl_attach where bno = #{bno} order by regDate
144+
</select>
145+
146+
<delete id="deleteAttach">
147+
delete from tbl_attach where bno = #{bno}
148+
</delete>
149+
150+
<insert id="replaceAttach">
151+
insert into tbl_attach(fullName, bno) values (#{fullName},#{bno})
152+
</insert>
136153
</mapper>

‎src/main/webapp/WEB-INF/spring/appServlet/servlet-context.xml‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,15 @@
2121
<beans:property name="suffix" value=".jsp" />
2222
</beans:bean>
2323

24+
<!-- 파일 업로드로 들어오는 데이터를 처리하는 객체 multipartResolver -->
25+
<beans:bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
26+
<beans:property name="maxUploadSize" value="10485760"></beans:property>
27+
</beans:bean>
28+
29+
<beans:bean id="uploadPath" class="java.lang.String">
30+
<beans:constructor-arg value="C:\Users\LG\Documents\uploadFiles"></beans:constructor-arg>
31+
</beans:bean>
32+
2433
<context:component-scan base-package="com.jh.controller" />
2534

2635

0 commit comments

Comments
 (0)