파일 업로드 방법이다.
먼저 톰캣 루트디렉토리에 uploadfiles 디렉토리를 생성한 뒤 실행한다.
참고문헌 - 열혈강의 프로그래머를 위한 서블릿/JSP
- upload.html -
<html><head><title>파일 업로드</title></head>
<body><h3>파일 업로드</h3>
<p>업로드할 파일을 선택해주세요
<p>
<form action="uploadfiles.jsp" method=post enctype="multipart/form-data">
<table>
<tr>
<td>제목</td>
<td><input type=text name=subject></td>
</tr>
<tr>
<td>파일 이름:</td>
<td><input type=file name=file1> </td>
</tr>
<tr>
<td>파일 이름:</td>
<td><input type=file name=file2></td>
</tr>
<tr>
<td colspan=2 align=center><input type=submit>
<input type=reset></td>
</tr>
</table>
</form></body></html>
- upload.jsp -
<%@ page contentType="text/html;charset=KSC5601" %>
<%@ page import="java.io.PrintWriter"%>
<%@ page import="com.oreilly.servlet.*"%>
<%@ page import="com.oreilly.servlet.multipart.*"%>
<% request.setCharacterEncoding("KSC5601"); %>
<html><head><title>파일 업로드</title></head>
<body><h3>파일 업로드</h3>
<%
String dir = application.getRealPath("/upload");
int max = 5 * 1024 * 1024; // 5MB
try {
MultipartRequest m = new MultipartRequest(request, dir, max,
"KSC5601", new DefaultFileRenamePolicy());
String subject = m.getParameter("subject");
String file1 = m.getFilesystemName("file1");
String ofile1 = m.getOriginalFileName("file1");
String file2 = m.getFilesystemName("file2");
String ofile2 = m.getOriginalFileName("file2");
%>
<p>제목 : <%= subject %>
<% if(file1 != null) { %>
<p>업로드 파일1 : <a href=/upload/<%= file1%>><%=ofile1%></a>
<% }
if(file2 != null) { %>
<p>업로드 파일2 : <a href=/upload/<%= file2%>><%=ofile2%></a>
<% }
}catch(Exception e) {
e.printStackTrace(new PrintWriter(out));
}
%>
</body></html>
댓글을 달아 주세요
또 다른 좋은 기사 주셔서 감사합니다. 어디 다른 사람이 작성 등 완벽한 방식으로 그런 정보를 얻을 수 있을까?