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);
例外是:
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。我怎么解决这个问题?谢谢。
答案 0 :(得分:1)
您需要配置webapp容器以解压缩WAR文件。 File
不能代表档案文件中的成员。
答案 1 :(得分:1)
您可以使用ClassPathResource.getInputStream()
方法获取输入流并更改loadProps
以从InputStream加载属性(请参阅http://download.oracle.com/javase/1.4.2/docs/api/java/util/Properties.html#load(java.io.InputStream))。
您无需解压缩存档以使其正常工作。
答案 2 :(得分:1)
好的,解决方案是:
String emsHome = null;
ClassPathResource emsInitResource = new ClassPathResource("ems-init.properties");
Properties properties = loadProps(emsInitResource.getInputStream());
logger.info("found 'ems-init.properties' on classpath, processing...");
emsHome = properties.getProperty("ems.home");
if (emsHome != null) {
logger.info("'ems.home' property initialized from 'ems-init.properties' as '" + emsHome + "'");
}
//=================================================================================================
FilenameFilter filenameFilter = new FilenameFilter() {
public boolean accept(File dir, String name) {
return name.startsWith("messages_") && name.endsWith(".properties");
}
};
URL emsInitUrl = this.getClass().getClassLoader().getResource("ems-init.properties");
emsInitUrl.openConnection();
VirtualFile emsInitVirtualFile = (VirtualFile) emsInitUrl.getContent();
File emsInitFile = emsInitVirtualFile.getPhysicalFile();
File[] messagePropertiesFiles = emsInitFile.getParentFile().listFiles(filenameFilter);
String locales = "en";
for (File file : messagePropertiesFiles) {
int endIndex = file.getName().indexOf('.');
String localeCode = file.getName().substring(9, endIndex);
locales += "," + localeCode;
}
logger.info("locales available configured are '" + locales + "'");
properties.setProperty("ems.locales", locales);