量産メモ帳

忘れっぽいのでメモを残しています。浅く適当に書きます。

JSF利用時のWEB-INFディレクトリ内のファイルの取得方法。

スポンサーリンク

例えば、/WEB-INF/web.xml ファイルは以下のように取得できる。

String fileName = "/WEB-INF/web.xml";
ServletContext servletContext = (ServletContext) FacesContext.getCurrentInstance().getExternalContext().getContext();
File file = new File(servletContext.getRealPath(fileName));


また、クラスパスルート(/WEB-INF/classes)の配下にあるファイル(例:log4j.properties)は以下のように取得できる(はず)。
URL url = getClass().getClassLoader().getResource("log4j.properties");
File file = new File(url.toURI());


勿論、上記の2つのコードは Tomcat 上で動かしている前提の話。


参考資料:

関連記事: