Java 8 已达到支持终止期限,并将于 2026 年 1 月 31 日
弃用。弃用后,您将无法部署 Java 8 应用,即使您的组织之前曾使用组织政策重新启用旧版运行时的部署,也无法部署。现有的 Java 8 应用在
弃用日期之后将继续运行并接收流量。我们建议您
迁移到最新支持的 Java 版本。
使用入门:传送静态内容
使用集合让一切井井有条
根据您的偏好保存内容并对其进行分类。
了解如何安全地从 Google App Engine 标准环境中传送静态内容,例如 HTML 文件、CSS 和图片。
准备工作
配置开发环境并创建 App Engine 项目。
如果您的网站使用的是自定义域名,请按照相关说明为您的项目添加自定义网域。
传送网页
App Engine 可以传送静态内容,例如 HTML 页面和图片等媒体。
静态内容是指不会作为 JSP 或 Servlet 执行的任何内容。
以下示例为一个基本 HTML 页面,显示了一条欢迎消息。
<!DOCTYPE html>
<html>
<head>
<title>The App Engine Blog</title>
</head>
<body>
<h1>Welcome to the App Engine Blog</h1>
<p>This is being served by App Engine!</p>
</body>
</html>
放置静态文件的位置
您必须将已传送的静态文件放置在应用的 webapp
目录中。您可以使用文件夹,但请注意,所有文件路径和 URI 都与 webapp
目录相关。
选择静态文件的位置后,您必须使用 <static-files>
元素在 appengine-web.xml
文件中定义该位置。
在以下示例中,基本 appengine-web.xml
配置将 webapp
目录中的所有 HTML 文件均视作静态文件。
<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>
您可以包含多个 <include path>
元素,以引用不同目录和不同文件类型。以下示例是对上述示例的扩展:
<static-files>
<include path="/**.html" >
</include>
<include path="/images/**.jpg" >
</include>
</static-files>
现在,webapp/images/
目录中带有 .jpg
扩展名的所有文件都将被视作静态文件。
在上面的示例中,如果要显示 webapp/images
目录中的 logo.jpg
,<img>
标记将具有来源 URI <img src="/images/logo.jpg">
。
为所有静态内容强制使用 HTTPS
App Engine 支持通过 HTTP 或 HTTPS 传送内容,建议您使用 HTTPS。要设置安全网址,您必须在项目的 web.xml
中添加 <security-constraint>
元素。示例 <security-constraint>
如下所示:
<security-constraint>
<web-resource-collection>
<web-resource-name>blog</web-resource-name>
<url-pattern>/*</url-pattern>
</web-resource-collection>
<user-data-constraint>
<transport-guarantee>CONFIDENTIAL</transport-guarantee>
</user-data-constraint>
</security-constraint>
通过 <transport-guarantee>CONFIDENTIAL</transport-guarantee>
,所有请求都会自动重定向至静态内容的 HTTPS URI。
部署到 App Engine
您可以使用 Maven 将应用部署到 App Engine。
转到项目的根目录并输入以下命令:
mvn package appengine:deploy -Dapp.deploy.projectId=PROJECT_ID
将 PROJECT_ID 替换为您的 Google Cloud 项目的 ID。 如果您的 pom.xml
文件已经指定您的项目 ID,则您无需在运行的命令中添加 -Dapp.deploy.projectId
属性。
Maven 完成应用部署后,输入以下命令可在新应用中自动打开网络浏览器标签:
gcloud app browse
后续步骤
静态文件可用于通过 App Engine 传送图片、层叠样式表和静态 HTML 内容。如需了解更多信息,请参阅通过 HTML 表单处理用户数据。
如未另行说明,那么本页面中的内容已根据知识共享署名 4.0 许可获得了许可,并且代码示例已根据 Apache 2.0 许可获得了许可。有关详情,请参阅 Google 开发者网站政策。Java 是 Oracle 和/或其关联公司的注册商标。
最后更新时间 (UTC):2025-09-04。
[[["易于理解","easyToUnderstand","thumb-up"],["解决了我的问题","solvedMyProblem","thumb-up"],["其他","otherUp","thumb-up"]],[["很难理解","hardToUnderstand","thumb-down"],["信息或示例代码不正确","incorrectInformationOrSampleCode","thumb-down"],["没有我需要的信息/示例","missingTheInformationSamplesINeed","thumb-down"],["翻译问题","translationIssue","thumb-down"],["其他","otherDown","thumb-down"]],["最后更新时间 (UTC):2025-09-04。"],[[["\u003cp\u003eGoogle App Engine standard environment can serve static content like HTML, CSS, and images, which are files that won't be executed as JSPs or Servlets.\u003c/p\u003e\n"],["\u003cp\u003eStatic files must be placed within the app's \u003ccode\u003ewebapp\u003c/code\u003e directory, and their locations must be defined in the \u003ccode\u003eappengine-web.xml\u003c/code\u003e file using the \u003ccode\u003e<static-files>\u003c/code\u003e element with \u003ccode\u003e<include path>\u003c/code\u003e directives.\u003c/p\u003e\n"],["\u003cp\u003eTo ensure secure serving of static content, use HTTPS by adding a \u003ccode\u003e<security-constraint>\u003c/code\u003e element with \u003ccode\u003e<transport-guarantee>CONFIDENTIAL</transport-guarantee>\u003c/code\u003e to the \u003ccode\u003eweb.xml\u003c/code\u003e file, which redirects all requests to HTTPS.\u003c/p\u003e\n"],["\u003cp\u003eDeploying the application to App Engine can be done using Maven with the command \u003ccode\u003emvn package appengine:deploy -Dapp.deploy.projectId=PROJECT_ID\u003c/code\u003e, replacing \u003ccode\u003ePROJECT_ID\u003c/code\u003e with your Google Cloud project ID.\u003c/p\u003e\n"],["\u003cp\u003eAfter deploying with Maven, the deployed app can be viewed by using the \u003ccode\u003egcloud app browse\u003c/code\u003e command to open a web browser automatically.\u003c/p\u003e\n"]]],[],null,["# Getting Started: Serving Static Content\n\nLearn how to securely serve static content such as HTML files, CSS, and images\nfrom Google App Engine standard environment.\n\nBefore you begin\n----------------\n\n1. [Configure your development environment and create your App Engine project](/appengine/docs/legacy/standard/java/building-app/environment-setup).\n\n2. If you are using a custom domain name for your website, follow the\n instructions for adding a\n [Custom Domain](/appengine/docs/legacy/standard/java/mapping-custom-domains)\n to your project.\n\nServing a web page\n------------------\n\nApp Engine can serve static content such as HTML pages and media such as images.\nStatic content is anything that will not be executed as JSPs or Servlets.\n\nThe following example is a basic HTML page that shows a welcome message. \n\n \u003c!DOCTYPE html\u003e\n \u003chtml\u003e\n \u003chead\u003e\n \u003ctitle\u003eThe App Engine Blog\u003c/title\u003e\n \u003c/head\u003e\n \u003cbody\u003e\n \u003ch1\u003eWelcome to the App Engine Blog\u003c/h1\u003e\n \u003cp\u003eThis is being served by App Engine!\u003c/p\u003e\n \u003c/body\u003e\n \u003c/html\u003e\n\nWhere to put your static files\n------------------------------\n\nYou must put your static served files within your app's `webapp` directory. You\ncan use folders, but remember that all file paths and URIs are relative to the\n`webapp` directory.\n\nAfter you choose a location for the static files, you must define their location\nin the `appengine-web.xml` file, using the\n[`\u003cstatic-files\u003e`](/appengine/docs/legacy/standard/java/config/appref#static_files)\nelement.\n\nIn the example below, a basic `appengine-web.xml` configuration treats all HTML\nfiles in the `webapp` directory as static files. \n\n \u003cappengine-web-app xmlns=\"http://appengine.google.com/ns/1.0\"\u003e\n \u003cthreadsafe\u003etrue\u003c/threadsafe\u003e\n \u003cruntime\u003ejava8\u003c/runtime\u003e\n \u003cstatic-files\u003e\n \u003cinclude path=\"/**.html\" \u003e\n \u003c/include\u003e\n \u003c/static-files\u003e\n \u003c/appengine-web-app\u003e\n\nYou can have multiple `\u003cinclude path\u003e` elements that reference different\ndirectories and different filetypes. Expanding on the previous example: \n\n \u003cstatic-files\u003e\n \u003cinclude path=\"/**.html\" \u003e\n \u003c/include\u003e\n \u003cinclude path=\"/images/**.jpg\" \u003e\n \u003c/include\u003e\n \u003c/static-files\u003e\n\nNow all files with the `.jpg` extension in the `webapp/images/` directory will\nbe treated as static files.\n\nIn the example above, if we wanted to display `logo.jpg` from the\n`webapp/images` directory, the `\u003cimg\u003e` tag would have the source URI\n`\u003cimg src=\"/images/logo.jpg\"\u003e`.\n\nForcing HTTPS for all static content\n------------------------------------\n\nAlthough App Engine supports serving content using either HTTP or HTTPS,\nyou should use HTTPS. In order to set up\n[secure URLs](/appengine/docs/legacy/standard/java/config/webxml#Secure_URLs),\nyou must add a `\u003csecurity-constraint\u003e` element to your project's\n`web.xml`. A sample `\u003csecurity-constraint\u003e` is shown here: \n\n \u003csecurity-constraint\u003e\n \u003cweb-resource-collection\u003e\n \u003cweb-resource-name\u003eblog\u003c/web-resource-name\u003e\n \u003curl-pattern\u003e/*\u003c/url-pattern\u003e\n \u003c/web-resource-collection\u003e\n \u003cuser-data-constraint\u003e\n \u003ctransport-guarantee\u003eCONFIDENTIAL\u003c/transport-guarantee\u003e\n \u003c/user-data-constraint\u003e\n \u003c/security-constraint\u003e\n\nBy using `\u003ctransport-guarantee\u003eCONFIDENTIAL\u003c/transport-guarantee\u003e`, all requests\nare automatically redirected to the HTTPS URI of the static content.\n\nDeploying to App Engine\n-----------------------\n\nYou can deploy your app to App Engine using Maven.\n\nGo to the root directory of your project and type: \n\n```\nmvn package appengine:deploy -Dapp.deploy.projectId=PROJECT_ID\n```\n\nReplace \u003cvar translate=\"no\"\u003ePROJECT_ID\u003c/var\u003e with the ID of your Google Cloud project. If\nyour `pom.xml` file already\n[specifies your\nproject ID](/appengine/docs/legacy/standard/java/maven-reference#appenginedeploy), you don't need to include the `-Dapp.deploy.projectId` property in the\ncommand you run.\n\nAfter Maven deploys your app, open a web browser tab automatically\nat your new app by typing: \n\n gcloud app browse\n\nWhat's next\n-----------\n\nStatic files can be used to serve images, Cascading Style Sheets and static\nHTML content through App Engine. To extend your knowledge, you might want to\nlook at [handling user data through HTML forms](/appengine/docs/legacy/standard/java/building-app/handling-form-data)."]]