Java 如何从 Web 存档的 WEB-INF 目录加载资源

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1108434/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 23:52:12  来源:igfitidea点击:

How to load a resource from WEB-INF directory of a web archive

java

提问by Michael Wiles

I have a web archive with a file placed in the WEB-INF directory.

我有一个 Web 存档,其中一个文件位于 WEB-INF 目录中。

How do I load that file in a java class?

如何在 java 类中加载该文件?

I know I can put it in the classes directory and load it from there. It would just be put it in WEB-INF.

我知道我可以将它放在 classes 目录中并从那里加载它。它只会放在 WEB-INF 中。

采纳答案by skaffman

Use the getResourceAsStream()method on the ServletContext object, e.g.

使用getResourceAsStream()ServletContext 对象上的方法,例如

servletContext.getResourceAsStream("/WEB-INF/myfile");

How you get a reference to the ServletContext depends on your application... do you want to do it from a Servlet or from a JSP?

您如何获得对 ServletContext 的引用取决于您的应用程序……您是想从 Servlet 还是从 JSP 中获得它?

EDITED: If you're inside a Servlet object, then call getServletContext(). If you're in JSP, use the predefined variable application.

编辑:如果您在 Servlet 对象中,则调用getServletContext(). 如果您使用的是 JSP,请使用预定义变量application

回答by webcom

Here is how it works for me with no Servlet use.

这是它在不使用 Servlet 的情况下对我的工作方式。

Let's say I am trying to access web.xml in project/WebContent/WEB-INF/web.xml

假设我试图访问 project/WebContent/WEB-INF/web.xml 中的 web.xml

  1. In project property Source-tab add source folder by pointing to the parent container for WEB-INF folder (in my case WebContent )

  2. Now let's use class loader:

    InputStream inStream = class.getClass().getClassLoader().getResourceAsStream("Web-INF/web.xml")
    
  1. 在项目属性 Source-tab 中,通过指向 WEB-INF 文件夹的父容器(在我的情况下为 WebContent )添加源文件夹

  2. 现在让我们使用类加载器:

    InputStream inStream = class.getClass().getClassLoader().getResourceAsStream("Web-INF/web.xml")
    

回答by Ted_Zactly

The problem I had accessing the sqlite db file I created in my java (jersey) server had solely to due with path. Some of the docs say the jdbc connect url should look like "jdbc:sqlite://path-to-file/sample.db". I thought the double-slash was part of a htt protocol-style path and would map properly when deployed, but in actuality, it's an absolute or relative path. So, when I placed the file at the root of the WebContent folder (tomcat project), a uri like this worked "jdbc:sqlite:sample.db".

我访问我在我的 java (jersey) 服务器中创建的 sqlite db 文件的问题完全与路径有关。一些文档说 jdbc 连接 url 应该看起来像“jdbc:sqlite://path-to-file/sample.db”。我认为双斜杠是 htt 协议样式路径的一部分,并且在部署时会正确映射,但实际上,它是绝对或相对路径。因此,当我将文件放在 WebContent 文件夹(tomcat 项目)的根目录时,像这样的 uri 可以工作“jdbc:sqlite:sample.db”。

The one thing that was throwing me was that when I was stepping through the debugger, I received a message that said "opening db: ... permission denied". I thought it was a matter of file system permissions or perhaps sql user permissions. After finding out that SQLite doesn't have the concept of roles/permissions like MySQL, etc, I did eventually change the file permissions before I came to what I believe was the correct solution, but I think it was just a bad message (i.e. permission denied, instead of File not found).

抛出我的一件事是,当我单步调试调试器时,我收到一条消息,上面写着“打开数据库:...权限被拒绝”。我认为这是文件系统权限或 sql 用户权限的问题。在发现 SQLite 没有像 MySQL 等角色/权限的概念后,我最终确实更改了文件权限,然后才找到我认为正确的解决方案,但我认为这只是一个坏消息(即权限被拒绝,而不是文件未找到)。

Hope this helps someone.

希望这可以帮助某人。