- /WebContent/WEB-INF/web.xml 소스
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:jsp="http://java.sun.com/xml/ns/javaee/jsp" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
<servlet>
<servlet-name>springboard</servlet-name>
<servlet-class>
org.springframework.web.servlet.DispatcherServlet
</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>springboard</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
<!-- 한글 처리 -->
<filter>
<filter-name>encodingFilter</filter-name>
<filter-class>
org.springframework.web.filter.CharacterEncodingFilter
</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>EUC-KR</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>encodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>
- /WebContent/WEB-INF/springboard-servlet.xml 소스
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
<!-- 핸들러 맵핑 작성 -->
<bean id = "handelermapping"
class = "org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
<property name="mappings">
<value>
<!-- 밑에있는 컨트롤러와 이름이 일치해야 함 -->
/FileSubmitController.do=fileSubmitController
</value>
</property>
</bean>
<!-- Controller 작성 -->
<bean id="fileSubmitController" class="controller.FileSubmitController"/>
<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<property name="defaultEncoding" value="euc-kr"/>
<property name="maxUploadSize" value="52428800"/>
<property name="uploadTempDir" value="temp"/>
</bean>
<!-- Controller 실행 후 결과를 result할 jsp 페이지를 설정 -->
<bean id = "internalResolver"
class = "org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name = "viewClass">
<value>org.springframework.web.servlet.view.JstlView</value>
</property>
<property name="prefix">
<value>/jsp/</value>
</property>
<property name="suffix">
<value>.jsp</value>
</property>
</bean>
<!-- 데이터 베이스 객체를 사용할 수 있게 설정해주는 Datasource 선언 -->
<bean id = "dataSource"
class = "org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name = "driverClassName">
<value>oracle.jdbc.driver.OracleDriver</value>
</property>
<property name="url">
<value>jdbc:oracle:thin:@127.0.0.1:1521:XE</value>
</property>
<property name="username">
<value>system</value>
</property>
<property name="password">
<value>123456</value>
</property>
</bean>
</beans>
- /WebContent/jsp/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> 파일 업로드 </title>
</head>
<body>
<center>
<font face="휴먼아미체">
<h2> 파일 업로드 </h2>
<form action = "/FileUpload/FileSubmitController.do" method="post" enctype="multipart/form-data">
<table border = "0" cellpadding = "5" cellspacing = "5">
<tr>
<td bgcolor = "lightyellow"> 파일명 </td>
<td> <input type = "text" name = "filename" align = "left"> </td>
</tr>
<tr>
<td bgcolor = "lightyellow"> 파일 </td>
<td> <input type = "file" name = "file"> </td>
</tr>
</table>
<table border = "0" cellpadding = "5">
<tr>
<td colspan = "4" align = "center">
<input type = "submit" value = "전송">
<input type = "reset" value = "취소">
</tr>
</table>
</form>
</font>
</center>
</body>
</html>
- /src/controller/FileSubmitController.java 소스
package controller;
import java.io.File;
import java.io.IOException;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;
public class FileSubmitController {
@RequestMapping("/FileSubmitController")
public ModelAndView getFile(@RequestParam("filename") String filename,
@RequestParam("file") MultipartFile file) throws IllegalStateException, IOException{
// 파일의 이름
String originalfilename = file.getOriginalFilename();
// 파일의 사이즈
long getSize = file.getSize();
// 업로드한 파일을 원하는 경로에 저장
String path = "C:/java/workspace3/FileUpload/temp/";
file.transferTo(new File(path+originalfilename));
System.out.println(originalfilename + " : " + getSize);
return new ModelAndView("FileResult");
}
}
<%@ 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> 파일 업로드 </title>
</head>
<body>
<center>
<font face="휴먼아미체">
완료
</font>
</center>
</body>
</html>
- 결과