java ServletContext getRealPath 方法 - 什么是虚拟路径?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12523848/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 09:14:06  来源:igfitidea点击:

ServletContext getRealPath method - what is a virtual path?

javaservletsservlet-container

提问by KyelJmD

I am reading the documentation for the getRealPath(String s)and I became confused when I read this statement

我正在阅读 的文档,getRealPath(String s)当我阅读此声明时,我感到困惑

Returns a String containing the real path for a given virtual path. For example, the path /index.htmlreturns the absolute file path on the server's filesystem would be served by a request for http://host/contextPath/index.html, where contextPath is the context path of this ServletContext..

返回一个包含给定虚拟路径的真实路径的字符串。例如,该路径/index.html返回服务器文件系统上的绝对文件路径,该路径将由对 的请求提供服务http://host/contextPath/index.html,其中 contextPath 是此 ServletContext 的上下文路径。

What is a virtual path? let's say I am inside in my deployment environment and when I say getRealPath("index.html") does this usually points to the WEB-INF directory? or does getRealPath() starts reading the root directory(inside the folder name of the web app)?

什么是虚拟路径?假设我在我的部署环境中,当我说 getRealPath("index.html") 这通常指向 WEB-INF 目录吗?还是 getRealPath() 开始读取根目录(在 Web 应用程序的文件夹名称内)?

采纳答案by home

ServletContext.getRealPath(String s)returns the real file system path. The input string is interpreted relative your Web Applications' Context Path.

ServletContext.getRealPath(String s)返回真实的文件系统路径。输入字符串相对于您的 Web 应用程序的上下文路径进行解释。

In e.g. eclipse this is typically the folder WebContentinside your Web Application project (it is possible to customize that). After building a WAR file out of the project, you'll realize that the WebContentfolder disappeared, so on the server the input string is interpreted relative to the WAR files' folder (or the .war library - this depends whether you explode the WAR during deployment or not).

例如,在 eclipse 中,这通常是WebContent您的 Web 应用程序项目中的文件夹(可以自定义)。在从项目中构建 WAR 文件后,您将意识到该WebContent文件夹消失了,因此在服务器上,输入字符串是相对于 WAR 文件的文件夹(或 .war 库 - 这取决于您是否在部署与否)。

A note on security

安全注意事项

This method should (not must) only be used in case you want to access a file on the serverside. Typical scenario is parsing a configuration file during startup. Just keep in mind to never let the caller of your application know the real file system path of a given resource.

此方法应该(不是必须)仅在您想访问服务器端的文件时使用。典型的场景是在启动时解析配置文件。请记住永远不要让应用程序的调用者知道给定资源的真实文件系统路径。

回答by Jops

The paths of resources you access from within the web container are all virtual paths. Their virtual root is the base folder of the web application. But in the actual operating systemwhere they reside, they will be in a "concrete" location for sure. getRealPathgives you that path.

您从 Web 容器内访问的资源路径都是虚拟路径。它们的虚拟根目录是 Web 应用程序的基本文件夹。但是在它们所在的实际操作系统中,它们肯定会处于“具体”位置。getRealPath给你那条路。

Say you have a jsp named index.jsp:

假设您有一个名为index.jsp的 jsp :

enter image description here

在此处输入图片说明

enter image description here

在此处输入图片说明

Take note that whenever you use this, you're creating cracks on the portability of your web application.

请注意,无论何时使用它,都会对 Web 应用程序的可移植性造成破坏。