[Spring] 파일 업로드

 

Controller

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
public class uploadController {
 
    //첨부파일 저장할 디렉토리
    @Resource(name = "upload_path")
    String upload_path;
    
    
    @RequestMapping("/upload/input.do")
    public String input() {
        return "upload/input"//views /upload/input.jsp
    }
    
        @RequestMapping("upload/upload.do")
        public ModelAndView uploadForm(MultipartFile file,ModelAndView mav) throws Exception{  //file << form 네임값들어옴
            //첨부파일이름
            String savedName = file.getOriginalFilename();
           // uuid 를 추가한 파일이름
            savedName = uploadFile(savedName,file.getBytes());
            //jsp 페이지 이름
            mav.setViewName("upload/upload_result");
            // jsp 페이지 저장 변수 저장
            mav.addObject("savedName",savedName);
            
            return mav;
        
        }
 
        private String uploadFile(String originalName, byte[] fileData) throws Exception {
            //uuid 고우변호
            UUID uid = UUID.randomUUID(); // 랜덤생성
            String savedName = uid.toString()+"_"+originalName; // 고유랜덤번호 지정
            
            File target = new File(upload_path,savedName); //������ ���
            //파일복사
            FileCopyUtils.copy(fileData, target); //저장
            
            return savedName;
        }  
}
cs

views/upload/input

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
width: 400px;
height: 200px;
border: 1px;
border-style: solid;
}
</style>
</head>
<body>
<%@ include file="../include/menu.jsp" %>
<form action="/spring05/upload/upload.do" id="form1" method="post" enctype="multipart/form-data" target="iframe1">
<input type="file" name="file">
<input type="submit" value="업로드"> 
</form>
<iframe name="iframe1"></iframe>
</body>
</html>
cs

view/upload/upload_result

1
2
3
4
5
6
7
8
9
10
11
12
13
<%@ page language="java" contentType="text/html; charset=EUC-KR"
    pageEncoding="EUC-KR"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="EUC-KR">
<title>Insert title here</title>
</head>
<body>
파일이 업로드 되었습니다.<br>
파일명 : ${saved_name}
</body>
</html>
cs

pom.xml

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
            <dependency>
    <groupId>com.oracle.database.jdbc</groupId>
    <artifactId>ojdbc8</artifactId>
    <version>21.5.0.0</version>
</dependency>
<dependency>
    <groupId>commons-dbcp</groupId>
    <artifactId>commons-dbcp</artifactId>
    <version>1.4</version>
</dependency>
 <dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-jdbc</artifactId>
    <version>5.3.20</version>
</dependency>
<dependency>
    <groupId>org.mybatis</groupId>
    <artifactId>mybatis-spring</artifactId>
    <version>2.0.7</version>
</dependency>
<dependency>
    <groupId>org.mybatis</groupId>
    <artifactId>mybatis</artifactId>
    <version>3.5.7</version>
</dependency>
<dependency>
    <groupId>commons-fileupload</groupId>
    <artifactId>commons-fileupload</artifactId>
    <version>1.4</version>
</dependency>
<!-- https://mvnrepository.com/artifact/javax.annotation/javax.annotation-api -->
<dependency>
    <groupId>javax.annotation</groupId>
    <artifactId>javax.annotation-api</artifactId>
    <version>1.3.2</version>
</dependency>
cs

servlet-context.xlm

1
2
3
4
5
6
<beans:bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
    <beans:property name="maxUploadSize" value="10485760" />
    </beans:bean>
    <beans:bean id="upload_path" class="java.lang.String">
    <beans:constructor-arg value="C:\Users\cas90\Desktop" />
    </beans:bean>
cs