본문 바로가기

Java

파일업로드 실습

-- mkdir.jsp

<%@ page language="java" contentType="text/html; charset=EUC-KR"
    pageEncoding="EUC-KR"%>
<%@ page import="java.io.*" %>
<%@ page import="com.oreilly.servlet.multipart.*" %>
<%@ page import="com.oreilly.servlet.*" %>       
<%
 String encType = "euc-kr";
 int maxSize = 5* 1024 * 1024;
 String saveFolder = getServletContext().getInitParameter("saveFolder");
 String realFolder = getServletContext().getRealPath(saveFolder);
 System.out.println(saveFolder);
 System.out.println(realFolder);
 
 File file = new File(realFolder);
 
 if (file != null) {
  if ( !file.isDirectory()) {
   file.mkdir();
   System.out.println("디렉토리가 생성되었습니다.");
  } else {
   System.out.println("디렉토리 생성이 실패하였습니다.");
   response.sendRedirect("./index.jsp");
  }
 }
 
 MultipartRequest multi = new MultipartRequest(
   request,
   realFolder,
   maxSize,
   encType,
   new DefaultFileRenamePolicy());
 
 response.sendRedirect("./index.jsp");
%>

-- index.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>
<form method="post" name="form" action="mkdir.jsp" enctype="multipart/form-data">
<input type="file" name="file" /><br/>
<input type="submit" />
</form>
</body>
</html>

-- web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app id="WebApp_ID" version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
 <display-name>test</display-name>
 <context-param>
  <param-name>saveFolder</param-name>
  <param-value>/bbb</param-value>
 </context-param>
 <welcome-file-list>
  <welcome-file>index.html</welcome-file>
  <welcome-file>index.htm</welcome-file>
  <welcome-file>index.jsp</welcome-file>
  <welcome-file>default.html</welcome-file>
  <welcome-file>default.htm</welcome-file>
  <welcome-file>default.jsp</welcome-file>
 </welcome-file-list>
</web-app>

'Java' 카테고리의 다른 글

자꾸 까먹는 3항 연산자.  (0) 2009.12.11
UTF-8로 개발환경을 도배하자.  (0) 2009.12.10
iBatis 에서 테이블 생성  (0) 2009.11.17
iBatis에서 테이블 생성하기 - 당근 퍼왔죠!!  (0) 2009.11.17
자바 정규 표현식  (0) 2009.11.13