java—如何使用classpathresource从现有文件创建文件

i2byvkas  于 2021-07-13  发布在  Java
关注(0)|答案(0)|浏览(836)

我正在开发一个java应用程序,它从数据库中填充数据并将其导出到ods文件中。
我开发了一个odsexport模块,它向主模块公开了一个方法generateodsfilewithtemplate,这样它就可以生成文件了,之后我在主模块的pom.xml中添加了odsexport的依赖关系。
我现在面临的问题是,我在主模块中创建了一个文件模板,当我调用odsexport并传递模板时,我得到一个异常,告诉我文件不存在,这是我的代码

@Override
  public File generateODSFileWithTemplate(Object[][] data) {

try {

      Resource resource = new ClassPathResource("resources/template.ods");
      File template = resource.getFile();

      //Calling ODSExport module
      return iODSExport.generateODSFileWithTemplate("outputFile.ods", data, template,
          Integer.parseInt(ExportODSEnum.ROW_INDEX.getValue()),
          Integer.parseInt(ExportODSEnum.COLUMN_INDEX.getValue()));

    } catch ( IOException ex) {
      LOGGER.info(ex.getMessage());
      return null;
    } 
}

异常堆栈跟踪:

class path resource [resources/template.ods] cannot be resolved to URL because it does not exist.

my template.ods位于主模块的src/main/resources下。我已经将resources文件夹添加到类路径中,但我似乎不明白为什么会触发此异常。

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题