首页 > 解决方案 > 将应用部署到 Google Cloud 平台时未检测到相对文件路径

问题描述

在本地一切正常,但是一旦我将我的应用程序部署到 App Engine。谷歌云没有获取相对文件路径

标签: javaspring-bootgoogle-cloud-platformfilepath

解决方案


根据Where to put your static files ,

您必须将静态提供的文件放在应用程序的 webapp 目录中。您可以使用文件夹,但请记住所有文件路径和 URI 都是相对于 webapp 目录的。

为静态文件选择位置后,您必须使用元素在 appengine-web.xml 文件中定义它们的位置。

一个如何配置的例子是:

<appengine-web-app xmlns="http://appengine.google.com/ns/1.0">
  <threadsafe>true</threadsafe>
  <runtime>java8</runtime>
  <static-files>
    <include path="/**.html" >
    </include>
  </static-files>
</appengine-web-app>

此外,在文档中:[直接从您的应用提供文件](https://cloud.google.com/appengine/docs/standard/java11/serving-static-files#serving_from_your_application),它提到:

要在标准环境中为 Java 11 提供静态文件,您可以使用 static_dir 或 static_files 元素在 app.yaml 文件中定义处理程序。

例如,以下行指示浏览器加载 main.css文件:

<link type="text/css" rel="stylesheet" href="/static/css/main.css">

./public目录在static_dir项目文件的元素中定义app.yaml

handlers:
  - url: /favicon\.ico
    static_files: favicon.ico

  - url: /static
    static_dir: public

  - url: /.*
    secure: always
    redirect_http_response_code: 301
    script: auto

推荐阅读