Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Google App Engine Java: Reading in an html file problems

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!

like image 443
LeeFoster Avatar asked Apr 26 '26 23:04

LeeFoster


1 Answers

Here seems to be 2 issues combined:

  • by default when you create file with relative path, working directory in this case is java.dir, which in most cases is not the same, as webapps folder of web-container
  • you seem to have extra war 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");
like image 174
n1ckolas Avatar answered Apr 29 '26 14:04

n1ckolas



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!