본문 바로가기

카테고리 없음

22. 파일 업로드

1. 파일 업로드 라이브러리 설치

http://www.servlets.com -> com.oreilly.servlet -> 맨 아래 Download 항목의 cos-버전명.zip 다운로드

 

Servlets.com

Home What's New? com.oreilly.servlet Servlet Polls Mailing Lists List Archives Servlet Engines Servlet ISPs Servlet Tools Documentation Online Articles The Soapbox "Java Servlet Programming, Second Edition" "Java Enterprise Best Practices" Speaking & Slide

www.servlets.com

 

② 다운받은 파일의 압축을 해제하고, lib\cos.jar 파일을 WEB-INF\lib에 복사

 

cos.jar 저장 경로

 

# 업로드 라이브러리는 여러 종류가 있으며, 그 중 하나만 선택하여 실습한다

 

 

 

 

 

2. 파일 업로드 프로그래밍

① 업로드된 파일을 저장할 폴더를 WebContent의 하위 폴더로 생성

 - 이클립스의 워크벤치에 폴더를 생성하면 톰켓에서도 같은 경로에 동일한 이름의 폴더가 생성된다

 - 실제 파일은 톰켓의 폴더에 업로드 된다

 

 

 

uploadFolder의 위치

 

 

 

 

 

 

② 서버의 실제 경로 확인

서버의 속성창에서 Server path와 Deploy path 확인

 

Server path\Deploy path\ 에 프로젝트명으로 된 폴더가 생성된 모습

 

 

 

 

 

 

 

③ 파일 업로드 테스트

 

 - uploadForm.jsp: 업로드 페이지

<%@ page language="java" contentType="text/html; charset=EUC-KR"
    pageEncoding="EUC-KR"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="EUC-KR">
<title>파일 업로드 페이지</title>
</head>
<body>
	<form action="uploadOK.jsp" method="post" enctype="multipart/form-data">
		파일: <input type="file" name="file"><br />
		<input type="submit" value="File Upload">
	</form>

</body>
</html>

 

 

업로드 폼

 

 

파일 선택

 

 

업로드할 파일이 선택된 모습

 

 

 

 

 

 

 

 

 

 

- uploadOK.jsp : 업로드 처리 및 결과 페이지

<%@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"%>
<%
	String path = request.getRealPath("uploadFolder");

	int size = 1024 * 1024 * 10; // 최대 용량 10MB
	String file ="";
	String oriFile = "";
	
	try{
		MultipartRequest multi = new MultipartRequest(request, path, size, "EUC-KR", new DefaultFileRenamePolicy());
		
		Enumeration files = multi.getFileNames();
		String str = (String)files.nextElement();
		
		file = multi.getFilesystemName(str);
		oriFile = multi.getOriginalFileName(str);
		
	} catch (Exception e) {
		e.printStackTrace();
	}
%>
<!DOCTYPE html>
<html>
<head>
<meta charset="EUC-KR">
<title>Insert title here</title>
</head>
<body>
	파일 업로드 성공!!
</body>
</html>

 

업로드 결과 페이지

 

 

실제로 해당 경로에 파일이 업로드 되어 있는 모습