gpt4 book ai didi

java - 应用程序在 Tomcat 服务器上找不到属性文件

转载 作者:行者123 更新时间:2023-11-28 22:54:48 25 4
gpt4 key购买 nike

我试图让 Tomcat 加载外部属性文件,该文件存在于 WAR 文件的类路径之外。

我有一个名为 PropertiesSingleton 的简单类,它应该将属性文件加载到 java.util.Properties 对象中。

package my.test.properties;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;


public class PropertiesSingleton {
private static String filenameProperty = "testprops";
private static IProperties properties; //This is an interface that defines a number of get methods for each property
private final static Logger log = LoggerFactory
.getLogger(PropertiesSingleton.class);

public static void main(String[] args) {
IProperties props = PropertiesSingleton.load();
}

public static synchronized IProperties load() {
InputStream stream;
if (properties == null) {
try {
String propertyFileName = System.getProperty(filenameProperty);
String variableFileName = System.getenv(filenameProperty);
log.debug("Value of "+filenameProperty+" parameter: "+propertyFileName);
log.debug("Value of "+filenameProperty+" env variable: "+variableFileName);
PropertiesImpl props = new PropertiesImpl(); //A subclass of java.util.Properties implementing the IProperties interface
if(propertyFileName != null && !propertyFileName.isEmpty()){
File file = new File(propertyFileName);
log.debug("Loading properties from parameter '"+propertyFileName+"'" + (file.exists() ? "" : ", which does not exist") );
stream = new FileInputStream(file);
}else if(variableFileName != null && !variableFileName.isEmpty()){
File file = new File(variableFileName);
log.debug("Loading properties from environment variable'"+variableFileName+"'"+ (file.exists() ? "" : ", which does not exist"));
stream = new FileInputStream(file);
}else{
log.debug("Loading properties from properties file");
stream = ClassLoader.getSystemClassLoader().getResourceAsStream("my/test/properties/test.properties");
}
if(stream == null){
log.warn("Could not load the properties file");
return props;
}

props.load(stream);
properties = props;
log.info("Loaded properties " + properties);
} catch (IOException ex) {
log.error("Could not load properties", ex);
} catch (Exception ex) {
log.error("Unexpected Error: Could not load properties", ex);
throw ex;
}
}
return properties;
}

public static synchronized IProperties reload(){
properties = null;
return load();
}

}

当我添加一个 Env 属性并通过我的 IDE 运行主要方法时,这似乎按预期工作。

2015-05-26 09:17:02,136 DEBUG Value of testprops parameter: null
2015-05-26 09:17:02,139 DEBUG Value of testprops env variable: C:\test\test.properties
2015-05-26 09:17:02,141 DEBUG Loading properties from environment variable'C:\test\test.properties'
2015-05-26 09:17:02,141 INFO Loaded properties {defaultLocale=dk, host=123.123.123.123}

但是,当我鼓起勇气将这段代码 WAR 打包并在我本地的 Tomcat8 上运行时,我收到了一条令人兴奋的新消息。 (tomcat WAR 在某些时候调用 PropertiesSingleton.load):

2015-05-26 09:17:02,136 DEBUG Value of testprops parameter: null
2015-05-26 09:17:02,139 DEBUG Value of testprops env variable: C:\test\test.properties
2015-05-26 09:17:02,141 DEBUG Loading properties from environment variable'C:\test\test.properties', which does not exist
2015-05-26 09:42:36,114 ERROR Could not load properties
java.io.FileNotFoundException: C:\test\test.properties (The system cannot find the file specified)

Tomcat 中是否有一些安全功能现在允许我访问我的文件系统,或者是其他什么可怕的错误?

我还应注意,我进行此练习是因为我需要一个位于 Tomcat 服务器上的 WAR 上下文之外的外部属性文件。有几个原因。例如,多个应用程序将共享来自另一个正在运行的应用程序的配置。 WAR 中的属性文件将非常不切实际。如果有人知道另一种从 WAR 中读取外部属性文件的方法,那也将被慷慨地接受为答案。

最佳答案

访问底层文件系统不受 servlet 规范支持并且不能保证工作,特别是如果访问发生在未分解的 .war 文件中(我假设这就是发生的事情你的情况)。

它使您的应用程序依赖于底层系统——操作系统、服务器等。recommended way是将您的配置打包到 .war 文件中。

如果你必须这样做(我同意外部配置是个好主意),这里有一个更独立于操作系统的方法,使用 java.net.URLfile: URI scheme :

URL url = new URL("file:///test/test.properties");

URLConnection con = url.openConnection();
ResourceUtils.useCachesIfNecessary(con);

InputStream stream = con.getInputStream();

改编自org.springframework.core.io.UrlResource.getInputStream() .如果可以,请使用 Spring 之类的库/框架及其 Resource 抽象。

关于java - 应用程序在 Tomcat 服务器上找不到属性文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30452658/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com