본문 바로가기

JSP

모델 1 자료실 만들기~~

pdswrite 만들기

더보기
<%@page import="java.util.Date"%>
<%@page import="dto.MemberDto"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>

<%
MemberDto mem = (MemberDto)session.getAttribute("login");

// file명 변경
//String filename = (new Date().getTime()) + "";
//System.out.println("filename:" + filename);

// mydata.txt -> 1616379133665.txt
// original filename : mydata.txt
// filename : 1616379133665.txt		-> 1616379133665.tmp
// 1616379133665.txt -> mydata.txt
%>        
    
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>

<h2>자료 올리기</h2>

<div align="center">

<%--
	id		-> String form field data
	title
	content

	file	-> byte
 --%>

<form action="pdsupload.jsp" method="post" enctype="multipart/form-data">

<table border="1">
<col width="200"><col width="500">

<tr>
	<th>아이디</th>
	<td>
		<input type="text" name="id" value="<%=mem.getId() %>" readonly="readonly">
	</td>
</tr>

<tr>
	<th>파일 업로드</th>
	<td>
		<input type="file" name="fileload" style="width: 400px">
	</td>
</tr>

<tr>
	<th>제목</th>
	<td>
		<input type="text" name="title">
	</td>
</tr>

<tr>
	<th>내용</th>
	<td>
		<textarea rows="20" cols="50" name="content"></textarea>
	</td>
</tr>

<tr align="center">
	<td colspan="2">
		<input type="submit" value="올리기">
	</td>
</tr>

</table>

</form> 

</div>




</body>
</html>

pdslist 만들기

더보기
<%@page import="dto.MemberDto"%>
<%@page import="dto.PdsDto"%>
<%@page import="java.util.List"%>
<%@page import="dao.PdsDao"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>

<%
PdsDao dao = PdsDao.getInstance();
List<PdsDto> list = dao.getPdsList();

MemberDto mem = (MemberDto)session.getAttribute("login");
%>    
   
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>

<h2>자료실</h2>

<div align="center">

<table border="1">
<col width="70"><col width="100"><col width="400"><col width="100">
<col width="80"><col width="120"><col width="120">

<tr>
	<th>번호</th><th>작성자</th><th>제목</th><th>다운로드</th>
	<th>조회수</th><th>다운로드수</th><th>작성일</th>
</tr>

<%
if(list.size() == 0){
%>
	<tr>
		<td colspan="7">자료가 없습니다</td>
	</tr>
<%
}else{
	
	for(int i = 0;i < list.size(); i++){
		PdsDto pds = list.get(i);	
		%>
		<tr align="center" height="5">
			<th><%=i+1 %></th>
			<td><%=pds.getId() %></td>
			<td>
				<a href="pdsdetail.jsp?seq=<%=pds.getSeq() %>">
					<%=pds.getTitle() %>
				</a>
			</td>
			<td>
				<input type="button" name="btndown" value="파일"
					onclick="location.href='filedown?filename=<%=pds.getFilename() %>&seq=<%=pds.getSeq() %>'">         
			</td>
			<td><%=pds.getReadcount() %></td>
			<td><%=pds.getDowncount() %></td>
			<td><%=pds.getRegdate() %></td>
		</tr>
		<%
	}
}
%>
</table>

<br>

<a href="pdswrite.jsp">자료 올리기</a>

</div>


</body>
</html>

psdupload 만들기 

더보기
<%@page import="dto.PdsDto"%>
<%@page import="dao.PdsDao"%>
<%@page import="java.util.Iterator"%>
<%@page import="java.util.List"%>
<%@page import="org.apache.commons.fileupload.disk.DiskFileItemFactory"%>
<%@page import="org.apache.commons.fileupload.servlet.ServletFileUpload"%>
<%@page import="java.io.File"%>
<%@page import="java.io.IOException"%>
<%@page import="org.apache.commons.fileupload.FileItem"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
    
<%!
// upload 함수
public String processUploadFile(FileItem fileItem, String dir)throws IOException{

	String filename = fileItem.getName();	// 경로 + 파일명
	long sizeInBytes = fileItem.getSize();
	
	if(sizeInBytes > 0){	// 파일이 정상?		// d:\\tmp\\abc.txt d:/tmp/abc.txt 
		
		int idx = filename.lastIndexOf("\\"); 
		if(idx == -1){
			idx = filename.lastIndexOf("/");
		}
		
		filename = filename.substring(idx + 1);
		File uploadFile = new File(dir, filename);
	
		try{	
			fileItem.write(uploadFile);		// 실제 upload되는 부분
		}catch(Exception e){
			e.printStackTrace();
		}		
	}
	return filename;	// DB에 저장하기 위한 return;
}
%>    
    
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>

<%
// tomcat 배포 - server
String fupload = application.getRealPath("/upload");

// 지정 폴더 - client
// String fupload = "d:\\tmp";

System.out.println("업로드 폴더:" + fupload);

String yourTempDir = fupload;

int yourMaxRequestSize = 100 * 1024 * 1024;	// 1 Mbyte
int yourMaxMemorySize = 100 * 1024;			// 1 Kbyte

// form field의 데이터를 저장할 변수
String id = "";
String title = "";
String content = "";

// file명 저장
String filename = "";

boolean isMultipart = ServletFileUpload.isMultipartContent(request);
if(isMultipart == true){
	
	// FileItem 생성
	DiskFileItemFactory factory = new DiskFileItemFactory();
	
	factory.setSizeThreshold(yourMaxMemorySize);
	factory.setRepository(new File(yourTempDir));
	
	ServletFileUpload upload = new ServletFileUpload(factory);
	upload.setSizeMax(yourMaxRequestSize);
	
	List<FileItem> items = upload.parseRequest(request);
	Iterator<FileItem> it = items.iterator();
	
	while(it.hasNext()){
		
		FileItem item = it.next();
		
		if(item.isFormField()){	// id, title, content
			if(item.getFieldName().equals("id")){
				id = item.getString("utf-8");
			}
			else if(item.getFieldName().equals("title")){
				title = item.getString("utf-8");
			}
			else if(item.getFieldName().equals("content")){
				content = item.getString("utf-8");
			}			
		}
		else{	// file
			if(item.getFieldName().equals("fileload")){
				filename = processUploadFile(item, fupload);
			}
		}		
	}	
}

// DB에 저장
PdsDao dao = PdsDao.getInstance();
boolean isS = dao.writePds(new PdsDto(id, title, content, filename));
if(isS){
	%>
	<script type="text/javascript">
	alert('파일 업로드 성공');
	location.href = "pdslist.jsp";
	</script>
	<%
}else{
	%>
	<script type="text/javascript">
	alert('파일 업로드 실패');
	location.href = "pdswrite.jsp";
	</script>
	<%
}
%>

</body>
</html>

■ PdsDao 만들기

더보기
package dto;

import java.io.Serializable;

// PDS - Public Domain Software = 자료실
public class PdsDto implements Serializable {

	private int seq;
	private String id;
	private String title;
	private String content;	
	private String filename;
	private int readcount;
	private int downcount;	
	private String regdate;
	// private String originalFilename;
	
	public PdsDto() {
	}

	public PdsDto(int seq, String id, String title, String content, String filename, int readcount, int downcount,
			String regdate) {
		super();
		this.seq = seq;
		this.id = id;
		this.title = title;
		this.content = content;
		this.filename = filename;
		this.readcount = readcount;
		this.downcount = downcount;
		this.regdate = regdate;
	}

	public PdsDto(String id, String title, String content, String filename) {
		super();
		this.id = id;
		this.title = title;
		this.content = content;
		this.filename = filename;
	}

	public int getSeq() {
		return seq;
	}

	public void setSeq(int seq) {
		this.seq = seq;
	}

	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

	public String getTitle() {
		return title;
	}

	public void setTitle(String title) {
		this.title = title;
	}

	public String getContent() {
		return content;
	}

	public void setContent(String content) {
		this.content = content;
	}

	public String getFilename() {
		return filename;
	}

	public void setFilename(String filename) {
		this.filename = filename;
	}

	public int getReadcount() {
		return readcount;
	}

	public void setReadcount(int readcount) {
		this.readcount = readcount;
	}

	public int getDowncount() {
		return downcount;
	}

	public void setDowncount(int downcount) {
		this.downcount = downcount;
	}

	public String getRegdate() {
		return regdate;
	}

	public void setRegdate(String regdate) {
		this.regdate = regdate;
	}

	@Override
	public String toString() {
		return "PdsDto [seq=" + seq + ", id=" + id + ", title=" + title + ", content=" + content + ", filename="
				+ filename + ", readcount=" + readcount + ", downcount=" + downcount + ", regdate=" + regdate + "]";
	}
	
}

PdsDto 만들기

'JSP' 카테고리의 다른 글

JSP Tag Change 화면 바꾸기 실습  (0) 2021.03.23
EL Taggggggggggggggggggggggg  (0) 2021.03.23
Core Taggggggggggggggggggggggggggggggg  (0) 2021.03.23