Tag: classpath

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

在这里,我正试图从一个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 […]

通过apache poi读取excel文件(在classpath中)

我想阅读(使用Apache poi).xlsx文件,这是不是在文件系统,但在类path。 我正在使用maven – 所以它在资源文件夹。 我的代码是 – InputStream resourceAsStream = MyReader.class.getClassLoader().getResourceAsStream("test.xlsx"); Workbook wb = new XSSFWorkbook(resourceAsStream); 我得到这个例外。 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] 当我从文件系统读取相同的文件时,一切都很好。 我的代码中是否有错误,或者我想知道什么? UPDATE1:这是在web应用程序,所以代码部署在tomcat 7。 更新2:当我用这种方式读取相同的文件 – 它的工作原理。 File file = new File("C:\\Users\\…..\\test.xlsx"); […]