Read excel file (which is in classpath) via apache poi

I am trying to read (using apache poi) a .xlsx file which is not in the file system, but in the classpath. I am using maven - so this is in the resources folder.

my code is

InputStream resourceAsStream = MyReader.class.getClassLoader().getResourceAsStream("test.xlsx"); Workbook wb = new XSSFWorkbook(resourceAsStream); 

I get this exception.

 Caused by: java.lang.IllegalArgumentException: MALFORMED at java.util.zip.ZipCoder.toString(ZipCoder.java:58) ~[?:1.7.0_51] at java.util.zip.ZipInputStream.readLOC(ZipInputStream.java:297) ~[?:1.7.0_51] at java.util.zip.ZipInputStream.getNextEntry(ZipInputStream.java:121) ~[?:1.7.0_51] at org.apache.poi.openxml4j.util.ZipInputStreamZipEntrySource.<init>(ZipInputStreamZipEntrySource.java:51) ~[poi a3] at org.apache.poi.openxml4j.opc.ZipPackage.<init>(ZipPackage.java:88) ~[poi-ooxml-3.11-beta3.jar:3.11-beta3] at org.apache.poi.openxml4j.opc.OPCPackage.open(OPCPackage.java:272) ~[poi-ooxml-3.11-beta3.jar:3.11-beta3] at org.apache.poi.util.PackageHelper.open(PackageHelper.java:37) ~[poi-ooxml-3.11-beta3.jar:3.11-beta3] 

When I read the same file from the file system, everything is fine. Is there a mistake in my code or am I missing something to understand?

UPDATE1: This is in a web application, so the code is deployed to tomcat 7.

UPDATE2: when I read the same file this way - it works.

 File file = new File("C:\\Users\\.....\\test.xlsx"); FileInputStream fileInputStream = new FileInputStream(file); Workbook wb = new XSSFWorkbook(fileInputStream); 

thanks

+5
java classpath maven excel apache-poi
source share
3 answers

After spending days on this issue, I found the answer in stackoverflow)). FileInputStream vs ClassPathResource vs getResourceAsStream and file integrity

The maven-resources-plugin filtering problem, it distorts the excel file.

 You should not filter binary files like excel and use two mutually exclusive resource sets as described at the bottom of this page 

maven resource plugin

+7
source share

Adding additional information to @ user1321466's answer, to filter, you can do as described in the maven resource plugin:

If you have both text files and binary files as resources, it is recommended that you have two separate folders. One src / main / resources folder (default) for resources that are not filtered and another src / main / resources-filter folder for resources that are filtered.

or just exclude files from filtering:

 <resource> <directory>src/main/resources</directory> <filtering>true</filtering> <excludes> <exclude>**/*.xsd</exclude> <exclude>**/*.xml</exclude> </excludes> </resource> <resource> <directory>src/main/resources</directory> <filtering>false</filtering> <includes> <include>**/*.xsl</include> <include>**/*.xslx</include> </includes> </resource> 
+1
source share

Is the file at the top of the classpath i.e. in WEB-INF / classes?

The API document for Classloader.getResource () states that the resource name:

"A resource name is a name, separated by" / ", that identifies the resource."

So, if your file is located in some subdirectory, this path should be part of the resource name.

0
source share

All Articles