일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |
- Log4j
- 메모
- iBATIS
- Modeling
- Oracle
- java se
- 안드로이드
- 오라클
- 오류
- 관심책
- Android
- eclipse plugin
- TDD
- annotation
- 우분투
- ERD
- junit
- TAG 오류
- 초대장
- derby
- java
- 실수
- eclipse
- action
- Spring Framework
- 디자인 오류
- struts2
- Spring
- DATABASE
- 초대장 배포
- Today
- 12
- Total
- 579,107
거꾸로 토마토
SpringMVC에서 MultipartFile 업로드 본문
SpringMVC에서 MultipartFile을 이용한 파일업로드 방법입니다.
1. 일단 jsp에서 <form> 태그 설정입니다.
... <form:form method="POST" commandName="simpleUploadForm" enctype="multipart/form-data"> <input type="file" name="file_nm1" /> ... |
enctype="multipart/form-data"으로 작성해야 multipart 객체로 받을 수 있습니다.
2. Context 환경 설정 내용입니다.
... <bean id="multipartResolver" ... |
spring에서 사용할 수 있는 기본 MultipartResolver를 선언합니다.
3. Controller 부분 내용입니다.
import org.springframework.web.multipart.commons.CommonsMultipartFile
... @RequestMapping(value="/business/nondisRegisterPro.do") public String nondisRegisterPro(MultipartHttpServletRequest request, ModelMap model) throws Exception { Map<String, MultipartFile> files = request.getFileMap(); CommonsMultipartFile cmf = (CommonsMultipartFile) files.get("file_nm1"); String path ="c:/test/"+cmf.getOriginalFilename(); File f = new File(path); // 파일 업로드 처리 완료. cmf.transferTo(f); HashMap map = new HashMap(); map.put("edu_inddis_no", request.getParameter("edu_inddis_no"));
try { minwonService.nondisInsert(map); model.addAttribute("resMessage", "성공적으로 신청되었습니다."); } catch (Exception e) { model.addAttribute("resMessage", "등록중 오류가 발생하였습니다"); }
return "www/business/nondisRegister"; } |
jsp의 file 객체명 'file_nm1'으로 CommonsMultipartFile 객체를 받고 transferTo(...) method를 이용해서 원하는 path로 파일을 떨어뜨리면 됩니다. transferTo method가 참 유용합니다. 없었다면 stream 객체를 이용해서 구현해야 했는데 말이죠.
'Programming > Spring Framework' 카테고리의 다른 글
SpringMVC에서 MultipartFile 업로드 (2) | 2012.12.18 |
---|---|
Struts 2. Action 객체에서 request, response 객체 사용하기 (0) | 2009.10.28 |
struts 2. action 설정에서 result tag 사용 (0) | 2009.10.28 |
Spring에서 log4j를 이용해서 로깅하기 위한 설정 (0) | 2009.10.21 |