【发布时间】:2016-06-06 06:55:26
【问题描述】:
我正在尝试从 maven 项目的资源文件夹(src/main/resources)加载一个 excel 文件。
文件夹结构
MyWebApp
|______src/main/java
| |____Test.java
|
|______src/main/resources
| |______test
| |___hello.properties
| |___template.xlsx
|______target
|___MyWebApp
|____WEB_INF
|___classes
|__test
|__hello.properties
|__template.xlsx
我的方法
//loading excel file
String resource = "/test/template.xlsx";
System.out.println(this.getClass().getResource(resource) == null); // prints true
//loading properties file
String resource = "/test/hello.properties";
System.out.println(this.getClass().getResource(resource) == null); //prints false
//I have also tried below methods
this.getClass().getClassLoader().getResourceAsStream(resource); //null
new ClassPathResource(resource).getInputStream(); //null
做了一些谷歌搜索后,我知道了,maven filters binary contents。为了克服我修改了我的pom.xml 以允许.xlsx,.xls 文件扩展名不被这个help 过滤。
pom.xml
<configuration>
<outputDirectory>${project.build.directory}/${project.build.finalName}/WEB-INF/classes</outputDirectory>
<resources>
<resource>
<directory>${basedir}/src/main/resources</directory>
<includes>
<include>**/*.xlsx</include>
<include>**/*.xls</include>
</includes>
</resource>
</resources>
</configuration>
我可以加载属性文件,但我无法使用上述方法加载 excel 文件。在我这边,我提到了以下两个链接(Rererence-1, Reference-2) 但没有成功。如果您对此问题有一些想法/想法,请帮助我。
【问题讨论】:
标签: java excel maven apache-poi classpath