servletcontext.getRealPath(" /")是什么意思,什么时候应该使用它 [英] What does servletcontext.getRealPath("/") mean and when should I use it
问题描述
在以下代码段中:
ServletContext context = request.getServletContext();
String path = context.getRealPath("/");
方法<$中的 /
是什么c $ c> 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内容根目录。即它代表 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 $ c的绝对磁盘文件系统路径$ c>项目的扩展WAR文件的文件夹。像
/path/to/server/work/folder/some.war /
之类的东西你可以在文件$ c中进一步使用$ c>或
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";
而不是
String absolutePathToIndexJSP = servletContext.getRealPath("/index.jsp");
不要在那里写文件
另请注意,即使您可以使用 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.
此外, getRealPath()
将始终返回 null
当服务器未配置为将WAR文件扩展到磁盘文件系统时,或者转换为例如内存作为虚拟文件系统。
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()
。实际上没有合理的现实世界用例。如果您真正需要的是获得Web资源的 InputStream
,那么最好使用 ServletContext #getResourceAsStream()
而不是这个无论WAR如何扩展,它都能正常工作。所以,如果您想要一个输入流
index.jsp
,那么不执行:
Use getRealPath()
carefully. There are actually no sensible real world use cases for it. 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!
或者如果您打算获取所有可用网络资源路径的列表,请使用 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("/");
您可以获取单个资源 URL
通过 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.
- getResourceAsStream() vs FileInputStream
- Recommended way to save uploaded files in a servlet application
- Simple ways to keep data on redeployment of Java EE 7 web application
这篇关于servletcontext.getRealPath(" /")是什么意思,什么时候应该使用它的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!