简体   繁体   English

从WEB-INF文件夹获取图像

[英]Getting image from WEB-INF folder

I trie to get an image from my image folder which is placed in the WEB-INF. 我试图从放置在WEB-INF中的图像文件夹中获取图像。 But i get the NullPointerException. 但是我得到了NullPointerException。

   Image logo = Image.getInstance(this.getClass().getResource("/resources/img/auditweb.png"));

在此处输入图片说明

EDIT: 编辑:

Have change the code as suggested 根据建议更改代码

Image logo = Image.getInstance(this.getClass().getResource("/WEB-INF/resources/img/auditweb.png"));

But didn't work 但是没用

neither the root folder of your webapp, not WEB-INF, are in the classpath. 您的Web应用程序的根文件夹(不是WEB-INF)都不在类路径中。 You thus can't load a resource that is located there with the ClassLoader, which looks into the classpath. 因此,您无法使用ClassLoader加载位于该资源的资源,该资源会查看类路径。

You need to use ServletContext.getResource() , or to put the images undere WEB-INF/classes . 您需要使用ServletContext.getResource()或将图像放在WEB-INF/classes

You are getting NullPointerException because you are providing the wrong path. 您正在获取NullPointerException因为您提供了错误的路径。

Image logo = Image.getInstance(this.getClass().getResource("/WEB-INF/resources/img/auditweb.png"));
                                                             /\
                                                             ||
                                                             ||
                                                             ||
                        Path should start with WEB-INF  ------

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

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