I'm trying to read in an html file as a string using InputStream but no matter what I try I keep getting a null pointer exception. The File I am trying to read is at "/war/index.html" and the code to read it in looks like this:
File f = new File(path);
ServletContext context = getServletContext();
InputStream is = context.getResourceAsStream(f.getAbsolutePath());
int data = is.read();
As soon as I call is.read() it gives me a NullPointerException. Any help is appreciated thanks!
Here seems to be 2 issues combined:
java.dir, which in most cases is not the same, as webapps folder of web-containerwar indicator in your path.Please check how ServletContext resolves files.
So you simply need to use:
ServletContext context = getServletContext();
InputStream is = context.getResourceAsStream("/index.html");
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With