代码之家  ›  专栏  ›  技术社区  ›  Bizon4ik

在war中打包后资源中的文件出现FileNotFoundException[duplicate]

  •  0
  • Bizon4ik  · 技术社区  · 6 年前

    我有以下代码,当它直接从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);
        }
    

    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]
    

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

    WorkbookFactory Apache poi 解放军。

    0 回复  |  直到 6 年前