본문 바로가기
매일코딩/Spring

24.스프링프로젝트 - download

by 인생여희 2016. 11. 17.
반응형
1.page

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
//page
    
    <%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%-- <%@ page session="false"%> --%>
<c:set var="path" value="${pageContext.request.contextPath}"></c:set> 
 
 
 
 
<jsp:include page="boardHeader.jsp" flush="false"/>
 
 
                    <h1 id="community"><div>COMMUNITY</div></h1>
          <article>
              <h2>게시판</h2>
              
 
    
    <table border="1" align="center" class="tbl_type" WIDTH=50% table-layout: fixed>
        <tr>
            <th  nowrap WIDTH="40">번호</th>
            <th  nowrap WIDTH="100">이름</th>
            <th  nowrap WIDTH="250">제목</th>
            <th  nowrap WIDTH="100">날짜</th>
            <th  nowrap WIDTH="40">조회</th>
            <th  nowrap WIDTH="70">첨부파일</th>
            <th  nowrap WIDTH="70">다운로드</th>
        </tr>
 
 
        <c:forEach var="row" items="${list}">
            <tr>
                <td>${row.idx }</td>
                <td>${row.username }</td>
                <td>
                
                <!-- 답변 들여쓰기 -->
                <c:forEach begin="1" end="${row.depth }">
                &nbsp;&nbsp;
                </c:forEach>
    
    
 
                <a href="${path}/board/view.do?idx=${row.idx}">
                ${row.subject}</a>
                <c:if test="${row.comment_count >0 }">
            
            <span style="color:red">[${row.comment_count}]</span>
                
                </c:if>
                </td>
    
                <td>${row.post_date}</td>
                <td>${row.hit}</td>
                
                
                <!-- 첨부파일의 사이즈가 보다 크면 파일 다운로드 링크 표시 -->
                
                
                <td align="center">
                <c:if test="${row.filesize > 0 }">
                    <a href="${path}/board/down.do?idx=${row.idx}">
                    <img src="${path}/images/file.gif">
                </a>
                </c:if>
                </td>
 
                <td>${row.down}</td>
            </tr>
        </c:forEach>
 
 
 
<tr align="center">
<td colspan="7">
<!-- 이전 -->
<c:if test="${page.curPage>1 }">
<a href="${path}/board/board_list.do?curPage=1&search_option=${search_option}&search=${search}">[시작] </a>
</c:if>
 
<c:if test="${page.curBlock > 1 }">
<a href="${path}/board/board_list.do?curPage=${page.prevPage}&search_option=${search_option}&search=${search}">[이전] </a>
</c:if>
 
 
 
<c:forEach var="pageNum" begin="${page.blockStart}" end="${page.blockEnd}">
 
<c:choose>
    <c:when test="${pageNum==page.curPage }">
 
    <span style="color:red">[${pageNum}]</span>
    </c:when>
    
    <c:otherwise>
<a href="${path}/board/board_list.do?curPage=${pageNum}&search_option=${search_option}&search=${search}">[${pageNum}] </a>&nbsp;
    </c:otherwise>
</c:choose>
 
</c:forEach>
 
<!-- 다음 -->
 
 
<c:if test="${page.curBlock <= page.totBlock}">
 
<a href="${path}/board/board_list.do?curPage=${page.nextPage}&search_option=${search_option}&search=${search}">[다음] </a>
</c:if>
 
<!-- 마지막 페이지 -->
 
<c:if test="${page.curPage < page.totPage}">
 
<a href="${path}/board/board_list.do?curPage=${page.totPage}&search_option=${search_option}&search=${search}">[끝] </a>
 
</c:if>
</td>
 
</tr>
 
    </table>
 
<br>
<!-- 검색폼 -->
 
    <form method="post" action="${path}/board/board_list.do">
        <div align="center"><select name="search_option">
            <option value="username"
            
            <c:if test= "${search_option =='username'}"> selected </c:if> >
            이름
            </option>
            
            <option value="subject" <c:if test= "${search_option =='subject'}"> selected </c:if> >제목</option>
            <option value="content" <c:if test= "${search_option =='content'}"> selected </c:if> >내용</option>
            <option value="all" <c:if test= "${search_option =='all'}"> selected </c:if> >전체</option>
        </select>
        
        <input type="text" name="search" value="${search}">
        <input type="submit" value="확인">&nbsp;&nbsp;&nbsp;
        <input type="button" id="btnWrite" value="글쓰기"></div>
    </form>    
<%-- <a href="${path}/board/write.do">글쓰기</a> --%>
    </article>
          <jsp:include page="boardSidebar.jsp" flush="false"/>
 
          <jsp:include page="boardFooter.jsp" flush="false"/>
 
cs



2.controller


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
//controller
    
    
    
    @RequestMapping("board/down.do")
    public void down(@RequestParam int idx, HttpServletResponse response) throws Exception {
        
        
        //filename 
        
        String filename = boardService.getFilename(idx);
        
 
        File file = new File("d:\\upload\\" + filename);
 
        // file's header info
        response.setContentType("application/octet-stream");
        response.setContentLength((int) file.length());
 
        //filename incoding korean
        
        response.setHeader("Content-Disposition",
                "attachment;fileName='" + URLEncoder.encode(filename, "utf-8"+ "';");
        response.setHeader("Content-Transfer-Encoding""binary");
 
        //send
        java.io.OutputStream out = response.getOutputStream();
        FileInputStream fis = null;
 
        try {
            
            fis = new FileInputStream(file);
            FileCopyUtils.copy(fis, out);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (fis != null) {
 
                try {
                    fis.close();
                } catch (Exception e2) {
 
                    e2.printStackTrace();
                }
            }
        }
 
        out.flush(); 
        
        
        
        boardService.downUp(idx);
    }
cs



3. service dao 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
//service
        @Override
    public String getFilename(int idx) {
        return boardDAO.getFilename(idx);
    }
    
    
    
    //dao
    
    
        @Override
    public String getFilename(int idx) {
        String filename = "";
 
        try {
            filename = sqlSession.selectOne("getFilename", idx);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return filename;
    }
    
    
    
    
    
    
        //service
    
        @Override
    public void downUp(int idx) {
 
        boardDAO.downUp(idx);
 
    }
    
    
    
    
    //dao
    
        @Override
    public void downUp(int idx) {
 
        try {
            sqlSession.update("downUp", idx);
        } catch (Exception e) {
            e.printStackTrace();
        }
 
    }
cs



5.mapper


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
//mapper
    
    <select id="getFilename" resultType="String">
 
        select filename from board
        where idx=#{idx}
 
    </select>
    
    
    
    //mapper downup
    
        <update id="downUp">
 
        update board set down=down+where idx=#{idx}
 
    </update>
    
cs


반응형

댓글