Spring MVC - fileupload
프로그래밍/Spring MVC 2009. 12. 24. 07:56Spring's multipart(fileupload) support
Spring 은 웹 애플리케이션에서 multipart 를 지원하기 위한 내장된 기능이 있다.
org.springframework.web.multipart.MultipartResolver 객체는 common fileupload 를 사용해서 multipart 를 처리할 수 있도록 설계되었다.
MultipartResolver 를 사용할 경우 모든 request 에 대해 multipart 여부를 체크하게 된다.
MultipartResolver 사용하기
<bean id="multipartResolver"
class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<!-- one of the properties available; the maximum file size in bytes -->
<property name="maxUploadSize" value="100000"/>
</bean>
<bean id="multipartResolver" class="org.springframework.web.multipart.cos.CosMultipartResolver">
<!-- one of the properties available; the maximum file size in bytes -->
<property name="maxUploadSize" value="100000"/>
</bean>
위 설정을 사용하기 위해서는 관련된 jar 파일이 필요하다. fileupload.jar 또는 cos.jar
폼에서 파일 업로드 다루기
<html>
<head>
<title>Upload a file please</title>
</head>
<body>
<h1>Please upload a file</h1>
<form method="post" action="upload.form" enctype="multipart/form-data">
<input type="file" name="file"/>
<input type="submit"/>
</form>
</body>
</html>
<beans>
<!-- lets use the Commons-based implementation of the MultipartResolver interface -->
<bean id="multipartResolver"
class="org.springframework.web.multipart.commons.CommonsMultipartResolver"/>
<bean id="urlMapping" class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
<property name="mappings">
<value>
/*/upload.form=fileUploadController
</value>
</property>
</bean>
<bean id="fileUploadController" class="examples.FileUploadController">
<property name="commandClass" value="examples.FileUploadBean"/>
<property name="formView" value="fileuploadform"/>
<property name="successView" value="confirmation"/>
</bean>
</beans>
public class FileUploadController extends SimpleFormController {
protected ModelAndView onSubmit(HttpServletRequest request, HttpServletResponse response,
Object command, BindException errors) throws ServletException, IOException {
// cast the bean
FileUploadBean bean = (FileUploadBean) command;
let's see if there's content there
byte[] file = bean.getFile();
if (file == null) {
// hmm, that's strange, the user did not upload anything
}
// well, let's do nothing with the bean for now and return
return super.onSubmit(request, response, command, errors);
}
protected void initBinder(HttpServletRequest request, ServletRequestDataBinder binder)
throws ServletException {
// to actually be able to convert Multipart instance to byte[]
// we have to register a custom editor
binder.registerCustomEditor(byte[].class, new ByteArrayMultipartFileEditor());
// now Spring knows how to handle multipart object and convert them
}
}
public class FileUploadBean {
private byte[] file;
public void setFile(byte[] file) {
this.file = file;
}
public byte[] getFile() {
return file;
}
}
FileUploadBean 객체는 FileUploadController 에 commandClass 로 설정된다.FileUploadBean 객체의 경우 file 을 담기위한 byte 배열 멤버를 가지고 있다. 업로드된 파일을 byte 배열로 담기위해
initBinder 메소드내에서 binder.registerCustomEditor( byte[].class, new ByteArrayMultipartFileEditor()) 를 설정하고 있다.
upload 된 파일을 String 으로 담기 위해서는 binder.registerCustomEditor(String.class, new StringMultipartFileEditor()) 로 설정하면 된다.
upload 된 파일을 MultipartFile 객체로 담기위해서는 추가적인 binder.registerCustomEditor 호출이 필요하지 않다.
'프로그래밍 > Spring MVC' 카테고리의 다른 글
| ContentNegotiatingViewResolver 이해하기 (0) | 2013.09.04 |
|---|---|
| Spring AOP를 사용하여 DB 트랜잭션시 주의점 (0) | 2013.08.19 |
| Spring MVC - Themes (0) | 2009.12.23 |
| Spring MVC - Locale (0) | 2009.12.23 |
| Spring MVC - Views (0) | 2009.12.23 |