![](/img/trans.png)
[英]cannot get folder from src/main/resources Java project Maven
[英]Loading an excel(.xlsx/.xls) file from maven project resources folder (src/main/resources)
在這里,我試圖從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過濾二進制內容 。 為了pom.xml
這個問題,我修改了pom.xml
以允許在此幫助下不過濾.xlsx
和.xls
文件擴展名。
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 ),但沒有成功。 如果您對此問題有任何想法/想法,請幫助我。
在您已經鏈接到的Maven文檔頁面中說:
如果同時具有文本文件和二進制文件作為資源,建議使用兩個分開的文件夾 。 一個文件夾src / main / resources(默認)用於未過濾的資源,另一個文件夾src / main / resources-filtered用於被過濾的資源。
因此,您應該將屬性和xlsx文件保存在單獨的目錄中。
還有關於從過濾中排除二進制文件的信息 :
<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.