-
[Spring] 게시판 만들기(20) - 파일 다운로드(BeanNameViewResolver)Spring/4.3.x - 게시판 만들기 2018. 12. 20. 14:15반응형
1. 설정 파일 수정
1_1) servlet-context.xml 수정
- 다운로드 처리를 위한 BeanNameViewResolver를 추가하세요.
- ViewResolver이 2개 이상일 때는 우선순위가 필요하므로 order를 추가하세요.
- FileDownloadUtil.java을 bean으로 등록하세요.
더보기123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657<?xml version="1.0" encoding="UTF-8"?><beans:beans xmlns="http://www.springframework.org/schema/mvc"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns:beans="http://www.springframework.org/schema/beans"xmlns:context="http://www.springframework.org/schema/context"xmlns:mvc="http://www.springframework.org/schema/mvc"xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsdhttp://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsdhttp://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd"><!-- DispatcherServlet Context: defines this servlet's request-processing infrastructure --><!-- Enables the Spring MVC @Controller programming model --><annotation-driven /><!-- Handles HTTP GET requests for /resources/** by efficiently serving up static resources in the $/resources directory --><resources mapping="/resources/**" location="/resources/" /><resources mapping="/css/**" location="/css/" /><resources mapping="/img/**" location="/img/" /><resources mapping="/js/**" location="/js/" /><!-- Resolves views selected for rendering by @Controllers to .jsp resources in the /WEB-INF/views directory --><beans:bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"><beans:property name="prefix" value="/WEB-INF/views/" /><beans:property name="suffix" value=".jsp" /><beans:property name="order" value="1" /></beans:bean><!-- BeanNameViewResolver 설정 --><beans:bean class="org.springframework.web.servlet.view.BeanNameViewResolver"><beans:property name="order" value="0" /></beans:bean><!-- 파일 다운로드를 처리할 클래스를 빈(bean)으로 등록 --><beans:bean id="fileDownloadUtil" class="com.spring.board.common.FileDownloadUtil" /><context:component-scan base-package="com.spring.board" use-default-filters="false"><context:include-filter type="annotation" expression="org.springframework.stereotype.Controller" /></context:component-scan><mvc:interceptors><mvc:interceptor><mvc:mapping path="/**"/><beans:bean id="loggerInterceptor" class="com.spring.board.common.LoggerInterceptor"></beans:bean></mvc:interceptor></mvc:interceptors><beans:bean class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter" /><!-- MultipartResolver 설정 --><beans:bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver"><beans:property name="maxUploadSize" value="100000000" /><beans:property name="maxInMemorySize" value="100000000" /></beans:bean></beans:beans>cs 2. Back-End 파일 수정 및 추가
2_1) FileDownloadUtil.java 추가
첨부파일을 다운로드하는 FileDownloadUtil 추가하세요.
더보기1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768package com.spring.board.common;import java.io.File;import java.io.FileInputStream;import java.io.OutputStream;import java.net.URLEncoder;import java.util.Map;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import org.springframework.util.FileCopyUtils;import org.springframework.web.servlet.view.AbstractView;public class FileDownloadUtil extends AbstractView {public FileDownloadUtil() {// 객체가 생성될 때 Content Type을 다음과 같이 변경setContentType("application/download; charset=utf-8");}@Overrideprotected void renderMergedOutputModel(Map<String, Object> model, HttpServletRequest request, HttpServletResponse response) throws Exception {@SuppressWarnings("unchecked")Map<String, Object> fileInfo = (Map<String, Object>) model.get("fileInfo"); // 전송받은 모델(파일 정보)String fileNameKey = (String) fileInfo.get("fileNameKey"); // 암호화된 파일명(실제 저장된 파일 이름)String fileName = (String) fileInfo.get("fileName"); // 원본 파일명(화면에 표시될 파일 이름)String filePath = (String) fileInfo.get("filePath"); // 파일 경로File file = new File(filePath, fileNameKey);response.setContentType(getContentType());response.setContentLength((int) file.length());// 브라우저, 운영체제정보String userAgent = request.getHeader("User-Agent");// IEif (userAgent.indexOf("MSIE") > -1 || userAgent.indexOf("Trident") > -1) {fileName = URLEncoder.encode(fileName, "UTF-8");} else {fileName = new String( fileName.getBytes("UTF-8"), "8859_1");}response.setHeader("Content-Disposition", "attachment; filename=\"" + fileName + "\";");response.setHeader("Content-Transfer-Encoding", "binary");OutputStream out = response.getOutputStream();FileInputStream fis = null;try {fis = new FileInputStream(file);FileCopyUtils.copy(fis, out);} finally {if(fis != null) {fis.close();}}out.flush();}}cs 2_2) boardMapper.xml
더보기123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280<?xml version="1.0" encoding="UTF-8" ?><!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"><mapper namespace="com.spring.board.boardMapper"><select id="getBoardCnt" parameterType="com.spring.board.form.BoardForm" resultType="int">SELECT COUNT(*)FROM BOARD.TB_BOARD</select><select id="getBoardList" parameterType="com.spring.board.form.BoardForm" resultType="com.spring.board.dto.BoardDto">SELECT *FROM ( SELECT BOARD_SEQ, BOARD_RE_REF, BOARD_RE_LEV, BOARD_RE_SEQ, BOARD_WRITER, BOARD_SUBJECT, BOARD_CONTENT, BOARD_HITS, DEL_YN, INS_USER_ID, CAST( DATE_FORMAT( INS_DATE, '%Y-%m-%d %H:%i:%s' ) AS CHAR(19) ) AS INS_DATE, UPD_USER_ID, CAST( DATE_FORMAT( UPD_DATE, '%Y-%m-%d %H:%i:%s' ) AS CHAR(19) ) AS UPD_DATEFROM BOARD.TB_BOARDORDER BY BOARD_RE_REF DESC, BOARD_RE_SEQ ASC) T1LIMIT #{limit} OFFSET #{offset}</select><delete id="updateBoardHits" parameterType="com.spring.board.form.BoardForm">UPDATE BOARD.TB_BOARDSET BOARD_HITS = BOARD_HITS + 1, UPD_USER_ID = 'NONMEMBER', UPD_DATE = NOW()WHERE BOARD_SEQ = #{board_seq}</delete><select id="getBoardDetail" parameterType="com.spring.board.form.BoardForm" resultType="com.spring.board.dto.BoardDto">SELECT BOARD_SEQ, BOARD_RE_REF, BOARD_RE_LEV, BOARD_RE_SEQ, BOARD_WRITER, BOARD_SUBJECT, BOARD_CONTENT, BOARD_HITS, DEL_YN, INS_USER_ID, CAST( DATE_FORMAT( INS_DATE, '%Y-%m-%d %H:%i:%s' ) AS CHAR(19) ) AS INS_DATE, UPD_USER_ID, CAST( DATE_FORMAT( UPD_DATE, '%Y-%m-%d %H:%i:%s' ) AS CHAR(19) ) AS UPD_DATEFROM BOARD.TB_BOARDWHERE BOARD_SEQ = #{board_seq}</select><select id="getBoardFileList" parameterType="com.spring.board.form.BoardFileForm" resultType="com.spring.board.dto.BoardFileDto">SELECT T1.*FROM (SELECT BOARD_SEQ, FILE_NO, FILE_NAME_KEY, FILE_NAME, FILE_PATH, FILE_SIZE, REMARK, INS_USER_ID, CAST( DATE_FORMAT( INS_DATE, '%Y-%m-%d %H:%i:%s' ) AS CHAR(19) ) AS INS_DATE, UPD_USER_ID, CAST( DATE_FORMAT( UPD_DATE, '%Y-%m-%d %H:%i:%s' ) AS CHAR(19) ) AS UPD_DATEFROM BOARD.TB_BOARD_FILEWHERE BOARD_SEQ = #{board_seq}AND DEL_YN = 'N') T1</select><select id="getBoardReRef" parameterType="com.spring.board.form.BoardForm" resultType="int">SELECT IFNULL(MAX(BOARD_RE_REF), 0) + 1FROM BOARD.TB_BOARD</select><insert id="insertBoard" parameterType="com.spring.board.form.BoardForm"><selectKey resultType="int" keyProperty="board_seq" order="AFTER">SELECT LAST_INSERT_ID()</selectKey>INSERTINTO BOARD.TB_BOARD(BOARD_RE_REF, BOARD_RE_LEV, BOARD_RE_SEQ, BOARD_WRITER, BOARD_SUBJECT, BOARD_CONTENT, INS_USER_ID, INS_DATE, UPD_USER_ID, UPD_DATE)VALUES(#{board_re_ref}, 0, 0, #{board_writer}, #{board_subject}, #{board_content}, 'NONMEMBER', NOW(), 'NONMEMBER', NOW())</insert><insert id="insertBoardFile" parameterType="com.spring.board.form.BoardFileForm"><selectKey resultType="int" keyProperty="file_no" order="BEFORE">SELECT IFNULL(MAX(FILE_NO), 0) + 1FROM BOARD.TB_BOARD_FILEWHERE BOARD_SEQ = #{board_seq}</selectKey>INSERTINTO TB_BOARD_FILE(BOARD_SEQ, FILE_NO, FILE_NAME_KEY, FILE_NAME, FILE_PATH, FILE_SIZE, DEL_YN, INS_USER_ID, INS_DATE, UPD_USER_ID, UPD_DATE)VALUES(#{board_seq}, #{file_no}, #{file_name_key}, #{file_name}, #{file_path}, #{file_size}, 'N', 'NONMEMBER', NOW(), 'NONMEMBER', NOW())</insert><insert id="insertBoardFail" parameterType="com.spring.board.form.BoardForm">INSERTINTO BOARD.TB_BOARD(BOARD_RE_REF, BOARD_RE_LEV, BOARD_RE_SEQ, BOARD_WRITER, BOARD_SUBJECT, BOARD_CONTENT, INS_USER_ID, INS_DATE, UPD_USER_ID, UPD_DATE)VALUES(0, 0, 0, #{board_writer1}, #{board_subject}, #{board_content}, 'NONMEMBER', NOW(), 'NONMEMBER', NOW())</insert><delete id="deleteBoard" parameterType="com.spring.board.form.BoardForm">DELETEFROM BOARD.TB_BOARDWHERE BOARD_SEQ = #{board_seq}</delete><delete id="updateBoard" parameterType="com.spring.board.form.BoardForm">UPDATE BOARD.TB_BOARDSET BOARD_SUBJECT = #{board_subject}, BOARD_CONTENT = #{board_content}, UPD_USER_ID = 'NONMEMBER', UPD_DATE = NOW()WHERE BOARD_SEQ = #{board_seq}</delete><select id="getBoardReplyInfo" parameterType="com.spring.board.form.BoardForm" resultType="com.spring.board.dto.BoardDto">SELECT BOARD_SEQ, BOARD_RE_REF, BOARD_RE_LEV, BOARD_RE_SEQFROM TB_BOARDWHERE BOARD_SEQ = #{board_parent_seq}</select><delete id="updateBoardReSeq" parameterType="com.spring.board.form.BoardForm">UPDATE TB_BOARDSET BOARD_RE_SEQ = BOARD_RE_SEQ + 1, UPD_USER_ID = 'NONMEMBER', UPD_DATE = NOW()WHERE BOARD_RE_REF = #{board_re_ref}AND BOARD_RE_SEQ > #{board_re_seq}</delete><insert id="insertBoardReply" parameterType="com.spring.board.form.BoardForm">INSERTINTO BOARD.TB_BOARD(BOARD_RE_REF, BOARD_RE_LEV, BOARD_RE_SEQ, BOARD_WRITER, BOARD_SUBJECT, BOARD_CONTENT, INS_USER_ID, INS_DATE, UPD_USER_ID, UPD_DATE)VALUES(#{board_re_ref}, #{board_re_lev} + 1, #{board_re_seq} + 1, #{board_writer}, #{board_subject}, #{board_content}, 'NONMEMBER', NOW(), 'NONMEMBER', NOW())</insert></mapper>cs 2_3) BoardFileDto.java
더보기123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115package com.spring.board.dto;import java.util.Date;public class BoardFileDto {int board_seq;int file_no;String file_name_key;String file_name;String file_path;String file_size;String remark;String del_yn;String ins_user_id;Date ins_date;String upd_user_id;Date upd_date;public int getBoard_seq() {return board_seq;}public void setBoard_seq(int board_seq) {this.board_seq = board_seq;}public int getFile_no() {return file_no;}public void setFile_no(int file_no) {this.file_no = file_no;}public String getFile_name_key() {return file_name_key;}public void setFile_name_key(String file_name_key) {this.file_name_key = file_name_key;}public String getFile_name() {return file_name;}public void setFile_name(String file_name) {this.file_name = file_name;}public String getFile_path() {return file_path;}public void setFile_path(String file_path) {this.file_path = file_path;}public String getFile_size() {return file_size;}public void setFile_size(String file_size) {this.file_size = file_size;}public String getRemark() {return remark;}public void setRemark(String remark) {this.remark = remark;}public String getDel_yn() {return del_yn;}public void setDel_yn(String del_yn) {this.del_yn = del_yn;}public String getIns_user_id() {return ins_user_id;}public void setIns_user_id(String ins_user_id) {this.ins_user_id = ins_user_id;}public Date getIns_date() {return ins_date;}public void setIns_date(Date ins_date) {this.ins_date = ins_date;}public String getUpd_user_id() {return upd_user_id;}public void setUpd_user_id(String upd_user_id) {this.upd_user_id = upd_user_id;}public Date getUpd_date() {return upd_date;}public void setUpd_date(Date upd_date) {this.upd_date = upd_date;}}cs 2_4) BoardDto.java
여러 개의 첨부파일 정보를 받아올 수 있도록 BoardFileDto를 List로 선언하여 GET/SET을 추가하세요.
더보기123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153package com.spring.board.dto;import java.util.List;public class BoardDto extends CommonDto {int board_seq;int board_parent_seq;int board_re_ref;int board_re_lev;int board_re_seq;String board_writer;String board_subject;String board_content;int board_hits;String del_yn;String ins_user_id;String ins_date;String upd_user_id;String upd_date;String result;List<BoardFileDto> files;public int getBoard_seq() {return board_seq;}public void setBoard_seq(int board_seq) {this.board_seq = board_seq;}public int getBoard_parent_seq() {return board_parent_seq;}public void setBoard_parent_seq(int board_parent_seq) {this.board_parent_seq = board_parent_seq;}public int getBoard_re_ref() {return board_re_ref;}public void setBoard_re_ref(int board_re_ref) {this.board_re_ref = board_re_ref;}public int getBoard_re_lev() {return board_re_lev;}public void setBoard_re_lev(int board_re_lev) {this.board_re_lev = board_re_lev;}public int getBoard_re_seq() {return board_re_seq;}public void setBoard_re_seq(int board_re_seq) {this.board_re_seq = board_re_seq;}public String getBoard_writer() {return board_writer;}public void setBoard_writer(String board_writer) {this.board_writer = board_writer;}public String getBoard_subject() {return board_subject;}public void setBoard_subject(String board_subject) {this.board_subject = board_subject;}public String getBoard_content() {return board_content;}public void setBoard_content(String board_content) {this.board_content = board_content;}public int getBoard_hits() {return board_hits;}public void setBoard_hits(int board_hits) {this.board_hits = board_hits;}public String getDel_yn() {return del_yn;}public void setDel_yn(String del_yn) {this.del_yn = del_yn;}public String getIns_user_id() {return ins_user_id;}public void setIns_user_id(String ins_user_id) {this.ins_user_id = ins_user_id;}public String getIns_date() {return ins_date;}public void setIns_date(String ins_date) {this.ins_date = ins_date;}public String getUpd_user_id() {return upd_user_id;}public void setUpd_user_id(String upd_user_id) {this.upd_user_id = upd_user_id;}public String getUpd_date() {return upd_date;}public void setUpd_date(String upd_date) {this.upd_date = upd_date;}public String getResult() {return result;}public void setResult(String result) {this.result = result;}public List<BoardFileDto> getFiles() {return files;}public void setFiles(List<BoardFileDto> files) {this.files = files;}}cs 2_5) BoardDao.java
더보기123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101package com.spring.board.dao;import java.util.List;import javax.annotation.Resource;import org.apache.ibatis.session.SqlSession;import org.springframework.stereotype.Repository;import com.spring.board.dto.BoardDto;import com.spring.board.dto.BoardFileDto;import com.spring.board.form.BoardFileForm;import com.spring.board.form.BoardForm;@Repositorypublic class BoardDao {@Resource(name = "sqlSession")private SqlSession sqlSession;private static final String NAMESPACE = "com.spring.board.boardMapper";/** 게시판 - 목록 수 */public int getBoardCnt(BoardForm boardForm) throws Exception {return sqlSession.selectOne(NAMESPACE + ".getBoardCnt", boardForm);}/** 게시판 - 목록 조회 */public List<BoardDto> getBoardList(BoardForm boardForm) throws Exception {return sqlSession.selectList(NAMESPACE + ".getBoardList", boardForm);}/** 게시판 - 조회 수 수정 */public int updateBoardHits(BoardForm boardForm) throws Exception {return sqlSession.update(NAMESPACE + ".updateBoardHits", boardForm);}/** 게시판 - 상세 조회 */public BoardDto getBoardDetail(BoardForm boardForm) throws Exception {return sqlSession.selectOne(NAMESPACE + ".getBoardDetail", boardForm);}/** 게시판 - 첨부파일 조회 */public List<BoardFileDto> getBoardFileList(BoardFileForm boardFileForm) throws Exception {return sqlSession.selectList(NAMESPACE + ".getBoardFileList", boardFileForm);}/** 게시판 - 그룹 번호 조회 */public int getBoardReRef(BoardForm boardForm) throws Exception {return sqlSession.selectOne(NAMESPACE + ".getBoardReRef", boardForm);}/** 게시판 - 등록 */public int insertBoard(BoardForm boardForm) throws Exception {return sqlSession.insert(NAMESPACE + ".insertBoard", boardForm);}/** 게시판 - 첨부파일 등록 */public int insertBoardFile(BoardFileForm boardFileForm) throws Exception {return sqlSession.insert(NAMESPACE + ".insertBoardFile", boardFileForm);}/** 게시판 - 등록 실패(트랜잭션 테스트) */public int insertBoardFail(BoardForm boardForm) throws Exception {return sqlSession.insert(NAMESPACE + ".insertBoardFail", boardForm);}/** 게시판 - 삭제 */public int deleteBoard(BoardForm boardForm) throws Exception {return sqlSession.delete(NAMESPACE + ".deleteBoard", boardForm);}/** 게시판 - 수정 */public int updateBoard(BoardForm boardForm) throws Exception {return sqlSession.update(NAMESPACE + ".updateBoard", boardForm);}/** 게시판 - 답글 정보 조회 */public BoardDto getBoardReplyInfo(BoardForm boardForm) throws Exception {return sqlSession.selectOne(NAMESPACE + ".getBoardReplyInfo", boardForm);}/** 게시판 - 답글의 순서 수정 */public int updateBoardReSeq(BoardForm boardForm) throws Exception {return sqlSession.update(NAMESPACE + ".updateBoardReSeq", boardForm);}/** 게시판 - 답글 등록 */public int insertBoardReply(BoardForm boardForm) throws Exception {return sqlSession.insert(NAMESPACE + ".insertBoardReply", boardForm);}}cs 2_6) BoardService.java 수정
게시글 상세 조회 시에 첨부파일 정보를 조회하는 부분을 추가하세요.
더보기123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240package com.spring.board.service;import java.io.File;import java.util.ArrayList;import java.util.HashMap;import java.util.List;import java.util.UUID;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.stereotype.Service;import org.springframework.web.multipart.MultipartFile;import com.spring.board.common.PagingUtil;import com.spring.board.common.ResultUtil;import com.spring.board.dao.BoardDao;import com.spring.board.dto.BoardDto;import com.spring.board.dto.CommonDto;import com.spring.board.form.BoardFileForm;import com.spring.board.form.BoardForm;import com.spring.board.form.CommonForm;@Servicepublic class BoardService {@Autowiredprivate BoardDao boardDao;/** 게시판 - 목록 조회 */public ResultUtil getBoardList(BoardForm boardForm) throws Exception {ResultUtil resultUtil = new ResultUtil();CommonDto commonDto = new CommonDto();int totalCount = boardDao.getBoardCnt(boardForm);if (totalCount != 0) {CommonForm commonForm = new CommonForm();commonForm.setFunction_name(boardForm.getFunction_name());commonForm.setCurrent_page_no(boardForm.getCurrent_page_no());commonForm.setCount_per_page(10);commonForm.setCount_per_list(10);commonForm.setTatal_list_count(totalCount);commonDto = PagingUtil.setPageUtil(commonForm);}boardForm.setLimit(commonDto.getLimit());boardForm.setOffset(commonDto.getOffset());List<BoardDto> list = boardDao.getBoardList(boardForm);HashMap<String, Object> resultMap = new HashMap<String, Object>();resultMap.put("list", list);resultMap.put("totalCount", totalCount);resultMap.put("pagination", commonDto.getPagination());resultUtil.setData(resultMap);resultUtil.setState("SUCCESS");return resultUtil;}/** 게시판 - 목록 조회 *//** public List<BoardDto> getBoardList(BoardForm boardForm) throws Exception {** return boardDao.getBoardList(boardForm); }*//** 게시판 - 상세 조회 */public BoardDto getBoardDetail(BoardForm boardForm) throws Exception {BoardDto boardDto = new BoardDto();String searchType = boardForm.getSearch_type();if ("S".equals(searchType)) {int updateCnt = boardDao.updateBoardHits(boardForm);if (updateCnt > 0) {boardDto = boardDao.getBoardDetail(boardForm);}} else {boardDto = boardDao.getBoardDetail(boardForm);}BoardFileForm boardFileForm = new BoardFileForm();boardFileForm.setBoard_seq(boardForm.getBoard_seq());boardDto.setFiles(boardDao.getBoardFileList(boardFileForm));return boardDto;}/** 게시판 - 등록 */public BoardDto insertBoard(BoardForm boardForm) throws Exception {BoardDto boardDto = new BoardDto();int insertCnt = 0;int boardReRef = boardDao.getBoardReRef(boardForm);boardForm.setBoard_re_ref(boardReRef);insertCnt = boardDao.insertBoard(boardForm);List<BoardFileForm> boardFileList = getBoardFileInfo(boardForm);for (BoardFileForm boardFileForm : boardFileList) {boardDao.insertBoardFile(boardFileForm);}if (insertCnt > 0) {boardDto.setResult("SUCCESS");} else {boardDto.setResult("FAIL");}return boardDto;}/** 게시판 - 첨부파일 정보 조회 */public List<BoardFileForm> getBoardFileInfo(BoardForm boardForm) throws Exception {List<MultipartFile> files = boardForm.getFiles();List<BoardFileForm> boardFileList = new ArrayList<BoardFileForm>();BoardFileForm boardFileForm = new BoardFileForm();int boardSeq = boardForm.getBoard_seq();String fileName = null;String fileExt = null;String fileNameKey = null;String fileSize = null;// 파일이 저장될 Path 설정String filePath = "C:\\board\\file";if (files != null && files.size() > 0) {File file = new File(filePath);// 디렉토리가 없으면 생성if (file.exists() == false) {file.mkdirs();}for (MultipartFile multipartFile : files) {fileName = multipartFile.getOriginalFilename();fileExt = fileName.substring(fileName.lastIndexOf("."));// 파일명 변경(uuid로 암호화) + 확장자fileNameKey = getRandomString() + fileExt;fileSize = String.valueOf(multipartFile.getSize());// 설정한 Path에 파일 저장file = new File(filePath + "/" + fileNameKey);multipartFile.transferTo(file);boardFileForm = new BoardFileForm();boardFileForm.setBoard_seq(boardSeq);boardFileForm.setFile_name(fileName);boardFileForm.setFile_name_key(fileNameKey);boardFileForm.setFile_path(filePath);boardFileForm.setFile_size(fileSize);boardFileList.add(boardFileForm);}}return boardFileList;}/** 게시판 - 삭제 */public BoardDto deleteBoard(BoardForm boardForm) throws Exception {BoardDto boardDto = new BoardDto();int deleteCnt = boardDao.deleteBoard(boardForm);if (deleteCnt > 0) {boardDto.setResult("SUCCESS");} else {boardDto.setResult("FAIL");}return boardDto;}/** 게시판 - 수정 */public BoardDto updateBoard(BoardForm boardForm) throws Exception {BoardDto boardDto = new BoardDto();int deleteCnt = boardDao.updateBoard(boardForm);if (deleteCnt > 0) {boardDto.setResult("SUCCESS");} else {boardDto.setResult("FAIL");}return boardDto;}/** 게시판 - 답글 등록 */public BoardDto insertBoardReply(BoardForm boardForm) throws Exception {BoardDto boardDto = new BoardDto();BoardDto boardReplayInfo = boardDao.getBoardReplyInfo(boardForm);boardForm.setBoard_seq(boardReplayInfo.getBoard_seq());boardForm.setBoard_re_lev(boardReplayInfo.getBoard_re_lev());boardForm.setBoard_re_ref(boardReplayInfo.getBoard_re_ref());boardForm.setBoard_re_seq(boardReplayInfo.getBoard_re_seq());int insertCnt = 0;insertCnt += boardDao.updateBoardReSeq(boardForm);insertCnt += boardDao.insertBoardReply(boardForm);if (insertCnt > 0) {boardDto.setResult("SUCCESS");} else {boardDto.setResult("FAIL");}return boardDto;}/** 32글자의 랜덤한 문자열(숫자포함) 생성 */public static String getRandomString() {return UUID.randomUUID().toString().replaceAll("-", "");}}cs 2_7) BoardController.java 수정
첨부파일에 암호화파일명, 파일명, 파일경로를 fileDownloadUtil.java에 넘겨주는 fileDownload() 컨트롤러를 추가하세요.
더보기123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137package com.spring.board.controller;import java.util.HashMap;import java.util.Map;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.stereotype.Controller;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RequestParam;import org.springframework.web.bind.annotation.ResponseBody;import org.springframework.web.servlet.ModelAndView;import com.spring.board.common.ResultUtil;import com.spring.board.dto.BoardDto;import com.spring.board.form.BoardForm;import com.spring.board.service.BoardService;@Controller@RequestMapping(value = "/board")public class BoardController {@Autowiredprivate BoardService boardService;/** 게시판 - 목록 페이지 이동 */@RequestMapping(value = "/boardList")public String boardList(HttpServletRequest request, HttpServletResponse response) throws Exception {return "board/boardList";}/** 게시판 - 목록 조회 */@RequestMapping(value = "/getBoardList")@ResponseBodypublic ResultUtil getBoardList(HttpServletRequest request, HttpServletResponse response, BoardForm boardForm) throws Exception {ResultUtil resultUtils = boardService.getBoardList(boardForm);return resultUtils;}/** 게시판 - 상세 페이지 이동 */@RequestMapping(value = "/boardDetail")public String boardDetail(HttpServletRequest request, HttpServletResponse response) throws Exception {return "board/boardDetail";}/** 게시판 - 상세 조회 */@RequestMapping(value = "/getBoardDetail")@ResponseBodypublic BoardDto getBoardDetail(HttpServletRequest request, HttpServletResponse response, BoardForm boardForm) throws Exception {BoardDto boardDto = boardService.getBoardDetail(boardForm);return boardDto;}/** 게시판 - 작성 페이지 이동 */@RequestMapping(value = "/boardWrite")public String boardWrite(HttpServletRequest request, HttpServletResponse response) throws Exception {return "board/boardWrite";}/** 게시판 - 등록 */@RequestMapping(value = "/insertBoard")@ResponseBodypublic BoardDto insertBoard(HttpServletRequest request, HttpServletResponse response, BoardForm boardForm) throws Exception {BoardDto boardDto = boardService.insertBoard(boardForm);return boardDto;}/** 게시판 - 삭제 */@RequestMapping(value = "/deleteBoard")@ResponseBodypublic BoardDto deleteBoard(HttpServletRequest request, HttpServletResponse response, BoardForm boardForm) throws Exception {BoardDto boardDto = boardService.deleteBoard(boardForm);return boardDto;}/** 게시판 - 수정 페이지 이동 */@RequestMapping(value = "/boardUpdate")public String boardUpdate(HttpServletRequest request, HttpServletResponse response) throws Exception {return "board/boardUpdate";}/** 게시판 - 수정 */@RequestMapping(value = "/updateBoard")@ResponseBodypublic BoardDto updateBoard(HttpServletRequest request, HttpServletResponse response, BoardForm boardForm) throws Exception {BoardDto boardDto = boardService.updateBoard(boardForm);return boardDto;}/** 게시판 - 답글 페이지 이동 */@RequestMapping(value = "/boardReply")public String boardReply(HttpServletRequest request, HttpServletResponse response) throws Exception {return "board/boardReply";}/** 게시판 - 답글 등록 */@RequestMapping(value = "/insertBoardReply")@ResponseBodypublic BoardDto insertBoardReply(HttpServletRequest request, HttpServletResponse response, BoardForm boardForm) throws Exception {BoardDto boardDto = boardService.insertBoardReply(boardForm);return boardDto;}/** 게시판 - 첨부파일 다운로드 */@RequestMapping("/fileDownload")public ModelAndView fileDownload(@RequestParam("fileNameKey") String fileNameKey,@RequestParam("fileName") String fileName,@RequestParam("filePath") String filePath) throws Exception {/** 첨부파일 정보 조회 */Map<String, Object> fileInfo = new HashMap<String, Object>();fileInfo.put("fileNameKey", fileNameKey);fileInfo.put("fileName", fileName);fileInfo.put("filePath", filePath);return new ModelAndView("fileDownloadUtil", "fileInfo", fileInfo);}}cs 3. Front-End 파일 수정
3_1) boardDetail.jsp
첨부파일 정보를 화면에 출력하고, 첨부파일명 클릭 시에 fileDownload() 컨트롤러가 호출되도록 수정하세요.
더보기123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%><%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%><%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions" %><!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"><html><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8"><title>게시판 상세</title><%String boardSeq = request.getParameter("boardSeq");%><c:set var="boardSeq" value="<%=boardSeq%>"/> <!-- 게시글 번호 --><!-- 공통 CSS --><link rel="stylesheet" type="text/css" href="/css/common/common.css"/><!-- 공통 JavaScript --><script type="text/javascript" src="/js/common/jquery.js"></script><script type="text/javascript">$(document).ready(function(){getBoardDetail();});/** 게시판 - 목록 페이지 이동 */function goBoardList(){location.href = "/board/boardList";}/** 게시판 - 수정 페이지 이동 */function goBoardUpdate(){var boardSeq = $("#board_seq").val();location.href = "/board/boardUpdate?boardSeq="+ boardSeq;}/** 게시판 - 답글 페이지 이동 */function goBoardReply(){var boardSeq = $("#board_seq").val();location.href = "/board/boardReply?boardSeq="+ boardSeq;}/** 게시판 - 상세 조회 */function getBoardDetail(boardSeq){var boardSeq = $("#board_seq").val();if(boardSeq != ""){$.ajax({url : "/board/getBoardDetail",data : $("#boardForm").serialize(),dataType : "JSON",cache : false,async : true,type : "POST",success : function(obj) {getBoardDetailCallback(obj);},error : function(xhr, status, error) {}});} else {alert("오류가 발생했습니다.\n관리자에게 문의하세요.");}}/** 게시판 - 상세 조회 콜백 함수 */function getBoardDetailCallback(obj){var str = "";if(obj != null){var boardSeq = obj.board_seq;var boardReRef = obj.board_re_ref;var boardReLev = obj.board_re_lev;var boardReSeq = obj.board_re_seq;var boardWriter = obj.board_writer;var boardSubject = obj.board_subject;var boardContent = obj.board_content;var boardHits = obj.board_hits;var delYn = obj.del_yn;var insUserId = obj.ins_user_id;var insDate = obj.ins_date;var updUserId = obj.upd_user_id;var updDate = obj.upd_date;var files = obj.files;var filesLen = files.length;str += "<tr>";str += "<th>제목</th>";str += "<td>"+ boardSubject +"</td>";str += "<th>조회수</th>";str += "<td>"+ boardHits +"</td>";str += "</tr>";str += "<tr>";str += "<th>작성자</th>";str += "<td>"+ boardWriter +"</td>";str += "<th>작성일시</th>";str += "<td>"+ insDate +"</td>";str += "</tr>";str += "<tr>";str += "<th>내용</th>";str += "<td colspan='3'>"+ boardContent +"</td>";str += "</tr>";if(filesLen > 0){for(var a=0; a<filesLen; a++){var boardSeq = files[a].board_seq;var fileNo = files[a].file_no;var fileNameKey = files[a].file_name_key;var fileName = files[a].file_name;var filePath = files[a].file_path;var fileSize = files[a].file_size;var remark = files[a].remark;var delYn = files[a].del_yn;var insUserId = files[a].ins_user_id;var insDate = files[a].ins_date;var updUserId = files[a].upd_user_id;var updDate = files[a].upd_date;str += "<tr>";str += "<th>첨부파일</th>";str += "<td colspan='3'><a href='/board/fileDownload?fileNameKey="+encodeURI(fileNameKey)+"&fileName="+encodeURI(fileName)+"&filePath="+encodeURI(filePath)+"'>" + fileName + "</a></td>";str += "</tr>";}}} else {alert("등록된 글이 존재하지 않습니다.");return;}$("#tbody").html(str);}/** 게시판 - 삭제 */function deleteBoard(){var boardSeq = $("#board_seq").val();var yn = confirm("게시글을 삭제하시겠습니까?");if(yn){$.ajax({url : "/board/deleteBoard",data : $("#boardForm").serialize(),dataType : "JSON",cache : false,async : true,type : "POST",success : function(obj) {deleteBoardCallback(obj);},error : function(xhr, status, error) {}});}}/** 게시판 - 삭제 콜백 함수 */function deleteBoardCallback(obj){if(obj != null){var result = obj.result;if(result == "SUCCESS"){alert("게시글 삭제를 성공하였습니다.");goBoardList();} else {alert("게시글 삭제를 실패하였습니다.");return;}}}</script></head><body><div id="wrap"><div id="container"><div class="inner"><h2>게시글 상세</h2><form id="boardForm" name="boardForm"><table width="100%" class="table01"><colgroup><col width="15%"><col width="35%"><col width="15%"><col width="*"></colgroup><tbody id="tbody"></tbody></table><input type="hidden" id="board_seq" name="board_seq" value="${boardSeq}"/> <!-- 게시글 번호 --><input type="hidden" id="search_type" name="search_type" value="S"/> <!-- 조회 타입 - 상세(S)/수정(U) --></form><div class="btn_right mt15"><button type="button" class="btn black mr5" onclick="javascript:goBoardList();">목록으로</button><button type="button" class="btn black mr5" onclick="javascript:goBoardUpdate();">수정하기</button><button type="button" class="btn black" onclick="javascript:deleteBoard();">삭제하기</button><button type="button" class="btn black mr5" onclick="javascript:goBoardReply();">답글쓰기</button></div></div></div></div></body></html>cs 4. 첨부파일이 다운로드 확인
첨부파일이 정상적으로 다운로드 되는지 확인하세요.
소스 코드는 Github Repository - https://github.com/tychejin1218/board_v1 (branch : section20) 를 참조하세요.
Github에서 프로젝트 가져오기 - https://tychejin.tistory.com/33반응형'Spring > 4.3.x - 게시판 만들기' 카테고리의 다른 글
[Spring] 게시판 만들기(22) - MDC(Mapped Diagnostic Context) 설정 (13) 2019.11.24 [Spring] 게시판 만들기(21) - 게시글 수정(첨부파일 등록 및 삭제) (5) 2019.05.10 [Spring] 게시판 만들기(19) - 파일 업로드(MultipartHttpServletRequest) (2) 2018.12.20 [Spring] 게시판 만들기(18) - 계층형 게시판(답글쓰기) 적용 (11) 2018.12.19 [Spring] 게시판 만들기(17) - 페이징(Paging) 처리 (31) 2018.12.19