JSP 6일차 FileUpload2

FileUpload.jsp

<%@ page language="java" contentType="text/html; charset=EUC-KR"
    pageEncoding="EUC-KR"%>
<!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=EUC-KR">
<title>File Upload</title>
</head>
<body>

<center>
<h2>File Upload</h2>
<form action="FileUploadProc.jsp"  method="post" enctype="multipart/form-data">
<table>
<tr>
<td>작성자</td>
<td><input type="text" name="writer"></td>
</tr>
<tr>
<td>제목</td>
<td><input type="text" name="subject"></td>
</tr>
<tr>
<td>파일</td>
<td><input type="file" name="upload"></td>

</tr>
<tr >
<td></td>
</tr>
</table>

<input type="submit" value="send">
</form>
</center>

</body>
</html>

FileUploadProc.jsp

<%@page import="java.io.File"%>
<%@page import="java.util.Enumeration"%>
<%@page import="com.oreilly.servlet.multipart.DefaultFileRenamePolicy"%>
<%@page import="com.oreilly.servlet.MultipartRequest"%>
<%@ page language="java" contentType="text/html; charset=EUC-KR"
    pageEncoding="EUC-KR"%>
<!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=EUC-KR">
<title>File Upload Procedure</title>
</head>
<body>

<%
String realfolder = "";
String saveFolder = "File";
String encType = "euc-kr";
int maxSize = 1024 * 1024 * 5;
ServletContext context = getServletContext();
realfolder = context.getRealPath(saveFolder);

out.println(realfolder);
try
{
MultipartRequest multi = new MultipartRequest(
request, realfolder, maxSize, encType,
new DefaultFileRenamePolicy());
out.println(multi.getParameter("writer")+"<br>");
out.println(multi.getParameter("subject")+"<br>");
Enumeration files = multi.getFileNames();
while(files.hasMoreElements())
{
String filename = (String)files.nextElement();
out.println(multi.getFilesystemName(filename)+"<br>");
out.println(multi.getOriginalFileName(filename)+"<br>");
File file = multi.getFile(filename);
out.println(file.length());
out.println("<hr><br>");
}
}
catch(Exception e)
{
}
%>

</body>
</html>


result)





댓글 없음: