1

我在迁移到 Maven3 的旧版 Web 应用程序时遇到问题。

我需要从目录结构中位于的 Classpath 中获取一个文件:

/src/main/resources/com/thinkglish/geoip/GeoIP.dat

当我使用 Maven 构建创建 .war 文件时,我可以确认此 .dat 文件位于(应该位于)以下位置:

WEB-INF/classes/com/thinkglish/geoip/GeoIP.dat

我正在尝试两种不同的方法来从我的一个类中获取资源,该类实现了javax.servlet.Filter

ClassPathResource resource = new ClassPathResource("com/thinkglish/geoip/GeoIp.dat");

URL resource = getClass().getResource("/com/thinkglish/geoip/GeoIp.dat");

如果我使用 Maven 的 Jetty 插件启动应用程序,这两种方式都可以正常工作。但是,当我在 Tomcat 中部署应用程序并启动服务器时,找不到资源。

在第一种情况下,我得到 a java.io.FileNotFoundException: class path resource [com/thinkglish/geoip/GeoIp.dat] cannot be resolved to URL because it does not exist,在第二种情况下,资源是null.

关于这一切的一个奇怪的事情是,如果我使用一种方法或另一种方法试图从类路径(例如com/thinkglish/struts/i18n/MessageResources.propertiescom/thinkglish/filter/LanguageFilter.class)获取另一个资源,它可以正常工作。

你对此有任何猜测吗?.dat 扩展名是否可能与此有关?


已编辑 - 更多数据!

我在 .dat 文件所在的完全相同的目录中添加了一个新的 .properties 模拟文件:

/src/main/resources/com/thinkglish/geoip/mock.properties

我试图在 Tomcat6 中获取它并且它有效

ClassPathResource resource = new ClassPathResource("com/thinkglish/geoip/mock.properties");

我开始认为我需要在配置方面做一些其他事情,以使 Tomcat6 接受 .dat 文件作为类路径资源。

提前致谢!

4

2 回答 2

0

以下应该工作:

String classpathLocation = "com/thinkglish/geoip/GeoIp.dat";
URL classpathResource = Thread.currentThread().getContextClassLoader().getResource(classpathLocation);
// Or:
InputStream input = Thread.currentThread().getContextClassLoader().getResourceAsStream(classpathLocation);
于 2012-10-05T18:52:37.710 回答
0

我可能在这里完全看错了树...但是您检查过 GeoIP.dat / GeoIp.dat 的大写吗?Tomcat 是否在区分大小写的操作系统上运行?

于 2012-10-06T09:37:12.127 回答