【问题标题】:Where to place a file for CloudRun to access the file with a Spring Boot application deployed in CloudRunCloud Run 中部署的 Spring Boot 应用程序在何处放置文件以访问该文件
【发布时间】:2023-02-03 21:56:04
【问题描述】:

我是 GCP/SpringBoot 的新手,正在从事一个项目,我有一个场景可以读取项目目录中存在的文件。下面的代码在我使用 localhost 运行时工作正常,但在将 Springboot 应用程序部署到 cloudrun 后失败并显示“找不到文件”。

任何人都可以帮助了解如何读取文件或放置文件的位置。

 InputStream is = new FileInputStream("Legend.jpg");

【问题讨论】:

    标签: spring-boot google-cloud-run


    【解决方案1】:

    不要将文件放在项目的根目录下,最好使用resource 文件。

    你可以把你的文件放在资源文件夹src/main/resources/images/Legend.png

    并按如下方式在 jar 中检索它:

    InputStream stream = ResourceUtil.class.getClassLoader().getResourceAsStream("images/Legend.png");
    

    您还可以查看此 topic 以获得有关从资源文件夹中检索文件的不同方法的更多解释。

    【讨论】:

      猜你喜欢
      • 2021-08-17
      • 1970-01-01
      • 2021-11-17
      • 2019-08-18
      • 2021-08-19
      • 2021-03-19
      • 1970-01-01
      • 2020-06-30
      • 2020-05-15
      相关资源
      最近更新 更多