简体   繁体   English

ServletContext.getResourceAsStream返回null

[英]ServletContext.getResourceAsStream returning null

I haven't had much luck finding out why this is returning null for ServletContext objects. 我没有太多运气能找到为什么它为ServletContext对象返回null的原因。

I've tried using relative and absolute file locations, both are returning null. 我尝试使用相对和绝对文件位置,两者都返回null。 I've tried moving the template to a deeper path, as i've read elsewhere on SO but it still returns a null. 我尝试将模板移到更深的路径,因为我在SO上的其他地方已经读过,但它仍返回null。 Perhaps I'm not quite understanding why it's returning null, is it not finding the file? 也许我不太了解为什么它返回null,是否找不到文件? It's not throwing an exception, so I'm having troubles understanding what is going on here. 它不会引发异常,因此我很难理解这里发生的事情。

ServletContext context = getServletContext();
InputStream is = context.getResourceAsStream("/WEB-INF/" + templateLocation);

It is not finding the file. 找不到文件。 The documentation of ServletContext specifies that getResourceAsStream returns ServletContext的文档指定getResourceAsStream返回

the InputStream returned to the servlet, or null if no resource exists at the specified path InputStream返回到servlet;如果指定路径上没有资源,则返回null

Note that not exceptions are thrown if the file doesn't exist. 请注意,如果文件不存在,则不会引发异常。 Check that the file is actually present in (tomcat path)/webapp/(context)/WEB-INF/(templateLocation) on the remote server. 检查该文件是否确实存在于远程服务器上的(tomcat路径)/ webapp /(上下文)/ WEB-INF /(templateLocation)中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM