ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • 05. 예외처리, File Upload [Spring]
    Spring 2016. 11. 4. 11:13
    반응형

    Bean에 등록하는것은 filter를 제외한 모든것을 Bean에 등록을 해준다.


    01. Exception


    com.ktds - common - exception.handler에

    CommonExceptionHandler.java 를 생성 (예외 처리를 위한 class)


    applicationContext.xml에 

    <!-- Exception Handler -->

    <bean class="com.ktds.common.exception.handler.ApplicationExceptionHandler" />



    @ControllerAdvice("최초패키지명(ex)com.ktds)")

    public class ApplicationExceptionHandler {


    @ExceptionHandler(RuntimeException.class)

    public ModelAndView viewRuntimeExceptionPage(RuntimeException e) {

    ModelAndView view = new ModelAndView();

    view.setViewName("error/runtime");

    if( e instanceof NullPointerException ) {

    // 현재 넘어오는 Exception이 NullPointerException 인지 물어보는것.

    view.addObject("message", "필수 입력값을 입력하지 않았습니다!");

    }

    else if ( e instanceof DataAccessException ) {

    view.addObject("message", "입력값에 문제가 있습니다.");

    }

    else if ( e instanceof NumberFormatException) {

    view.addObject("message", "숫자 입력 값에 문제가 있습니다!");

    }

    else {

    view.addObject("message", e.getMessage());

    }

    return view;

    }

    }




    02. File Upload


    applicationContext.xml에서

    <!-- FileUpload 설정 -->

    <bean id="multipartResolver"

    class="org.springframework.web.multipart.commons.CommonsMultipartResolver">

    <property name="maxUploadSize" value="104857600"><!-- 100MB --></property>

    <property name="defaultEncoding" value="UTF-8"></property>

    </bean>




    fileUpload.jsp 생성


    <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>


    <form  method="post" enctype="multipart/form-data" 

    action="<c:url value="/doFileUpload"/>">

    <input type="text" name="name" />

    <input type="file" name="attachedFile" />

    </form>



    IndexContoller.java


    @RequestMapping("/fileUpload")

    public String viewFileUploadPage() {

    return "fileUpload";

    }

    @RequestMapping("/doFileUpload")

    public ModelAndView doFileUpladAction( MultipartHttpServletRequest request ) {

    UploadVO uploadVO


      UploadVO 에는 name과 attachedFile가 있다


      <!-- 둘다 사용 가능함 -->

    MultipartFile file = request.getFile("attachedFile");

    MultipartFile file = uploadVO.getAttachedFile();


      <!-- 둘다 사용 가능함 -->

      String name = uploadVO.getName();

      String name = request.getParameter("name");

    logger.info(name + "이 업로드 요청 함");

    if( !file.isEmpty() ) {

    File uploadFile = new File("D:\\uploadFiles\\" + file.getOriginalFilename());

    logger.info(uploadFile.getAbsolutePath() + "에 업로드 함.");

    try {

    file.transferTo(uploadFile);

    } catch (IllegalStateException e) {

    throw new RuntimeException(e.getMessage(), e);

    } catch (IOException e) {

    throw new RuntimeException(e.getMessage(), e);

    }

    }

    ModelAndView view = new ModelAndView();

    view.setViewName("redirect:/fileUpload");

    return view;

    }

    반응형

    댓글

Designed by Tistory.