有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

war中打包后资源中文件的java FileNotFoundException

我有下面的代码,当它直接从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]

问题在哪里?我做错了什么

PSWorkbookFactoryApache poi库的一部分


共 (0) 个答案