파일업로드
[파일업로드 구현]
- 파일업로드 라이브러리 다운로드
. http://www.servlets.com 접속
. com.oreilly.servlet 메뉴를 선택
. cos-26Dec2008.zip 파일을 다운로드
- 위치지정
. 압축을 푼 후에 cos.jar 파일
. WebContent/WEB-INF/lib/ 에 파일 복사
- 업로드 파일을 저장하기 위한 폴더 생성
. WebContent 폴더 아래에 업로드 파일을 저장할 폴더를 생성
-------------------------------------------------------------------------------------------------------------------------------------
uploadFile.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!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=UTF-8">
<title>Insert title here</title>
</head>
<body>
<form action="fileOk.jsp" method="post" enctype="multipart/form-data">
파일명:<input type="file" name="t1file"><br>
파일명:<input type="file" name="t2file"><br>
<input type="submit" value="전송">
</form>
</body>
</html>
--------------------------------------------------------------------------------------
fileOk.jsp
<%@ page import = "com.oreilly.servlet.MultipartRequest" %>
<%@ page import = "com.oreilly.servlet.multipart.DefaultFileRenamePolicy" %>
<%@ page import ="java.util.Enumeration" %>
<%@ page language="java" contentType="text/html; charset=EUC-KR"
pageEncoding="EUC-KR"%>
<%
String uploadPath = request.getRealPath("upload_file");
int maxSize = 1024*1024 * 10; // 10M
String file = "";
String orginFile="";
try{
MultipartRequest multi = new MultipartRequest(request, uploadPath, maxSize, "EUC-KR", new DefaultFileRenamePolicy());
Enumeration files = multi.getFileNames();
String names = (String)files.nextElement();
file = multi.getFilesystemName(names);
orginFile = multi.getOriginalFileName(names);
}catch (Exception e){
e.printStackTrace();
}
%>
<!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>
file Upload 완료!!!
</body>
</html>