Dev/Spring

Spring[MultipartResolver]

OK-가자 2021. 10. 16. 22:48

Spring[MultipartResolver]

1. 설정

spring-servlet.xml 에 CommonMultipartResolver 설정을 해준다.

<!-- 멀티파트 리졸버 -->
<bean id="multipartResolver" 
       class="org.springframework.web.multipart.commons.CommonsMultipartResolver">

      <!-- 최대업로드 가능한 바이트크기 -->
      <property name="maxUploadSize" value="52428800" />

      <!-- 디스크에 임시 파일을 생성하기 전에 메모리에 보관할수있는 최대 바이트 크기 -->
     <!-- property name="maxInMemorySize" value="52428800" /-->

     <!-- defaultEncoding -->
     <property name="defaultEncoding" value="utf-8" />

</bean>

pom.xml 에 common-fileupload, common-io 라이브러리 dependency를 추가

<!-- common fileupload -->
    <dependency>
        <groupId>commons-fileupload</groupId>
        <artifactId>commons-fileupload</artifactId>
        <version>1.2.1</version>
    </dependency>

    <dependency>
        <groupId>commons-io</groupId>
        <artifactId>commons-io</artifactId>
        <version>1.4</version>
    </dependency>

2. [실습] 업로드 폼 구성 (views/fileupload/form.jsp)

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
<link href="${pageContext.request.contextPath }/assets/css/guestbook.css" rel="stylesheet" type="text/css">
</head>
<body>
    <div id="container">
        <div id="header">
            <c:import url="/WEB-INF/views/include/header.jsp"/>
        </div>
        <div id="content">
            <div style="margin:50px auto; width:500px; ">
                <h1 style="margin-bottom:20px">파일 업로드 예제</h1>
                <form method="post" action="" enctype="multipart/form-data">
                    <label>email:</label>
                    <input type="text" name="email" value="kickscar@gmail.com">
                    <br><br>

                    <label>파일1:</label>
                    <input type="file" name="file1">
                    <br><br>

                    <label>파일2:</label>
                    <input type="file" name="file2">
                    <br><br>

                    <br>
                    <input type="submit" value="upload">
                </form>
            </div>                
        </div>
        <div id="navigation">
            <c:import url="/WEB-INF/views/include/navigation.jsp"/>
        </div>
        <div id="footer">
            <p>(c)opyright 2014 </p>
        </div>
    </div>
</body>
</html>

3. [실습] 업로드 결과 페이지 (views/fileupload/result.jsp)

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
<link href="${pageContext.request.contextPath }/assets/css/guestbook.css" rel="stylesheet" type="text/css">
</head>
<body>
    <div id="container">
        <div id="header">
            <c:import url="/WEB-INF/views/include/header.jsp"/>
        </div>
        <div id="content">
            <div style="margin:50px auto; width:500px;">
                <h1 style="margin-bottom:20px">Upload completed</h1>
                <div class="result-images">
                    <img src="${pageContext.request.contextPath }/${urlImage }" style="width:150px"><br>
                </div>
                <p>
                    <a href='${pageContext.request.contextPath }/fileupload/form'> 다시 업로드 하기 </a>
                </p>
            </div>
        </div>
        <div id="navigation">
            <c:import url="/WEB-INF/views/include/navigation.jsp"/>
        </div>
        <div id="footer">
            <p>(c)opyright 2014 </p>
        </div>
    </div>    
</body>
</html>

4. FileUploadController 작성

@Controller
@RequestMapping( "/fileupload" )
public class FileUploadController {

    @Autowired
    private FileUploadService fileUploadService;

    @RequestMapping( "/form" )
    public String form(){
        return "fileupload/form";
    }

    @RequestMapping( "/upload" )
    public String upload( @RequestParam String email, @RequestParam( "file1" ) MultipartFile file1, Model model ){
        String saveFileName = fileUploadService.restore( file1 );

                String url = "/upload-images/" + saveFileName;
               model.addAttribute( "urlImage", url );

        return "fileupload/result";
    }    
}

5. FileUploadService 구현하기

@Service
public class FileUploadService {

    public String restore( MultipartFile file ) {


        return null;
    }
}

6. url 과 resource 매핑 하기

<!-- the mvc resources tag does the magic -->
<mvc:resources mapping="/upload-images/**" location="file:/upload/" />

spring-servlet.xml 에 설정한다.
요청 URL의 리소스 위치를 실제 물리적인 위치와 매핑한다.

'Dev > Spring' 카테고리의 다른 글

Spring[@Valid]  (0) 2021.10.17
Spring[Logging]  (0) 2021.10.17
Spring[Interceptor-Annotation활용]  (0) 2021.10.16
Spring [DefaultServlet-ViewResolver-Exception]  (0) 2021.10.16
Spring @MVC, Data Access, MyBatis  (0) 2021.10.15