gpt4 book ai didi

java - ClassPathResource.getFile() 在 WAR 中抛出 FileNotFoundException

转载 作者:行者123 更新时间:2023-12-02 00:42:51 26 4
gpt4 key购买 nike

ClassPathResource.getFile() 抛出 FileNotFoundException。这是代码片段:

    ClassPathResource emsInitResource = new ClassPathResource("ems-init.properties");
Properties props = loadProps(emsInitResource.getFile());
logger.info("found 'ems-init.properties' on classpath, processing...");
emsHome = props.getProperty("ems.home");
if (emsHome != null) {
logger.info("'ems.home' property initialized from 'ems-init.properties' as '" + emsHome + "'");
}
FilenameFilter ff = new FilenameFilter() {
public boolean accept(File dir, String name) {
return name.startsWith("messages_") && name.endsWith(".properties");
}
};
File[] messagePropsFiles = emsInitResource.getFile().getParentFile().listFiles(ff);
String locales = "en";
for (File f : messagePropsFiles) {
int endIndex = f.getName().indexOf('.');
String localeCode = f.getName().substring(9, endIndex);
locales += "," + localeCode;
}
logger.info("locales available configured are '" + locales + "'");
props.setProperty("ems.locales", locales);

异常(exception)是:

9:38:04,902 INFO  [STDOUT] Caused by: java.io.FileNotFoundException: class path resource [ems-init.properties] cannot be resolved to absolute file path because it does not reside in the file system: vfs:/home/tanmoy/JBoss/jboss-as-distribution-6.0.0.Final/server/default/deploy/EMS.war/WEB-INF/classes/ems-init.properties
19:38:04,902 INFO [STDOUT] at org.springframework.util.ResourceUtils.getFile(ResourceUtils.java:201)
19:38:04,902 INFO [STDOUT] at org.springframework.core.io.ClassPathResource.getFile(ClassPathResource.java:175)
19:38:04,902 INFO [STDOUT] at info.ems.config.EMSConfigurer.configureEMS(EMSConfigurer.java:45)
19:38:04,902 INFO [STDOUT] at info.ems.config.EMSConfigurer.postProcessBeanFactory(EMSConfigurer.java:34)

但是在 WAR 中/WEB-INF/classes/ems-init.properties 是存在的。我怎么解决这个问题?谢谢。

最佳答案

您可以使用 ClassPathResource.getInputStream() 方法获取输入流并更改 loadProps 以从 InputStream 加载属性(请参阅 http://download.oracle.com/javase/1.4.2/docs/api/java/util/Properties.html#load(java.io.InputStream ) )。

您无需解压存档即可使其工作。

关于java - ClassPathResource.getFile() 在 WAR 中抛出 FileNotFoundException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5848975/

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