1)파일 업로드 설정
1. pom.xml
(파일업로드, 이미지 썸네일 관련 라이브러리 추가)
1
2
3
4
5
6
7
8
9
10
11
12
13
|
<!-- 파일업로드/다운로드 -->
<dependency>
<groupId>commons-fileupload</groupId>
<artifactId>commons-fileupload</artifactId>
<version>1.3.2</version>
</dependency>
<!-- 파일관련 / 썸네일라이브러리 -->
<dependency>
<groupId>org.imgscalr</groupId>
<artifactId>imgscalr-lib</artifactId>
<version>4.2</version>
</dependency>
|
cs |
2. servlet-context.xml에 bean 과 파일업로드 경로 추가
1
2
3
4
5
6
7
8
9
10
11
12
|
<!-- 파일 업로드에 필요한 bean -->
<beans:bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<!-- 파일업로드 용량 -->
<beans:property name="maxUploadSize" value="10485760"/>
</beans:bean>
<!-- 업로드경로 -->
<beans:bean id="uploadPath" class="java.lang.String">
<!-- 파일업로드 디렉토리 -->
<beans:constructor-arg value="C:\Users\PoPo\Desktop\uploadtmp"/>
</beans:bean>
|
cs |
2)파일 업로드 구현
1. uploadController -파일 업로드 관련 컨트롤러 추가
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
32
33
34
35
36
37
38
39
40
|
@Controller
@Log4j
public class UploadController {
//bean 의 id가 uploadPath인 태그 참조 (xml에 설정된 리소스참조)
@Resource(name="uploadPath")
String uploadPath;
@GetMapping(value = "/upload/uploadForm")
public void uploadForm() {
//upload/uploadForm.jsp로 포워딩
}
//업로드 흐름: 업로드 버튼 클릭 -> 임시디렉토리 업로드 -> 지정된 디렉토리 저장 ->파일정보가 file에 저장
@PostMapping(value ="/upload/uploadForm")
public ModelAndView uploadFomr(MultipartFile file, ModelAndView mav) throws Exception{
log.info("파일이름" + file.getOriginalFilename());
log.info("파일크기" + file.getSize());
log.info("컨텐트타입" + file.getContentType());
//파일 이름을 savedName 변수에 저장
String savedName = file.getOriginalFilename();
//uploadPath경로의 savedName 파일에 대한 file 객체 생성
File target = new File(uploadPath, savedName);
//file의 내용을 target에 복사함
FileCopyUtils.copy(file.getBytes(), target);
//모델앤뷰의 뷰 경로지정
mav.setViewName("upload/uploadResult");
//속성추가
mav.addObject("savedName", savedName);
return mav;
}
}
|
cs |
2. View(파일업로드 페이지, 업로드 결과 페이지)
form 태그 속성에 enctype="multipart/form-data"추가
- uploadForm.jsp (파일업로드 페이지)
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<form action="/upload/uploadForm" method="post" enctype="multipart/form-data">
<input type="file" name="file">
<input type="submit" value="업로드">
</form>
</body>
</html>
|
cs |
- uploadResult.jsp 파일 업로드 결과페이지
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
파일이 업로드 됨.
파일명 : ${savedName} //컨트롤러에서 savedName으로 데이터를 보냈기때문에 끄내서 사용함.
</body>
</html>
|
cs |
파일업로드가 처리가 잘되었다.
File 클래스 정리 - (java클래스) 기존의 파일이나,폴더에 대한 제어를 하는데 사용
ModelAndView 정리 - (spring클래스) 단일 반환값으로 모델과 뷰를 반환
FileCopyUtils 정리 - (spring클래스) 파일 및 스트림 복사를 위한 간단한 유틸리티 방법
docs.spring.io/spring-framework/docs/current/javadoc-api/org/springframework/util/FileCopyUtils.html