-
211021_파일입출력JSP 2021. 10. 21. 12:21
uploadform.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<link href="https://fonts.googleapis.com/css2?family=Do+Hyeon&display=swap" rel="stylesheet">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/css/bootstrap.min.css">
<script src="https://code.jquery.com/jquery-3.5.0.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/js/bootstrap.min.js"></script>
<title>Insert title here</title>
</head>
<body>
<form action="uploadaction.jsp" method="post" enctype="multipart/form-data">
<table class="table table-bordered" style="width: 500px;">
<tr>
<th width="100">이름</th>
<td>
<input type="text" name="name">
</td>
</tr>
<tr>
<th width="100">제목</th>
<td>
<input type="text" name="title">
</td>
</tr>
<tr>
<th width="100">파일</th>
<td>
<input type="file"name="uploadFile">
</td>
</tr>
<tr>
<td colspan="2" align="center">
<input type="submit" value="업로드">
</td>
</tr>
</table>
</form>
</body>
</html>
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
uploadaction.jsp
<%@page import="com.oreilly.servlet.multipart.DefaultFileRenamePolicy"%>
<%@page import="com.oreilly.servlet.MultipartRequest"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<link href="https://fonts.googleapis.com/css2?family=Do+Hyeon&display=swap" rel="stylesheet">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/css/bootstrap.min.css">
<script src="https://code.jquery.com/jquery-3.5.0.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/js/bootstrap.min.js"></script>
<title>Insert title here</title>
</head>
<body>
<%
String folder="/save(1021)"; //가상의 폴더
ServletContext context=getServletContext();
String realFolder=context.getRealPath(folder); //실제 폴더
//실제 폴더가 어딘지 찍어보기
System.out.println("folder: "+realFolder);
int filesize=1024*1024*5; //5mb
MultipartRequest multi=null;
try{
multi=new MultipartRequest(request,realFolder,filesize,"utf-8",
new DefaultFileRenamePolicy());
String name=multi.getParameter("name");
String title=multi.getParameter("title");
String uploadFileName=multi.getFilesystemName("uploadFile");
String originalFileName=multi.getOriginalFileName("uploadFile");
%>
<table style="width: 300px; border: 1px;">
<tr>
<th width="100">이름</th>
<td><%=name %></td>
</tr>
<tr>
<th width="100">제목</th>
<td><%=title %></td>
</tr>
<tr>
<th width="100">업로드 파일명</th>
<td><img alt="" src="../save(1021)/<%=uploadFileName%>"></td>
</tr>
<tr>
<th width="100">원래 파일명</th>
<td><%=originalFileName %></td>
</tr>
<tr>
<td colspan="2" align="center">
<input type="button" value="다시 업로드"
onclick="location.href='uploadform.jsp'">
</td>
</tr>
</table>
<%} catch(Exception e){
System.out.println("업로드오류: "+e.getMessage());
}%>
</body>
</html>
'JSP' 카테고리의 다른 글
211021_파일입출력을 이용해서 게시판 만들기 (0) 2021.10.21 211021_파일입출력(여러개) (0) 2021.10.21 211020_session (0) 2021.10.20 211020_session으로 로그인/로그아웃 (0) 2021.10.20 211020_jsp로 게시판 만들기 (0) 2021.10.20