๐ป
JSP ํ๋ก๊ทธ๋๋ฐ - [MVC/DB์ฐ๋] ํ์ผ์ ๋ก๋ ๊ธฐ๋ฅ ๋ณธ๋ฌธ
boardํด๋(๊ฒ์ํ๋ง๋ค๊ธฐ ํด๋)๋ฅผ ๋ณต์ฌํด์ fileboardํด๋๋ฅผ ๋ง๋ค์ด๋ณด์.
ํ์ผ์ ์ถ๋ ฅ์ ์ํ ๊ฒ์ด๊ณ ๋๋จธ์ง๋ ๋ค ๊ฐ๋ค. (๋ณต์ฌํด์ ํด๋๋ฅผ ๋ง๋ค ๋, ์ฃผ์๋ฅผ ์ ์ดํด์ผํ๋ค. ๊ผฌ์ด์ง ์๋๋ก ์ฃผ์!)
Orcle SQL developer
์์ ํ์ BoardDao.java๋ก ๊ฐ์ insert๋ฌธ์ ๋ณ๊ฒฝ์์ผ์ฃผ์๋ค. (์๋ก์ด ์นผ๋ผ์ ์ถ๊ฐํ๋๋ column๊ฐ์๊ฐ ๋ง์ง ์์์ insert๊ฐ ๋์ง ์์ ๊ธ์ฐ๊ธฐ๊ฐ ๋์ง ์์๋ค)
(์์ ์ด ์ ๋์๋์ง ํ์ธํ๋ ๋ฐฉ๋ฒ์, ํฐ์บฃ์๋ฒ๋ก ์คํํ ๋ค์ ๊ฒฝ๋ก๊ฐ /fileboard/ ๋ก ์ ์ฐํ๋ ์ง ํ์ธํ๋ค.)
๋ค์ด๋ก๋ ๋ฐ์์ WEB-INF/libs ํด๋์ .jarํ์ผ์ ์ถ๊ฐํด์ค๋ค.
http://commons.apache.org/proper/commons-fileupload/
https://commons.apache.org/proper/commons-io/download_io.cgi
ํ์ผ์ ๋ก๋ ๊ธ์ฐ๊ธฐ
write.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<!DOCTYPE html>
<c:set var="root" value="${pageContext.request.contextPath }" />
<html>
<head>
<meta charset="UTF-8">
<title>ํ์ผ๋ณด๋ ๊ธ์์ฑ ํ์ด์ง</title>
<link rel="stylesheet" href="${root}/css/board/boardStyle.css" />
<script type="text/javascript" >
function boardCheck(obj){
//alert("OK");
}
function replyFunc(root, boardNumber, groupNumber, sequenceNumber, sequenceLevel){
var url = root + "/fileboard/write.do?boardNumber=" + boardNumber +
"&groupNumber=" + groupNumber + "&sequenceNumber=" + sequenceNumber +
"&sequenceLevel=" + sequenceLevel;
location.href = url;
}
function delFunc(root, boardNumber, pageNumber){
var url = root+ "/fileboard/delete.do?boardNumber=" + boardNumber + "&pageNumber=" + pageNumber;
location.href = url;
/*var value = confirm("์ญ์ ํ์๊ฒ ์ต๋๊น?");
if(value == true){
var url = root+ "/board/deleteOk.do?boardNumber=" + boardNumber + "&pageNumber=" + pageNumber;
alert(url);
}else{
alert("์ทจ์๋์์ต๋๋ค.");
}*/
}
function updFunc(root, boardNumber, pageNumber){
var url = root + "/fileboard/update.do?boardNumber=" + boardNumber + "&pageNumber=" + pageNumber;
location.href = url;
}
</script>
</head>
<body>
<form id="createform" action="${root}/fileboard/writeOk.do" method="post"
onsubmit="return boardCheck(this)" enctype="multipart/form-data">
<div id="title"><a href="${root}/fileboard/list.do">๊ธ๋ชฉ๋ก</a></div>
<div class="menu" style="border-bottom-width: 0px;">
<input type="hidden" name="boardNumber" value="${boardNumber}" />
<input type="hidden" name="groupNumber" value="${groupNumber}" />
<input type="hidden" name="sequenceNumber" value="${sequenceNumber}" />
<input type="hidden" name="sequenceLevel" value="${sequenceLevel}" />
<div id="id">์์ฑ์</div>
<span>
<input type="text" name="writer" size="12" />
</span>
</div>
<div class="menu" style="border-bottom-width: 0px;">
<div id="id">์ ๋ชฉ</div>
<span>
<input type="text" name="subject" size="50" />
</span>
</div>
<div class="menu" style="border-bottom-width: 0px;">
<div id="id">์ด๋ฉ์ผ</div>
<span>
<input type="email" name="email" size="50" />
</span>
</div>
<div class="content" style="border-bottom-width: 0px;">
<div id="text">๋ด์ฉ</div>
<span>
<textarea name="content" rows="12" cols="65"></textarea>
</span>
</div>
<div class="menu" style="border-bottom-width: 0px;">
<div id="id">๋น๋ฐ๋ฒํธ</div>
<span>
<input type="password" name="password" size="30" />
</span>
</div>
<!-- ํ์ผ ์
๋ก๋ -->
<div class="menu" style="border-bottom-width: 0px;">
<div id="id">ํ์ผ๋ช
</div>
<span>
<input type="file" name="file" size="50" />
</span>
</div>
<div class="menu" style="border-bottom-width: 3px; text-align: center;">
<span>
<input type="submit" value="๊ธ์ฐ๊ธฐ" />
<input type="reset" value="๋ค์์์ฑ" />
<input type="button" value="๋ชฉ๋ก๋ณด๊ธฐ" onclick="location.href='${root}/fileboard/list.do'"/>
</span>
</div>
</form>
</body>
</html>
WriteCommand.java
package com.java.fileboard.command;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.java.command.Command;
public class WriteCommand implements Command {
@Override
public String proRequest(HttpServletRequest request, HttpServletResponse response) throws Exception {
//๋ถ๋ชจ๊ธ(ROOT)
int boardNumber = 0; //ROOT๊ธ์ด๋ฉด 0
int groupNumber = 1; //๊ทธ๋ฃน๋ฒํธ
int sequenceNumber = 0; //๊ธ์์
int sequenceLevel = 0; //๊ธ๋ ๋ฒจ
//๋ต๊ธ์ธ๊ฒฝ์ฐ ๋ถ๋ชจ๊ธ์ DB ๊ธ๋ฒํธ, ๊ทธ๋ฃน๋ฒํธ, ๊ธ์์, ๊ธ๋ ๋ฒจ
if(request.getParameter("boardNumber")!=null) {
boardNumber = Integer.parseInt(request.getParameter("boardNumber"));
groupNumber = Integer.parseInt(request.getParameter("groupNumber"));
sequenceNumber = Integer.parseInt(request.getParameter("sequenceNumber"));
sequenceLevel = Integer.parseInt(request.getParameter("sequenceLevel"));
}
logger.info(logMsg +"boardNumber:" + boardNumber);
logger.info(logMsg +"groupNumber:" + groupNumber);
logger.info(logMsg +"sequenceNumber:" + sequenceNumber);
logger.info(logMsg +"sequenceLevel:" + sequenceLevel);
request.setAttribute("boardNumber", boardNumber);
request.setAttribute("groupNumber", groupNumber);
request.setAttribute("sequenceNumber", sequenceNumber);
request.setAttribute("sequenceLevel", sequenceLevel);
return "/WEB-INF/views/fileboard/write.jsp";
}
}
WriteOkCommand.java
package com.java.fileboard.command;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
import com.java.command.Command;
import com.java.fileboard.model.BoardDao;
import com.java.fileboard.model.BoardDto;
public class WriteOkCommand implements Command {
@Override
public String proRequest(HttpServletRequest request, HttpServletResponse response) throws Exception {
DiskFileItemFactory factory = new DiskFileItemFactory(); //ํ์ผ ๋ณด๊ด ๊ฐ์ฒด
ServletFileUpload upload = new ServletFileUpload(factory); //์์ฒญ ์ฒ๋ฆฌ ๊ฐ์ฒด
List<FileItem> list = upload.parseRequest(request);
Iterator<FileItem> iter = list.iterator();
BoardDto boardDto = new BoardDto();
HashMap<String, String> dataMap = new HashMap<String, String>();
while(iter.hasNext()) { //11๊ฐ
FileItem fileItem = iter.next();
if(fileItem.isFormField()) {
String name=fileItem.getFieldName();
String value=fileItem.getString("utf-8");
logger.info(logMsg + name + "\t" + value);
dataMap.put(name, value);
/*
* String name=(fileItem.getFieldName());
* logger.info(logMsg + "text: " + name);
*/
}else {
if(fileItem.getFieldName().equals("file")) {
// ํ์ผ๋ช
fileItem.getName() / ํ์ผ์ฌ์ด์ฆ fileItem.getSize(), getInputStream()
if(fileItem.getName()==null||fileItem.getName().equals("")) continue;
upload.setFileSizeMax(1024*1024*10); // byte * kb * mb * gb
String fileName = System.currentTimeMillis() + "_"+fileItem.getName();
//์ ๋ ๊ฒฝ๋ก
String dir = "C:\\sohyunkim\\mvc\\workspace\\MVCHomePage\\WebContent\\pds";
File file = new File(dir, fileName);
//ํฐ์บฃ ์ค์ ์๋ฒ ๊ฒฝ๋ก
//String dir = request.getServletContext().getRealPath("\\pds\\");
//logger.info(logMsg + dir);
//File dir = new File("C:\\pds\\"); dir.mkdir();
//File file = null;
//if(dir.exists() && dir.isDirectory()) {
// file = new File(dir, fileItem.getName());
//}
BufferedInputStream bis = null;
BufferedOutputStream bos = null;
try {
bis = new BufferedInputStream(fileItem.getInputStream(), 1024);
bos = new BufferedOutputStream( new FileOutputStream(file), 1024);
while(true) {
int data = bis.read();
if(data==-1) break;
bos.write(data);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
if(bis!=null) bis.close();
if(bos!=null) bos.close();
}
boardDto.setFileName(fileName);
boardDto.setFileSize(fileItem.getSize());
boardDto.setPath(file.getAbsolutePath());
}
}
}
boardDto.setDataMap(dataMap);
boardDto.setWriteDate(new Date());
logger.info(logMsg + boardDto.toString());
int check = BoardDao.getInstance().insert(boardDto);
logger.info(logMsg + check);
request.setAttribute("check", check);
return "/WEB-INF/views/fileboard/writeOk.jsp";
}
}
ํ์ผ์ ๋ก๋ ๊ธ ์ฝ๊ธฐ
read.jsp
<%@ 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="fmt" uri="http://java.sun.com/jsp/jstl/fmt"%>
<!DOCTYPE html>
<c:set var="root" value="${pageContext.request.contextPath}" />
<html>
<head>
<meta charset="UTF-8">
<title>ํ์ผ๋ณด๋ ์ฝ๊ธฐ ํ์ด์ง</title>
<link rel="stylesheet" href="${root}/css/board/boardStyle.css" />
<script type="text/javascript">
function boardCheck(obj){
//alert("OK");
}
function replyFunc(root, boardNumber, groupNumber, sequenceNumber, sequenceLevel){
var url = root + "/fileboard/write.do?boardNumber=" + boardNumber +
"&groupNumber=" + groupNumber + "&sequenceNumber=" + sequenceNumber +
"&sequenceLevel=" + sequenceLevel;
location.href = url;
}
function delFunc(root, boardNumber, pageNumber){
var url = root+ "/fileboard/delete.do?boardNumber=" + boardNumber + "&pageNumber=" + pageNumber;
location.href = url;
/*var value = confirm("์ญ์ ํ์๊ฒ ์ต๋๊น?");
if(value == true){
var url = root+ "/board/deleteOk.do?boardNumber=" + boardNumber + "&pageNumber=" + pageNumber;
alert(url);
}else{
alert("์ทจ์๋์์ต๋๋ค.");
}*/
}
function updFunc(root, boardNumber, pageNumber){
var url = root + "/fileboard/update.do?boardNumber=" + boardNumber + "&pageNumber=" + pageNumber;
location.href = url;
}
</script>
</head>
<body>
<div id="createform">
<div class="menu" style="border-bottom-width: 0px;">
<div id="id">๊ธ๋ฒํธ</div>
${boardDto.boardNumber}
</div>
<div class="menu" style="border-bottom-width: 0px;">
<div id="id">์์ฑ์</div>
${boardDto.writer}
</div>
<div class="menu" style="border-bottom-width: 0px;">
<div id="id">์กฐํ์</div>
${boardDto.readCount}
</div>
<div class="menu" style="border-bottom-width: 0px;">
<div id="id">์์ฑ์ผ</div>
<fmt:formatDate value="${boardDto.writeDate}" pattern="yyyy-MM-dd HH:mm:ss"/>
</div>
<div class="menu" style="border-bottom-width: 0px;">
<div id="id">์ ๋ชฉ</div>
${boardDto.subject}
</div>
<div class="content" style="border-bottom-width: 0px;">
<div id="text">๋ด์ฉ</div>
${boardDto.content}
</div>
<c:if test="${boardDto.fileSize!=0}">
<div class="menu" style="border-bottom-width: 0px;">
<div id="id">ํ์ผ๋ช
</div>
<a href="${root}/fileboard/download.do?boardNumber=${boardDto.boardNumber}">${boardDto.fileName}</a>
</div>
</c:if>
<div class="menu" style="border-bottom-width: 3px; text-align: center;">
<input type="button" value="๊ธ์์ " onclick="updFunc('${root}','${boardDto.boardNumber}','${pageNumber}')"/>
<input type="button" value="๊ธ์ญ์ " onclick="delFunc('${root}','${boardDto.boardNumber}','${pageNumber}')"/>
<input type="button" value="๋ต๊ธ" onclick="replyFunc('${root}','${boardDto.boardNumber}','${boardDto.groupNumber}','${boardDto.sequenceNumber}','${boardDto.sequenceLevel}')"/>
<input type="button" value="๊ธ๋ชฉ๋ก" onclick="location.href='${root}/fileboard/list.do?pageNumber=${pageNumber}'"/>
</div>
</div>
</body>
</html>
ReadCommand.java
package com.java.fileboard.command;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.java.fileboard.model.BoardDao;
import com.java.fileboard.model.BoardDto;
import com.java.command.Command;
public class ReadCommand implements Command {
@Override
public String proRequest(HttpServletRequest request, HttpServletResponse response) throws Exception {
int boardNumber = Integer.parseInt(request.getParameter("boardNumber"));
int pageNumber = Integer.parseInt(request.getParameter("pageNumber"));
logger.info(logMsg + boardNumber + "," + pageNumber);
BoardDto boardDto = BoardDao.getInstance().read(boardNumber);
logger.info(logMsg + boardDto);
if(boardDto.getFileSize()!=0) {
int index = boardDto.getFileName().indexOf("_")+1;
boardDto.setFileName(boardDto.getFileName().substring(index));
}
request.setAttribute("pageNumber", pageNumber);
request.setAttribute("boardDto", boardDto);
return "/WEB-INF/views/fileboard/read.jsp";
}
}
ํ์ผ์ ๋ก๋ ๊ธ ์์
update.jsp
UpdateCommand.java
updateOk.jsp
UpdateOkCommand.java
[์์ ]
๊ธฐ์กด ๊บผ ์ง์ฐ๊ณ ์๋ก ์ฌ๋ฆฐ๋ค.
ํ์ผ ์ด๋ฆ์ด ์์ด๋ ์ ๋ก๋ ๋ฒํผ์ด ์์ด์ผํจ.
์ด๋ฆ ์์ด๋ ๋นํ๋ฉด์ผ๋ก
ํ์ผ์ ๋ก๋ ๊ธ ์ญ์
delete.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri = "http://java.sun.com/jsp/jstl/core" %>
<!DOCTYPE html>
<c:set var = "root" value = "${pageContext.request.contextPath}"/>
<html>
<head>
<meta charset="UTF-8">
<title>ํ์ผ๋ณด๋ ์ญ์ ํ์ด์ง</title>
</head>
<body>
<div align="center">
<form action="${root}/fileboard/deleteOk.do" method="post">
<input type="hidden" name="boardNumber" value="${boardNumber}"/>
<input type="hidden" name="pageNumber" value="${pageNumber}"/>
<div>๋น๋ฐ๋ฒํธ๋ฅผ ์
๋ ฅํด์ฃผ์ธ์.</div>
<div>
<input type="password" name="password"/>
</div>
<div>
<input type="submit" value="๊ธ์ญ์ "/>
<input type="button" value="๋ชฉ๋ก๋ณด๊ธฐ" onclick="location.href='${root}/fileboard/list.do?pageNumber=${pageNumber}'"/>
</div>
</form>
</div>
</body>
</html>
DeleteCommand.java
package com.java.fileboard.command;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.java.command.Command;
public class DeleteCommand implements Command {
@Override
public String proRequest(HttpServletRequest request, HttpServletResponse response) throws Exception {
int boardNumber = Integer.parseInt(request.getParameter("boardNumber"));
int pageNumber = Integer.parseInt(request.getParameter("pageNumber"));
logger.info(logMsg + boardNumber + "\t" + pageNumber);
request.setAttribute("boardNumber", boardNumber);
request.setAttribute("pageNumber", pageNumber);
return "/WEB-INF/views/fileboard/delete.jsp";
}
}
deleteOk.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri = "http://java.sun.com/jsp/jstl/core" %>
<!DOCTYPE html>
<c:set var = "root" value = "${pageContext.request.contextPath}"/>
<html>
<head>
<meta charset="UTF-8">
<title>ํ์ผ๋ณด๋ ์ญ์ ์๋ฃํ์ด์ง</title>
</head>
<body>
<c:if test="${check > 0}">
<script type="text/javascript">
alert("์ญ์ ๋์์ต๋๋ค.");
location.href="${root}/fileboard/list.do?pageNumber=${pageNumber}";
</script>
</c:if>
<c:if test="${check == 0}">
<script type="text/javascript">
alert("์ญ์ ๋์ง ์์์ต๋๋ค.");
location.href="${root}/fileboard/list.do?pageNumber=${pageNumber}";
</script>
</c:if>
</body>
</html>
deleteOkCommand.java
package com.java.fileboard.command;
import java.io.File;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.java.command.Command;
import com.java.fileboard.model.BoardDao;
import com.java.fileboard.model.BoardDto;
public class DeleteOkCommand implements Command {
@Override
public String proRequest(HttpServletRequest request, HttpServletResponse response) throws Exception {
int boardNumber = Integer.parseInt(request.getParameter("boardNumber"));
int pageNumber = Integer.parseInt(request.getParameter("pageNumber"));
String password = request.getParameter("password");
logger.info(logMsg + boardNumber + "\t" + pageNumber + "\t" + password);
BoardDto readBoard = BoardDao.getInstance().select(boardNumber);
int check = BoardDao.getInstance().delete(boardNumber, password);
logger.info(logMsg + check);
if(check>0 && readBoard.getPath()!=null) {
// ํ์ผ ์ง์ฐ๊ธฐ
File file = new File(readBoard.getPath());
if(file.exists() && file.isFile()) {
file.delete();
}
}
request.setAttribute("check", check);
request.setAttribute("pageNumber", pageNumber);
return "/WEB-INF/views/fileboard/deleteOk.jsp";
}
}