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

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

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

  1. @Override
  2. public File generateODSFileWithTemplate(Object[][] data) {
  3. try {
  4. Resource resource = new ClassPathResource("resources/template.ods");
  5. File template = resource.getFile();
  6. //Calling ODSExport module
  7. return iODSExport.generateODSFileWithTemplate("outputFile.ods", data, template,
  8. Integer.parseInt(ExportODSEnum.ROW_INDEX.getValue()),
  9. Integer.parseInt(ExportODSEnum.COLUMN_INDEX.getValue()));
  10. } catch ( IOException ex) {
  11. LOGGER.info(ex.getMessage());
  12. return null;
  13. }
  14. }

异常堆栈跟踪:

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

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

暂无答案!

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

相关问题