2012-01-25 24 views
3

파일 크기를 초과하면 파일 업로드시 사용자 정의 오류 메시지를 가로 채고 보낼 수 있습니까? 컨트롤러 클래스에 주석 처리 된 예외 처리기가 있지만 요청이 컨트롤러에 전달되지 않았습니다. 이 링크에서 나는 대답 How to handle MaxUploadSizeExceededException에 대한 답은 HandlerExceptionResolver 구현을 제안합니다.Spring MVC로 MaxUploadSizeExceededException 처리

스프링 3.5에서 변경되었거나 여전히 유일한 해결책입니까?

@Component public class ExceptionResolverImpl implements HandlerExceptionResolver { 
private static final Logger LOG = LoggerFactory.getLogger(ExceptionResolverImpl.class); 

@Override 
public ModelAndView resolveException(HttpServletRequest request, 
     HttpServletResponse response, Object obj, Exception exc) { 

    if(exc instanceof MaxUploadSizeExceededException) { 
     response.setContentType("text/html"); 
     response.setStatus(HttpStatus.REQUEST_ENTITY_TOO_LARGE.value()); 

     try { 
      PrintWriter out = response.getWriter(); 

      Long maxSizeInBytes = ((MaxUploadSizeExceededException) exc).getMaxUploadSize(); 

      String message = "Maximum upload size of " + maxSizeInBytes + " Bytes per attachment exceeded"; 
      //send json response 
      JSONObject json = new JSONObject(); 

      json.put(REConstants.JSON_KEY_MESSAGE, message); 
      json.put(REConstants.JSON_KEY_SUCCESS, false); 

      String body = json.toString(); 

      out.println("<html><body><textarea>" + body + "</textarea></body></html>"); 

      return new ModelAndView(); 
     } 
     catch (IOException e) { 
      LOG.error("Error writing to output stream", e); 
     } 
    } 

    //for default behaviour 
    return null; 
} 

}

:

답변

4

나는 구현 HandlerExceptionResolver을 종료
관련 문제