방카@Dev
[Spring]게시판 파일 업로드 라이브러리(COS) 본문
1. <pom.xml> Dependency 추가
<!-- https://mvnrepository.com/artifact/com.servlets/cos -->
<dependency>
<groupId>com.servlets</groupId>
<artifactId>cos</artifactId>
<version>09May2002</version>
</dependency>
2. 파일이 들어갈 boardupload 폴더 생성하기
//폴더 경로
C:\Users\BIT\Documents\Spring_exam ((스프링 워크스페이스))
\.metadata\.plugins\org.eclipse.wst.server.core\tmp0\wtpwebapps\{프로젝트명}\boardupload
3. <servlet-context.xml>에서 불러올 경로 설정
<resources mapping="/resources/**" location="/resources/" />
<resources mapping="/boardupload/**" location="/boardupload/" />
4. 업로드 View 화면
//<boardwrite.jsp>
<%@ page language="java" contentType="text/html; charset=EUC-KR"%>
<html>
<head>
<title>MVC 게시판</title>
<script language="javascript">
function addboard(){
boardform.submit();
}
</script>
</head>
<body>
<!-- 게시판 등록 -->
<form action="${pageContext.request.contextPath}/board/boardAddAction" method="post"
enctype="multipart/form-data" name="boardform">
<table cellpadding="0" cellspacing="0">
<tr align="center" valign="middle">
<td colspan="5">MVC 게시판</td>
</tr>
<tr>
<td style="font-family:돋음; font-size:12" height="16">
<div align="center">글쓴이</div>
</td>
<td>
<input name="BOARD_NAME" type="text" size="10" maxlength="10"
value=""/>
</td>
</tr>
<tr>
<td style="font-family:돋음; font-size:12" height="16">
<div align="center">비밀번호</div>
</td>
<td>
<input name="BOARD_PASS" type="password" size="10" maxlength="10"
value=""/>
</td>
</tr>
<tr>
<td style="font-family:돋음; font-size:12" height="16">
<div align="center">제 목</div>
</td>
<td>
<input name="BOARD_SUBJECT" type="text" size="50" maxlength="100"
value=""/>
</td>
</tr>
<tr>
<td style="font-family:돋음; font-size:12">
<div align="center">내 용</div>
</td>
<td>
<textarea name="BOARD_CONTENT" cols="67" rows="15"></textarea>
</td>
</tr>
<tr>
<td style="font-family:돋음; font-size:12">
<div align="center">파일 첨부</div>
</td>
<td>
<input name="BOARD_FILE" type="file"/>
</td>
</tr>
<tr bgcolor="cccccc">
<td colspan="2" style="height:1px;">
</td>
</tr>
<tr><td colspan="2"> </td></tr>
<tr align="center" valign="middle">
<td colspan="5">
<a href="javascript:addboard()">[등록]</a>
<a href="javascript:history.go(-1)">[뒤로]</a>
</td>
</tr>
</table>
</form>
<!-- 게시판 등록 -->
</body>
</html>
4.서비스 로직
package com.kbfg.digi.board;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import org.springframework.ui.Model;
import com.kbfg.digi.Command;
import com.kbfg.digi.DAO.BoardDAO;
import com.kbfg.digi.DTO.BoardBean;
import com.oreilly.servlet.MultipartRequest;
import com.oreilly.servlet.multipart.DefaultFileRenamePolicy;
public class BWriteCommand implements Command {
@Override
public void execute(Model model) {
// TODO Auto-generated method stub
Map<String, Object> map = model.asMap();
HttpServletRequest request = (HttpServletRequest) map.get("request");
BoardDAO boarddao=new BoardDAO();
BoardBean boarddata=new BoardBean();
String realFolder="";
String saveFolder="boardupload";
int fileSize=5*1024*1024;
realFolder=request.getRealPath(saveFolder);
boolean result=false;
try{
MultipartRequest multi=null;
multi=new MultipartRequest(request,
realFolder,
fileSize,
"euc-kr",
new DefaultFileRenamePolicy());
boarddata.setBOARD_NAME(multi.getParameter("BOARD_NAME"));
boarddata.setBOARD_PASS(multi.getParameter("BOARD_PASS"));
boarddata.setBOARD_SUBJECT(multi.getParameter("BOARD_SUBJECT"));
boarddata.setBOARD_CONTENT(multi.getParameter("BOARD_CONTENT"));
boarddata.setBOARD_FILE(
multi.getFilesystemName((String)multi.getFileNames().nextElement()));
result=boarddao.boardInsert(boarddata);
if(result==false){
System.out.println("게시글 업로드에 실패하였습니다.");
}
System.out.println("게시글 업로드에 성공하였습니다.");
}catch(Exception ex){
ex.printStackTrace();
}
}
}
5. 업로드 확인 화면
<%@ page language="java" contentType="text/html; charset=UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<html>
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Banca@Dev</title>
<link rel="stylesheet" href="${pageContext.request.contextPath}/resources/css/boardview.css">
<link href="https://cdnjs.cloudflare.com/ajax/libs/flowbite/2.3.0/flowbite.min.css" rel="stylesheet" />
<script src="https://cdnjs.cloudflare.com/ajax/libs/flowbite/2.3.0/flowbite.min.js"></script>
</head>
<body>
<div class="main-container">
<c:import url="../nav.jsp"></c:import>
<!-- 게시판 수정 -->
<div class="wrapper">
<div class="title" align="center">🏬게시판 상세보기🚀</div>
<table>
<tr>
<td style="font-family:돋음; font-size:12" height="16">
<div align="center">제 목 </div>
</td>
<td style="font-family:돋음; font-size:12">
${requestScope.boarddata.BOARD_SUBJECT}
</td>
</tr>
<tr bgcolor="cccccc">
<td colspan="2" style="height:1px;">
</td>
</tr>
<tr>
<td style="font-family:돋음; font-size:12">
<div align="center">내 용</div>
</td>
<td style="font-family:돋음; font-size:12">
<table border=0 width=490 height=250 style="table-layout:fixed">
<tr>
<td valign=top style="font-family:돋음; font-size:12">
${requestScope.boarddata.BOARD_CONTENT}
</td>
</tr>
</table>
</td>
</tr>
<tr>
<td style="font-family:돋음; font-size:12">
<div align="center">첨부파일</div>
</td>
<td style="font-family:돋음; font-size:12">
<c:if test="${!(requestScope.boarddata.BOARD_FILE==null)}">
<a href="${pageContext.request.contextPath}/boardupload/${requestScope.boarddata.BOARD_FILE}">
${requestScope.boarddata.BOARD_FILE}
</a>
</c:if>
</td>
</tr>
<tr bgcolor="cccccc">
<td colspan="2" style="height:1px;"></td>
</tr>
<tr><td colspan="2"> </td></tr>
<tr align="center" valign="middle">
<td colspan="5">
<font size=2>
<a href="./BoardReplyAction.bo?num=${requestScope.boarddata.BOARD_NUM}">
[답변]
</a>
<a href="./BoardModify.bo?num=${requestScope.boarddata.BOARD_NUM}">
[수정]
</a>
<a href="${pageContext.request.contextPath}/board/boardDelete?num=${requestScope.boarddata.BOARD_NUM}">
[삭제]
</a>
<a href="${pageContext.request.contextPath}/board/list">[목록]</a>
</font>
</td>
</tr>
</table>
</div>
</div>
<!-- 게시판 수정 -->
</body>
</html>
'BackEnd > Spring' 카테고리의 다른 글
[Spring]빈 생명주기 콜백 (0) | 2024.06.21 |
---|---|
[Spring]JDBC Template 정리 (0) | 2024.06.20 |
[Spring]Validator를 이용한 유효성 검사 (0) | 2024.06.17 |
[Spring]IntelliJ 자바 버전 변경하는 방법 (0) | 2024.06.15 |
[Spring]MVC에서 데이터를 주고 받는 방법 (0) | 2024.06.12 |