gpt4 book ai didi

java - 从 Maven 项目资源文件夹 (src/main/resources) 加载 excel(.xlsx/.xls) 文件

转载 作者:行者123 更新时间:2023-12-01 09:50:06 24 4
gpt4 key购买 nike

这里我试图从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-1Reference-2 )但没有成功。如果您对此问题有任何想法/想法,请帮助我。

最佳答案

the maven documentation page您在其中链接的内容是:

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.

因此您应该将属性和 xlsx 文件保存在不同的目录中。

还有一个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>

关于java - 从 Maven 项目资源文件夹 (src/main/resources) 加载 excel(.xlsx/.xls) 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37651233/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com