ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • [SPRING]09SEP21
    SPRING 2021. 9. 9. 13:53

    파일업로드하기

     

     

     

    <%@ 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>
    		<h1>글 등록</h1>
    		<a href="logout.do">Log-out</a>
    		<hr>
    		<form action="insertBoard.do" method="post">
    			<table border="1" cellpadding="0" cellspacing="0">
    				<tr>
    					<td bgcolor="orange" width="70">제목</td>
    					<td align="left"><input type="text" name="title" /></td>
    				</tr>
    				<tr>
    					<td bgcolor="orange">작성자</td>
    					<td align="left"><input type="text" name="writer" size="10" /></td>
    				</tr>
    				<tr>
    					<td bgcolor="orange">내용</td>
    					<td align="left"><textarea name="content" cols="40" rows="10"></textarea></td>
    				</tr>
    				<tr>
    					<td bgcolor="orange">업로드</td>
    					<td><input type="file" name="uploadFile"></td>
    				</tr>
    				<tr>
    					<td colspan="2" align="center"><input type="submit"
    						value=" 새글 등록 " /></td>
    				</tr>
    			</table>
    		</form>
    		<hr>
    		<a href="getBoardList.do">글 목록 가기</a>
    </body>
    </html>

    jsp에 업로드 부분 추가하기

     

     

    private int seq;
    	private String title;
    	private String writer;
    	private String content;
    	private Date regdate;
    	private int cnt;
    	private String searchCondition;
    	private String searchKeyword;
    	private MultipartFile uploadFile;

    MultipartFile 추가하기

     

     

     

    pom.xml에 라이브러리 추가하기

    <dependency>
      <groupId>commons-fileupload</groupId>
      <artifactId>commons-fileupload</artifactId>
      <version>1.3.1</version>
    </dependency>

     

     

     

     

    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans"
    	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    	xmlns:context="http://www.springframework.org/schema/context"
    	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
    		http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd">
    
    	<context:component-scan base-package="com.springbook.view"></context:component-scan>
    	
    	<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
    	
    	</bean>
    </beans>

     

    스프링 컨트롤러가 파일을 받을 수 있도록 해주는 multipartResolver 빈 객체를 스프링에 등록한다.

    MultipartResolver는 Multipart형식으로 데이터가 전송된 경우, 해당 데이터를 스프링 MVC에서 사용할 수 있도록 변환해준다. (DispatcherSerlvet 은 이름이 "multipartResolver"인 빈을 사용하기 때문에 다른 이름을 지정하면 안된다)

    MultipartResolver는 Multipart 객체를 컨트롤러에 전달하는 역할을 한다.

     

     

     

    @Controller
    @SessionAttributes("board")
    public class BoardController {
    	@Autowired
    	private BoardService boardService; 
    	
    	@RequestMapping("/insertBoard.do")
    	public String insertBoard(BoardVO vo) throws IllegalStateException,IOException{
    		System.out.println("run insertBoard");
    		MultipartFile uploadFile = vo.getUploadFile();
    		
    		if(!uploadFile.isEmpty()) {
    			String fileName = uploadFile.getOriginalFilename();
    			uploadFile.transferTo(new File("D:/"+fileName));
    		}
    		boardService.insertBoard(vo);
    		
    		return "redirect:getBoardList.do";
    	}
        
        ...
    }

    insert된 파일은 BoardController에 적어둔 경로에 업로드된다.

    728x90
Designed by Tistory.