2013년 7월 17일 수요일

(130717) 18일차 FileUpload.jsp, FileUploadProc.jsp (JSP를 이용한 파일 업로드 테스트 - MultipartRequest 메소드)

 - 파일 전송 방식 (파일 업로드)
 : <form action = "..." method="post" enctype="multipart/form-data"> 를 하면 찾아보기 버튼이 자동으로 생긴다.
 : JAVA(서블릿)에서 파일은 Stream으로 넘어가기 때문에 getInputStream, getOutputStream을 이용해서 주고 받는다.
 : JSP에서는 다른 방식을 사용한다.
 : 하지만 남들이 만들어놓은 Java 파일(패키지)을 이용해서 파일 업로드를 하는 것이 더 효율적이다.
 : cos.jar 파일을 이용해서 파일 업로드를 수행한다.

에서 cos-26Dec2008.zip를 다운받으면 cos.jar 파일의 API를 볼 수 있다.


 - cos.jar파일 저장























 - 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>Insert title here</title>
</head>
<body>
<center>
<h2> 파일 업로드 </h2>
<form action = "FileUploadProc.jsp" method = "post" enctype = "multipart/form-data">
<table border = "0" cellpadding="8" cellspacing="5">
    <tr align = "center">
<th> 작성자 : </th>
<td> <input type = "text" name = "writer"> </td>
</tr>
<tr align = "center">
<th> 제목 : </th>
<td> <input type = "text" name = "subject"> </td>
</tr>
<tr align = "center">
<th> 파일명 : </th>
<td> <input type = "file" name = "upload"> </td>
</tr>

<tr align = "center">
<td colspan = "2"> <input type = "submit" value = "전송"> </td>
</tr>
</table>
</form>
</center>
</body>
</html>


 - MultipartRequest 메소드
























 - DefaultFileRenamePolicy 클래스





















 - 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>Insert title here</title>
</head>
<body>

<%
// 프로젝트 내에 저장된 폴더를 지정하고 이름을 얻어오는 변수
String realFolder = "";

// 만든 폴더의 이름을 설정
String saveFolder = "File";
String encType = "euc-kr";
// 서버측에 저장될 파일의 크기를 설정
int maxSize = 1024*1024*5; // 5Mb (post 방식으로 넘어온 파일의 사이즈를 제한)

// Context에 대한 정보를 알아옴
ServletContext context = getServletContext(); // 서블릿에 대한 환경정보를 가져옴
// 실제 파일이 저장될 폴더의 경로를 얻어옴
realFolder = context.getRealPath(saveFolder); // File 폴더의 경로를 얻어옴
out.println("현재 절대 경로 : " + realFolder + "<br><br>");

try{
// 클라이언트 측에서 보낸 파일과 파라미터를 읽어 드리는 클래스 객체를 선언
MultipartRequest multi = new MultipartRequest(request, realFolder, maxSize, encType, new DefaultFileRenamePolicy());

out.println("작성자 : " + multi.getParameter("writer") + "<br>");
out.println("제목 : " + multi.getParameter("subject") + "<br><br>");


// 파일에 대한 정보를 화면에 출력
// 저장된 파일의 데이터를 모두 가져옴
Enumeration files = multi.getFileNames();

// 반복문을 이용하여 저장된 파일을 꺼내서 화면에 출력
while(files.hasMoreElements()){
// 객체에 저장된 데이터의 이름을 스트링으로 가져옴
String fileName = (String)files.nextElement();
out.println("Form 안에 있는 파일 태그 이름 : " + fileName + "<br>");
out.println("서버에 저장된 이름 : " + multi.getFilesystemName(fileName) + "<br>");
out.println("원 파일 이름 : " + multi.getOriginalFileName(fileName) + "<br>");
out.println("전송된 파일의 타입 : " + multi.getContentType(fileName) + "<br>");

// 파일 객체로 변환
File file = multi.getFile(fileName);
out.println("파일 길이 : " + file.length() + "<br><br>");
}

// 입력 Form의 파라미터 목록을 가져옴
Enumeration params = multi.getParameterNames();

// 어떤 입력 태그가 있었는지 파라미터를 출력
while(params.hasMoreElements()){ 
String name = (String)params.nextElement(); // input tag 이름
  String value = multi.getParameter(name); // input tag 값  
out.println(name + " = " + value +"<br>");
}

} catch(Exception e){
}
%>
</body>
</html>


 - 결과