웹 프로그래밍 18(JSP,DB)-파일 업로드
웹 프로그래밍 18(JSP,DB)-파일 업로드
*파일 업로드 - 라이브러리 세팅
-파일 업로드에 필요한 라이브러리를 받아 놓아야한다.
servlets.com => com.oreilly.servlet => Download - cos-20.08.zip를 다운받는다.
-받은 파일을 압축푼 후, lib=>cos.jar 파일을 복사한다.
-복사한 파일을 프로젝트=>webContent=>WEB-INF=>lib에 붙여 넣기 해주면 끝난다.
*파일 업로드 - 소스예제
-uploadForm.jsp
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
<%@ 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>
<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> |
form을 enctype="multipart/form-data" 추가해준다.
-uploadOk.jsp
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
<%@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 = "D:\\uploadFolder"; //String path = application.getRealPath("fileFolder"); //getRealPath를 사용하면 경로값이 null로 잡힌다. 이유를 아시는분 쪽지 부탁드립니다.
int size = 1024 * 1024 * 10; String file = "";
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); }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 success! </body> </html> |
-출력화면
uploadForm.jsp
uploadOk.jsp
-파일이 생성된 것을 확인
'프로그래밍 정리 > JSP, Servlet, DB(oracle)' 카테고리의 다른 글
웹 프로그래밍 19-2(JSP)-EL과 빈(bean) (0) | 2020.01.06 |
---|---|
웹 프로그래밍 19-1(JSP)-EL(Expression Language) (0) | 2020.01.06 |
웹 프로그래밍 17(JSP,DB)-커넥션 풀 (0) | 2020.01.05 |
웹 프로그래밍 16(JSP,DB)-PreparedStatement (0) | 2020.01.05 |
웹 프로그래밍 15(JSP,DB)-DAO, DTO (1) | 2020.01.03 |