Spring MVC 에서 Static file 존재 여부 체크
Accessing files under WEB-INF (Servlets forum at JavaRanch) 의 답변중에 아래 내용으로 시작하는 부분을 보면,
You can, but you shouldn't. It's a violation of the J2EE spec, which says that a J2EE webapp is a single (WAR) file, and therefore filesystem access to components within the WAR is not guaranteed.
...
normal Java File API 로 File 에 대한 접근을 하는 것은 .war 파일안에 있는 resource 에 대한 접근을 guarantee 할 수 없다고 한다.
그러므로 File class 를 통해서 static file 이 존재하는 지 여부를 check 하는 것은 좋은 방법이 아니다. 그래서 위의 글에서도 얘기하듯이 ServletContext 의 method 를 통해서 접근해야 한다.
@Autowired
ServletContext servletContext;
private String getRedirectPage(HttpSession session) {
String redirectPage = SHOWED_FIRST_PAGE;
if(session.getAttribute(SessionAttr.ID_ADMIN_YN).equals(SessionAttr.VALUE_ADMIN_Y)) {
try {
String filename = "/WEB-INF" + ADMIN_SHOWED_FIRST_PAGE;
URL url = servletContext.getResource(filename);
if(url != null) {
// file exists
redirectPage = ADMIN_SHOWED_FIRST_PAGE;
}
} catch (MalformedURLException e) {
e.printStackTrace();
}
}
return redirectPage;
}
Request 를 이용해서 servletContext 를 가져올 수도 있다.
request.getServletContext().getResourceAsStream("/WEB-INF/config/api.properties")
댓글 없음:
댓글 쓰기