我有以下代码,当直接从idea执行时,它可以正常工作
log.info("00");
File file = new File(classLoader.getResource("template/legalStatistic.xlsx").getFile());
log.info("01");
log.info(String.valueOf(file == null));
log.info(file.getAbsolutePath());
Workbook workbook = null;
try {
workbook = WorkbookFactory.create(new FileInputStream(file));
} catch (IOException e) {
log.error(System.getProperty("user.dir"));
throw new RuntimeException("There is some issues with file template/legalStatistic.xlsx", e);
}
但是,当此代码打包到war文件中并执行但命令nohup java -Dfile.encoding=UTF-8 -jar name.war
执行时,出现异常:
INFO 14859 --- [nio-9001-exec-1] biz.Services.ExportToExcelService : 00
INFO 14859 --- [nio-9001-exec-1] biz.Services.ExportToExcelService : 01
INFO 14859 --- [nio-9001-exec-1] biz.Services.ExportToExcelService : false
INFO 14859 --- [nio-9001-exec-1] biz.Services.ExportToExcelService : /home/bizon4ik/java/pitstop/target/file:/home/bizon4ik/java/pitstop/target/PitStop-1.0.war!/WEB-INF/classes!/template/legalStatistic.xlsx
ERROR 14859 --- [nio-9001-exec-1] biz.Services.ExportToExcelService : /home/bizon4ik/java/pitstop/target
ERROR 14859 --- [nio-9001-exec-1] o.a.c.c.C.[.[.[/].[dispatcherServlet] : Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is java.lang.RuntimeException: There is some issues with file template/legalStatistic.xlsx] with root cause
java.io.FileNotFoundException: file:/home/bizon4ik/java/pitstop/target/PitStop-1.0.war!/WEB-INF/classes!/template/legalStatistic.xlsx (No such file or directory)
at java.io.FileInputStream.open0(Native Method) ~[na:1.8.0_181]
问题出在哪里?我做错了什么?
PS WorkbookFactory
是Apache poi
lib的一部分。