I have a small java web app and want to have access to some resources during validation.
I know how to do it in servlet since I can use ServletContext, but I don't know how to access it from my validator code.
Resources can be stored in WEB_INF folder or inside packages, but when I tried Class.class.getResourcesAsStream()
it always returned null
.
I feel like I'm doing something wrong, but cannot get what is.
Asked
Active
Viewed 51 times
0
-
Hi, see [ask] and act accordingly (something about an mcve – Kukeltje May 21 '15 at 09:54
1 Answers
0
Well alternatively you can get a file then put an InputStream on it
String path = "resources/images/image.png"
ServletContext ctx = (ServletContext) FacesContext.getCurrentInstance()
.getExternalContext().getContext();
String realPath = ctx.getRealPath("/");
File file = new File(realPath + path);
if the file is in a package then the path would look like
String path= "WEB-INF/classes/package/file"

Ced
- 15,847
- 14
- 87
- 146