[英]Loading an excel(.xlsx/.xls) file from maven project resources folder (src/main/resources)
Here i am trying to load an excel file from resources folder(src/main/resources) of a maven project. 在这里,我试图从Maven项目的资源文件夹(src / main / resources)加载Excel文件。
Folder Structure 资料夹结构
MyWebApp
|______src/main/java
| |____Test.java
|
|______src/main/resources
| |______test
| |___hello.properties
| |___template.xlsx
|______target
|___MyWebApp
|____WEB_INF
|___classes
|__test
|__hello.properties
|__template.xlsx
My Approach 我的方法
//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
After doing some googling i came to know, maven filters binary contents . 在进行了一次谷歌搜索之后,我知道了, maven过滤二进制内容 。 To over come that i modified my pom.xml
to allow .xlsx
, .xls
file extenstions not to be filtered with this help . 为了pom.xml
这个问题,我修改了pom.xml
以允许在此帮助下不过滤.xlsx
和.xls
文件扩展名。
pom.xml 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>
I could able to load the properties file, but i could not able to load the excel file by using above approach. 我可以加载属性文件,但是无法使用上述方法加载excel文件。 From my side i referred the below two links ( Rererence-1 , Reference-2 ) but no success. 在我看来,我提到了以下两个链接( Rererence-1和Reference-2 ),但没有成功。 Please help me if you have some thoughts/ideas on this issue. 如果您对此问题有任何想法/想法,请帮助我。
In the maven documentation page which you've linked in your there is said: 在您已经链接到的Maven文档页面中说:
If you have both text files and binary files as resources it is recommended to have two separated folders . 如果同时具有文本文件和二进制文件作为资源,建议使用两个分开的文件夹 。 One folder src/main/resources (default) for the resources which are not filtered and another folder src/main/resources-filtered for the resources which are filtered. 一个文件夹src / main / resources(默认)用于未过滤的资源,另一个文件夹src / main / resources-filtered用于被过滤的资源。
So you should hold the properties and xlsx files in separate directories. 因此,您应该将属性和xlsx文件保存在单独的目录中。
There is also an information about excluding binary files from filtering : 还有关于从过滤中排除二进制文件的信息 :
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-resources-plugin</artifactId>
<version>3.0.0</version>
<configuration>
...
<nonFilteredFileExtensions>
<nonFilteredFileExtension>pdf</nonFilteredFileExtension>
<nonFilteredFileExtension>swf</nonFilteredFileExtension>
</nonFilteredFileExtensions>
...
</configuration>
</plugin>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.