servletcontext.getRealPath("/") 是什么意思,我应该什么时候使用它 [英] What does servletcontext.getRealPath("/") mean and when should I use it
问题描述
在以下代码段中:
ServletContext context = request.getServletContext();
String path = context.getRealPath("/");
getRealPath()
方法中的 /
代表什么?我应该什么时候使用它?
What does /
in the method getRealPath()
represent? When should I use it?
推荐答案
简介
ServletContext#getRealPath()
旨在将 Web 内容路径(服务器磁盘文件系统上扩展的 WAR 文件夹结构中的路径)转换为绝对磁盘文件系统路径.
Introduction
The ServletContext#getRealPath()
is intented to convert a web content path (the path in the expanded WAR folder structure on the server's disk file system) to an absolute disk file system path.
"/"
代表 Web 内容根.IE.它代表 web
文件夹,如下面的项目结构所示:
The "/"
represents the web content root. I.e. it represents the web
folder as in the below project structure:
YourWebProject
|-- src
| :
|
|-- web
| |-- META-INF
| | `-- MANIFEST.MF
| |-- WEB-INF
| | `-- web.xml
| |-- index.jsp
| `-- login.jsp
:
因此,将 "/"
传递给 getRealPath()
将返回 /web
文件夹的绝对磁盘文件系统路径项目的扩展 WAR 文件.像 /path/to/server/work/folder/some.war/
之类的东西,您应该能够在 File
或 FileInputStream
中进一步使用.
So, passing the "/"
to getRealPath()
would return you the absolute disk file system path of the /web
folder of the expanded WAR file of the project. Something like /path/to/server/work/folder/some.war/
which you should be able to further use in File
or FileInputStream
.
请注意,大多数初学者似乎没有看到/意识到您实际上可以将整个 Web 内容路径传递给它并且他们经常使用
Note that most starters don't seem to see/realize that you can actually pass the whole web content path to it and that they often use
String absolutePathToIndexJSP = servletContext.getRealPath("/") + "index.jsp"; // Wrong!
甚至
String absolutePathToIndexJSP = servletContext.getRealPath("") + "index.jsp"; // Wronger!
代替
String absolutePathToIndexJSP = servletContext.getRealPath("/index.jsp"); // Right!
永远不要在那里写文件
另请注意,即使您可以使用 FileOutputStream
将新文件写入其中,但只要重新部署 WAR,所有更改(例如新文件或编辑过的文件)都会丢失;原因很简单,所有这些更改都不包含在原始 WAR 文件中.所以所有试图将上传的文件保存在那里的初学者都做错了.
Don't ever write files in there
Also note that even though you can write new files into it using FileOutputStream
, all changes (e.g. new files or edited files) will get lost whenever the WAR is redeployed; with the simple reason that all those changes are not contained in the original WAR file. So all starters who are attempting to save uploaded files in there are doing it wrong.
此外,当服务器未配置为将 WAR 文件扩展到磁盘文件系统时,getRealPath()
将始终返回 null
或完全意外的路径,但是而是进入例如内存作为虚拟文件系统.
Moreover, getRealPath()
will always return null
or a completely unexpected path when the server isn't configured to expand the WAR file into the disk file system, but instead into e.g. memory as a virtual file system.
小心使用getRealPath()
.实际上没有合理的现实世界用例.根据我 20 年的 Java EE 经验,总是有另一种比 getRealPath()
更好、更便携的方法.
Use getRealPath()
carefully. There are actually no sensible real world use cases for it. Based on my 20 years of Java EE experience, there has always been another way which is much better and more portable than getRealPath()
.
如果您真正需要的是获取 Web 资源的 InputStream
,最好使用 ServletContext#getResourceAsStream()
代替,无论 WAR 如何被扩展.因此,例如,如果您想要 index.jsp
的 InputStream
,请执行 not 执行:
If all you actually need is to get an InputStream
of the web resource, better use ServletContext#getResourceAsStream()
instead, this will work regardless of the way how the WAR is expanded. So, if you for example want an InputStream
of index.jsp
, then do not do:
InputStream input = new FileInputStream(servletContext.getRealPath("/index.jsp")); // Wrong!
而是这样做:
InputStream input = servletContext.getResourceAsStream("/index.jsp"); // Right!
或者,如果您打算获取所有可用 Web 资源路径的列表,请使用 ServletContext#getResourcePaths()
代替.
Or if you intend to obtain a list of all available web resource paths, use ServletContext#getResourcePaths()
instead.
Set<String> resourcePaths = servletContext.getResourcePaths("/");
您可以通过 ServletContext#getResource()
.当资源不存在时,这将返回 null
.
You can obtain an individual resource as URL
via ServletContext#getResource()
. This will return null
when the resource does not exist.
URL resource = servletContext.getResource(path);
或者,如果您打算保存上传的文件或创建临时文件,请参阅下面的另请参阅"链接.
Or if you intend to save an uploaded file, or create a temporary file, then see the below "See also" links.
这篇关于servletcontext.getRealPath("/") 是什么意思,我应该什么时候使用它的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!