티스토리 뷰
파일업로드
pom.xml 의 Dependencies에 file upload dependency를 추가한다.
파일 업로드를 할 때 form에서 enctype을 "multipart/form-data"로 정의해야한다.
multipart 기능을 사용하기 위해서 applicationContext.xml 에 MutipartResolver를 스프링 설정으로 등록시켜 준다.
|
1
2
3
4
5 |
<bean id="multipartResolver"
class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<property name="maxUploadSize" value="104857600" />
<property name="defaultEncoding" value="UTF-8" />
</bean> |
cs |
MultipartResolver는 enctype이 multipart/form-data 형식으로 데이터가 전송되었을 때 해당 데이터를 Spring MVC에서 사용할 수 있도록 변환해준다.
Controller에 File Upload 추가
|
1
2
3
4
5
6
7
8
9
10 |
MultipartFile uploadFile = loginVO.getUploadFile();
if ( !uploadFile.isEmpty() ) {
String randomFileName = UUID.randomUUID().toString();
File tempFile = new File("D:\\" + randomFileName );
try {
uploadFile.transferTo(tempFile);
} catch (IllegalStateException | IOException e) {
throw new RuntimeException(e.getMessage(), e);
}
} |
cs |
이때 HttpServlerRequest와 동일하게 Multipartfile도 Command 객체로 받아올 수 있다.
멤버변수 타입이 MultipartFile로 선언되어야 한다.
|
1 |
private MultipartFile uploadFile; |
cs |
'BackEnd > Spring' 카테고리의 다른 글
| [Spring] Mapper XML Files ( select / insert, update, delete / parameters ) (0) | 2016.04.20 |
|---|---|
| [Spring] ORM : MyBatis 연동 (0) | 2016.04.20 |
| [Spring] Controller의 리턴 타입 (0) | 2016.04.15 |
| [Spring] 예외처리 (1) | 2016.04.14 |
| [Spring] Command 객체 값 검증, Error 메세지 (0) | 2016.04.14 |
댓글
공지사항
최근에 올라온 글
최근에 달린 댓글
- Total
- Today
- Yesterday
TAG
- INSERT
- DFS
- sort
- table
- mybatis
- 예외처리
- BFS
- java
- 이클립스
- order by
- algorithm
- 자바
- AlertDialog.Builder
- indexOf
- controller
- onBackPressed
- list
- RequestMapping
- Baekjoon Online Judege
- REDIRECT
- jsp
- 안드로이드 비콘
- onPostExecute
- Spring
- DP
- 안드로이드 스튜디오
- maven
- servlet
- restfb
- boj
| 일 | 월 | 화 | 수 | 목 | 금 | 토 |
|---|---|---|---|---|---|---|
| 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 |
글 보관함