1. dispatcher-servlet.xml
<bean id="multipartResolver"
class="kame.spring.chap04.controller.MultiCommonsMultipartResolver" >
<property name="maxUploadSize" value="15000000" />
<property name="maxInMemorySize" value="15000000"></property>
</bean>
<bean id="handlerMapping1"
class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
<property name="order" value="1" />
<property name="alwaysUseFullPath" value="true" />
<property name="mappings">
<props>
<prop key="/submitReport.do">
submitReportController
</prop>
<prop key="/multiSubmitReportForm.do">
multiSubmitFormController
</prop>
<prop key="/multiSubmitReport.do">
multiSubmitReportController
</prop>
</props>
</property>
</bean>
<bean id="submitReportController" class="kame.spring.chap04.controller.MultiPartFileUploadController">
<property name="commandClass" value="kame.spring.chap04.controller.MultiPartFileUploadBean" />
<property name="formView" value="form" />
<property name="successView" value="success" />
</bean>
<bean id="multiSubmitFormController"
class="kame.spring.chap04.controller.MultiSubmitFormController" />
<bean id="multiSubmitReportController" class="kame.spring.chap04.controller.GenericFileUploadController" />
2. form으로 이동시켜주는 controller
kame.spring.chap04.controller.MultiSubmitFormController
package kame.spring.chap04.controller;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.Controller;
public class MultiSubmitFormController implements Controller {
@Override
public ModelAndView handleRequest(HttpServletRequest request,
HttpServletResponse response) throws Exception {
ModelAndView mav = new ModelAndView();
mav.setViewName("multiSubmitReportForm");
return mav;
}
}
3. multiSubmitReportForm.jsp 파일들을 입력할 폼
<%@ page language="java" contentType="text/html; charset=EUC-KR"
pageEncoding="EUC-KR"%>
<%@ taglib prefix="spring" uri="http://www.springframework.org/tags" %>
<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form" %>
<!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>
<spring:hasBindErrors name="report" />
<form action="multiSubmitReport.do" method="post" enctype="multipart/form-data">
<p>
<label for="subject">제목</label>
<input type="text" id="subject" name="subject" value="${report.subject }"/><form:errors path="report.subject" />
</p>
<p>
<label for="reportFile1">리포트 파일1</label>
<input type="file" id="uploadMultiFile" name="uploadMultiFile"/><form:errors path="report.reportFiles"/>
</p>
<p>
<label for="reportFile2">리포트 파일2</label>
<input type="file" id="uploadMultiFile" name="uploadMultiFile"/><form:errors path="report.reportFiles"/>
</p>
<p>
<input type="submit" value="리포트 전송" />
</p>
</form>
</body>
</html>
4. form jsp 페이지에서 submit 후 이동하는 kame.spring.chap04.controller.GenericFileUploadController
로직은 이곳에서 이루어진다.
package kame.spring.chap04.controller;
import java.io.File;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.util.Assert;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.Controller;
public class GenericFileUploadController implements Controller {
@Override
@SuppressWarnings("unchecked")
public ModelAndView handleRequest(final HttpServletRequest request, final HttpServletResponse response)
throws Exception {
/*
* validate request type
*/
Assert.state(request instanceof MultipartHttpServletRequest, "request !instanceof MultipartHttpServletRequest");
final MultipartHttpServletRequest multiRequest = (MultipartHttpServletRequest) request;
/*
* validate text input
*/
//Assert.state(request.getParameter("type").equals("genericFileMulti"), "type != genericFileMulti");
System.out.println(request.getParameter("subject"));
/*
* extract files
*/
final Map<String, MultipartFile> files = multiRequest.getFileMap();
Assert.state(files.size() > 0, "0 files exist");
/*
* process files
*/
for (MultipartFile file : files.values()) {
file.transferTo(new File(new File("c:\\"), file.getOriginalFilename()));
}
return new ModelAndView("success");
}
}
5. CommonsMultipartResolver 바꾸자 kame.spring.chap04.controller.MultiCommonsMultipartResolver
package kame.spring.chap04.controller;
import java.io.UnsupportedEncodingException;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import javax.servlet.ServletContext;
import org.apache.commons.fileupload.FileItem;
import org.springframework.util.StringUtils;
import org.springframework.web.multipart.MultipartException;
import org.springframework.web.multipart.commons.CommonsMultipartFile;
import org.springframework.web.multipart.commons.CommonsMultipartResolver;
public class MultiCommonsMultipartResolver extends CommonsMultipartResolver {
public MultiCommonsMultipartResolver() {
}
public MultiCommonsMultipartResolver(ServletContext servletContext) {
super(servletContext);
}
@Override
@SuppressWarnings("unchecked")
protected MultipartParsingResult parseFileItems(List fileItems, String encoding) {
Map multipartFiles = new HashMap();
Map multipartParameters = new HashMap();
// Extract multipart files and multipart parameters.
for (Iterator it = fileItems.iterator(); it.hasNext();) {
FileItem fileItem = (FileItem) it.next();
if (fileItem.isFormField()) {
String value = null;
if (encoding != null) {
try {
value = fileItem.getString(encoding);
} catch (UnsupportedEncodingException ex) {
if (logger.isWarnEnabled()) {
logger.warn("Could not decode multipart item '" + fileItem.getFieldName()
+ "' with encoding '" + encoding + "': using platform default");
}
value = fileItem.getString();
}
} else {
value = fileItem.getString();
}
String[] curParam = (String[]) multipartParameters.get(fileItem.getFieldName());
if (curParam == null) {
// simple form field
multipartParameters.put(fileItem.getFieldName(), new String[] { value });
} else {
// array of simple form fields
String[] newParam = StringUtils.addStringToArray(curParam, value);
multipartParameters.put(fileItem.getFieldName(), newParam);
}
} else {
// multipart file field
CommonsMultipartFile file = new CommonsMultipartFile(fileItem);
if (multipartFiles.put(fileItem.getName(), file) != null) {
throw new MultipartException("Multiple files for field name [" + file.getName()
+ "] found - not supported by MultipartResolver");
}
if (logger.isDebugEnabled()) {
logger.debug("Found multipart file [" + file.getName() + "] of size " + file.getSize()
+ " bytes with original filename [" + file.getOriginalFilename() + "], stored "
+ file.getStorageDescription());
}
}
}
return new MultipartParsingResult(multipartFiles, multipartParameters);
}
}
6. success 페이지는 대충...
어디선가 보고 만들었네요.^^
'SPRING_SETTING > spring' 카테고리의 다른 글
spring form tag (0) | 2011.01.23 |
---|---|
핸들러 인터셉터 (0) | 2011.01.18 |
AOP의 개념 정리를 위한 TEST (0) | 2010.12.17 |
Spring MVC 03 - web.xml 설정 (0) | 2010.11.23 |
Spring MVC 01 (0) | 2010.11.22 |