day4 class05 파일 업로드 및 예외 처리
1. 파일업로드
1-1. 파일 업로드 입력화면 추가
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 | <form action="insertBoard.do" method="post" enctype="multipart/form-data"> <table border="1" cellpadding="0" cellspacing="0"> <tr> <td bgcolor="orange" width="70">제목</td> <td align="left"> <input name="title" type="text" /> </td> </tr> <tr> <td bgcolor="orange">작성자</td> <td align="left"> <input name="writer" type="text" 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" width="70">업로드</td> <td align="left"> <input type="file" name="uploadFile"></input> </td> </tr> <tr> <td colspan="2" align="center"> <input type="submit" value="새글 등록" /> </td> </tr> </table> </form> | cs |
1-2. command 객체 수정
- BoardVO 객체에 uploadFile 변수와 getter/setter 메소드를 추가한다.
1-3. FileUpload 라이브러리 추가
- apache에서 제공하는 Common FileUpload 라이브러리를 pom.xml에 추가해준다.
- Maven Dependencies 폴더에서 commons-fileupload-1.3.1.jar / commons-io-2.2.jar을 확인한다.
1 2 3 4 5 6 | <!-- FileUpload --> <dependency> <groupId>commons-fileupload</groupId> <artifactId>commons-fileupload</artifactId> <version>1.3.1</version> </dependency> | cs |
1-4. MultipartResolver 설정
- 지금까지 클래스를 스프링 설정파일에 <bean>등록 할 때 아이디 값을 임의로 정했지만, CommonsMultipartResolver 클래스는 아이디 값을 multipartResolver을 사용해야 한다.
- 추가적으로 Resolver로 끝나는 클래스들은 대부분 아이디가 정해져 있다.
- maxUploadSize는 지정하지 않으면 기본으로 -1로 설정되며 이는 무제한을 뜻한다.
- MultipartFile 인터페이스가 제공하는 주요 메소드
메소드 |
설명 |
String getOriginalFilename() |
업로드한 파일명을 문자열로 리턴 |
void transferTo(File destFile) |
업로드한 파일을 destFile에 저장 |
boolean isEmpty() |
업로드한 파일 존재 여부 리턴(없으면 true 리턴) |
1 2 3 4 | <!-- 파일 업로드 설정 --> <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver"> <property name="maxUploadSize" value="100000"></property> </bean> | cs |
1-5. 파일 업로드 처리
- BoardController의 insertBoard() 메소드에 파일 업로드 코드를 입력한다.
- 폴더 경로 구분은 '/'로 한다.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | // 글 등록 @RequestMapping(value="/insertBoard.do") public String insertBoard(BoardVO vo) throws IOException { // System.out.println("글 등록 처리"); // 파일 업로드 처리 MultipartFile uploadFile = vo.getUploadFile(); if(!uploadFile.isEmpty()) { String fileName = uploadFile.getOriginalFilename(); uploadFile.transferTo(new File("C:/Users/lkh/Documents/" + fileName)); } boardService.insertBoard(vo); return "getBoardList.do"; } | cs |
2. 예외 처리
2-1. 예외 처리 경우 생성
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | // LoginController.java @RequestMapping(value="/login.do", method=RequestMethod.POST) public String login(UserVO vo, UserDAO userDAO, HttpSession session) { System.out.println("로그인 처리..."); if(vo.getId() == null || vo.getId().equals("")) { throw new IllegalArgumentException("아이디는 반드시 입력해야 합니다."); } UserVO user = userDAO.getUser(vo); if(userDAO.getUser(vo) != null) { session.setAttribute("userName", user.getName()); return "redirect:getBoardList.do"; } else { return "login.jsp"; } } | cs |
2-2. 예외 화면 생성
- src/main/webapp/common에 생성한다.
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 | <!-- error.jsp --> <%@ page contentType="text/html; charset=EUC-KR" %> <%@ page isErrorPage="true" %> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <title>에러 화면</title> </head> <body bgcolor="#ffffff" text="#000000"> <!-- 타이틀 시작 --> <table width="100%" border="1" cellspacing="0" cellpadding="0"> <tr> <td align="center" bgcolor="orange"><b>기본 에러 화면입니다.</b></td> </tr> </table> <br> <!-- 에러 메시지 --> <table width="100%" border="1" cellspacing="0" cellpadding="0" align="center"> <tr> <td align="center"><br><br><br><br><br>Message : ${exception.message }<br><br><br><br><br></td> </tr> </table> </body> </html> | cs |
2-3. XML 기반 예외 처리
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | <!-- presentation-layer.xml --> <!-- 예외 처리 설정 --> <bean id="exceptionResolver" class="org.springframework.web.servlet.handler.SimpleMappingExceptionResolver"> <property name="exceptionMappings"> <props> <prop key="java.lang.ArithmeticException"> common/arithmeticError.jsp </prop> <prop key="java.lang.NullPointerException"> common/nullPointerError.jsp </prop> </props> </property> <property name="defaultErrorView" value="common/error.jsp" /> </bean> | cs |