【问题标题】:ClassPathResource.getFile() throws FileNotFoundException within WARClassPathResource.getFile() 在 WAR 中抛出 FileNotFoundException
【发布时间】:2011-05-01 14:26:37
【问题描述】:

ClassPathResource.getFile() 抛出 FileNotFoundException。这是代码 sn-p :

    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。我怎么解决这个问题?谢谢。

【问题讨论】:

标签: java spring filenotfoundexception


【解决方案1】:

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

您无需解压存档即可使用。

【讨论】:

  • 成功了。但是我需要从该路径加载其他文件,正如您在我的代码“File[] messagePropsFiles = emsInitResource.getFile().getParentFile().listFiles(ff)”底部看到的那样,我怎样才能做到这一点?
  • 您可以使用相同的技巧(使用getInputStream),但通常使用来自档案的资源(jar/war 或一般而言的类路径)您不能使用 File.listFiles,因此您必须修复它在构建时(生成所需资源的列表并将其放入可以从 Web 应用程序内部访问的已知类路径资源中。
【解决方案2】:

你需要配置你的 webapp 容器来解压 WAR 文件。 File 不能代表存档文件中的成员。

【讨论】:

  • @Tapas Bose - 使用 jar 实用程序,或者(更好地)配置您的 Web 容器以自动解包。 Tomcat 默认执行此操作。你用的是什么容器?
【解决方案3】:

好的,解决办法是:

    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);

【讨论】:

  • VirtualFile 的包是什么?
  • @cosbor11 org.jboss.vfs.VirtualFile
猜你喜欢
  • 2020-02-04
  • 1970-01-01
  • 2012-06-19
  • 2020-07-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多