在eclipse中放置属性文件的位置以及如何在本地和服务器上访问路径 [英] Where to put properties file in eclipse and how to access the path both locally and on server
问题描述
我在这看到了一些类似的问题,但我对它们的了解不够,所以我想问问题并按照自己的方式进行调整。
I saw some similar questions on this,but I don't understand them enough so i want to ask the question and tailor it my own way.
我有我存储在src / main / resources中的属性文件,然后在我的代码中引用它,如下所示:
I have a properties file that I store in src/main/resources, and then reference this in my code like so:
Properties prop = new Properties();
InputStream input = null;
try {
input = new FileInputStream("src/main/resources/app.properties");
// load a properties file
prop.load(input);
这在本地工作,但是当我创建.war文件并将其部署到服务器时,它没有想法属性文件的位置。我在哪里放置这个属性文件,如何在代码中引用它,以便我可以在本地运行它,并在服务器上运行没有任何问题?
this works locally, however when I create a .war file and deploy it to the sever it has no idea where the properties file is. Where do I put this properties file, and how do I reference it in the code, so that I can run it locally, and on the server without any problems?
推荐答案
我假设您有maven项目,可以将该属性文件放在 src / main / resources /app.properties
并更改代码以将其读取为
I assume you have maven project, you can put that properties file at src/main/resources/app.properties
and change the code to read it as
getClass().getClassLoader().getResourceAsStream("/app.properties");
这篇关于在eclipse中放置属性文件的位置以及如何在本地和服务器上访问路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!