Java 如何从 WEB-INF 目录加载文件/属性?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9663564/
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-16 05:48:50  来源:igfitidea点击:

How to load files/properties from WEB-INF directory?

javatapestryshirotynamo

提问by xyz

It seems that in my Tapestry app, I can't load ini files nor properties file from WEB-INF directory or class path.

似乎在我的 Tapestry 应用程序中,我无法从 WEB-INF 目录或类路径加载 ini 文件或属性文件。

I tried several different methods which should load my file but non of them worked.

我尝试了几种不同的方法,它们应该加载我的文件,但它们都不起作用。

  1. ex
  1. 前任

realm.setResourcePath("/WEB-INF/auth.properties");

realm.setResourcePath("/WEB-INF/auth.properties");

  1. ex
  1. 前任

realm.setResourcePath("classpath:wip/pages/auth.properties");

realm.setResourcePath("classpath:wip/pages/auth.properties");

I need to load properties/ini file in order to use tapestry-security module which is based on Shiro.

我需要加载 properties/ini 文件才能使用基于 Shiro 的挂毯安全模块。

Thanks for help !

感谢帮助 !

采纳答案by ascandroli

The root of the classpath is the way to go. Put your file in src/main/resources/auth.propertiesthen set your resourcePathusing realm.setResourcePath("classpath:auth.properties");

类路径的根是要走的路。将您的文件放在src/main/resources/auth.properties 中,然后使用 realm.setResourcePath("classpath:auth.properties");设置您的资源路径。

Check the ExtendedPropertiesRealmand the tapestry-security testapp for an example

检查ExtendedPropertiesRealm和挂毯安全测试应用程序的示例

回答by Andrzej Jozwik

Try ServletContext.getResourceAsStream("/WEB-INF/auth.properties")or ServletContext.getResourceAsStream("WEB-INF/auth.properties")

尝试ServletContext.getResourceAsStream("/WEB-INF/auth.properties")ServletContext.getResourceAsStream("WEB-INF/auth.properties")

ServletContext has to be use from servlet, servletListener etc.

ServletContext 必须在 servlet、servletListener 等中使用。

回答by Abhisek Mohapatra

Try

尝试

Properties props = new Properties();
props.load(new FileInputStream(new File(req.getServletContext().getRealPath("/WEB-INF/fileName.properties"))));
System.out.println(props);

回答by Colin D

I found the easiest way was to

我发现最简单的方法是

  • put file in src/main/resources/config.properties. This will be put in /WEB-INF/classes/config.properties when the project is compiled by maven into WAR

  • read the file from a servlet with the following

    InputStreaminputStream = getClass().getClassLoader().getResourceAsStream("config.properties");

  • 将文件放在 src/main/resources/config.properties 中。这个会在maven编译成WAR的时候放在/WEB-INF/classes/config.properties

  • 使用以下命令从 servlet 读取文件

    InputStreaminputStream = getClass().getClassLoader().getResourceAsStream("config.properties");

https://crunchify.com/java-properties-file-how-to-read-config-properties-values-in-java/

https://crunchify.com/java-properties-file-how-to-read-config-properties-values-in-java/