gpt4 book ai didi

java - Spring Boot 无法使用 ClassPathResource 找到文件

转载 作者:行者123 更新时间:2023-11-30 06:47:33 25 4
gpt4 key购买 nike

使用 Spring 引导。我正在尝试使用 Apache POI 库解析项目中本地存储的 Excel 文件。我的目录结构是这样的:

  • 源代码

    • 主要内容

      • java

        • com.app
          • Controller
          • 型号
          • 存储库<-我从这里打电话
      • 资源

        • 静态
          • Test.xlsx <- 我正在尝试访问此
<小时/>
File excel = new ClassPathResource("Test.xlsx").getFile();

File file = new File(classLoader.getResource("src/main/resources/static/test.xlsx").getFile());

我已经尝试了上述两种方法来访问该文件,并且尝试了该文件路径的多种变体,但没有任何效果。我没有收到构建错误或运行时错误,只是找不到该文件,当我尝试调用该方法时,我在该行收到 NullPointerException。

最佳答案

我相信您的情况正确的是

new ClassPathResource("/static/Test.xlsx").getFile()

关于java - Spring Boot 无法使用 ClassPathResource 找到文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43433999/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com