servletcontext.getRealPath(“/”)是什么意思,什么时候应该使用它

时间:2022-09-11 10:36:00

In the following snippet:

在以下代码段中:

ServletContext context = request.getServletContext();
String path = context.getRealPath("/");

What does / in the method getRealPath() represent? When should I use it?

getRealPath()方法中的/表示什么?我应该什么时候使用它?

3 个解决方案

#1


86  

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.

ServletContext#getRealPath()用于将Web内容路径(服务器磁盘文件系统上扩展的WAR文件夹结构中的路径)转换为绝对磁盘文件系统路径。

The "/" represents the web content root. I.e. it represents the web folder as in the below project structure:

“/”表示Web内容根。即它表示以下项目结构中的Web文件夹:

YourWebProject
 |-- src
 |    :
 |
 |-- web
 |    |-- META-INF
 |    |    `-- MANIFEST.MF
 |    |-- WEB-INF
 |    |    `-- web.xml
 |    |-- index.jsp
 |    `-- login.jsp
 :    

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.

因此,将“/”传递给getRealPath()会返回项目展开的WAR文件的/ web文件夹的绝对磁盘文件系统路径。像/path/to/server/work/folder/some.war/这样你应该能够在File或FileInputStream中进一步使用。

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

请注意,大多数初学者似乎没有看到/意识到您实际上可以将整个Web内容路径传递给它并且他们经常使用

String absolutePathToIndexJSP = servletContext.getRealPath("/") + "index.jsp";

instead of

代替

String absolutePathToIndexJSP = servletContext.getRealPath("/index.jsp");

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.

另请注意,即使您可以使用FileOutputStream将新文件写入其中,只要重新部署WAR,所有更改(例如新文件或编辑过的文件)都将丢失;原因很简单,所有这些更改都不包含在原始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.

此外,当服务器未配置为将WAR文件扩展到磁盘文件系统时,getRealPath()将始终返回null或完全意外的路径,而是将其转换为例如内存作为虚拟文件系统。

getRealPath() is unportable; you'd better never use it

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:

仔细使用getRealPath()。实际上没有合理的现实世界用例。如果您真正需要的只是获取Web资源的InputStream,那么最好使用ServletContext#getResourceAsStream(),无论WAR如何扩展,这都将起作用。所以,如果你想要一个index.jsp的InputStream,那么不要这样做:

InputStream input = new FileInputStream(servletContext.getRealPath("/index.jsp")); // Wrong!

But instead do:

而是做:

InputStream input = servletContext.getResourceAsStream("/index.jsp"); // Right!

Or if you intend to obtain a list of all available web resource paths, use ServletContext#getResourcePaths() instead.

或者,如果您打算获取所有可用Web资源路径的列表,请改用ServletContext#getResourcePaths()。

Set<String> resourcePaths = servletContext.getResourcePaths("/");

You can obtain an individual resource as URL via ServletContext#getResource(). This will return null when the resource does not exist.

您可以通过ServletContext#getResource()获取单个资源作为URL。当资源不存在时,这将返回null。

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.

或者,如果您打算保存上传的文件或创建临时文件,请参阅下面的“另请参见”链接。

See also:

#2


2  

A web application's context path is the directory that contains the web application's WEB-INF directory. It can be thought of as the 'home' of the web app. Often, when writing web applications, it can be important to get the actual location of this directory in the file system, since this allows you to do things such as read from files or write to files.

Web应用程序的上下文路径是包含Web应用程序的WEB-INF目录的目录。它可以被认为是网络应用程序的“主页”。通常,在编写Web应用程序时,在文件系统中获取此目录的实际位置非常重要,因为这样可以执行诸如从文件读取或写入文件之类的操作。

This location can be obtained via the ServletContext object's getRealPath() method. This method can be passed a String parameter set to File.separator to get the path using the operating system's file separator ("/" for UNIX, "\" for Windows).

可以通过ServletContext对象的getRealPath()方法获取此位置。可以将此方法传递给File参数设置为File.separator,以使用操作系统的文件分隔符获取路径(对于UNIX为“/”,对于Windows为“\”)。

#3


1  

There is also a change between Java 7 and Java 8. Admittedly it involves the a deprecated call, but I hae aWe had to add a "/" to get our program working! Here is the link discussing it Why does servletContext.getRealPath returns null on tomcat 8?

Java 7和Java 8之间也有一个变化。不可否认它涉及一个不赞成的电话,但是我需要添加一个“/”来让我们的程序正常工作!这是讨论它的链接为什么servletContext.getRealPath在tomcat 8上返回null?

#1


86  

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.

ServletContext#getRealPath()用于将Web内容路径(服务器磁盘文件系统上扩展的WAR文件夹结构中的路径)转换为绝对磁盘文件系统路径。

The "/" represents the web content root. I.e. it represents the web folder as in the below project structure:

“/”表示Web内容根。即它表示以下项目结构中的Web文件夹:

YourWebProject
 |-- src
 |    :
 |
 |-- web
 |    |-- META-INF
 |    |    `-- MANIFEST.MF
 |    |-- WEB-INF
 |    |    `-- web.xml
 |    |-- index.jsp
 |    `-- login.jsp
 :    

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.

因此,将“/”传递给getRealPath()会返回项目展开的WAR文件的/ web文件夹的绝对磁盘文件系统路径。像/path/to/server/work/folder/some.war/这样你应该能够在File或FileInputStream中进一步使用。

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

请注意,大多数初学者似乎没有看到/意识到您实际上可以将整个Web内容路径传递给它并且他们经常使用

String absolutePathToIndexJSP = servletContext.getRealPath("/") + "index.jsp";

instead of

代替

String absolutePathToIndexJSP = servletContext.getRealPath("/index.jsp");

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.

另请注意,即使您可以使用FileOutputStream将新文件写入其中,只要重新部署WAR,所有更改(例如新文件或编辑过的文件)都将丢失;原因很简单,所有这些更改都不包含在原始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.

此外,当服务器未配置为将WAR文件扩展到磁盘文件系统时,getRealPath()将始终返回null或完全意外的路径,而是将其转换为例如内存作为虚拟文件系统。

getRealPath() is unportable; you'd better never use it

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:

仔细使用getRealPath()。实际上没有合理的现实世界用例。如果您真正需要的只是获取Web资源的InputStream,那么最好使用ServletContext#getResourceAsStream(),无论WAR如何扩展,这都将起作用。所以,如果你想要一个index.jsp的InputStream,那么不要这样做:

InputStream input = new FileInputStream(servletContext.getRealPath("/index.jsp")); // Wrong!

But instead do:

而是做:

InputStream input = servletContext.getResourceAsStream("/index.jsp"); // Right!

Or if you intend to obtain a list of all available web resource paths, use ServletContext#getResourcePaths() instead.

或者,如果您打算获取所有可用Web资源路径的列表,请改用ServletContext#getResourcePaths()。

Set<String> resourcePaths = servletContext.getResourcePaths("/");

You can obtain an individual resource as URL via ServletContext#getResource(). This will return null when the resource does not exist.

您可以通过ServletContext#getResource()获取单个资源作为URL。当资源不存在时,这将返回null。

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.

或者,如果您打算保存上传的文件或创建临时文件,请参阅下面的“另请参见”链接。

See also:

#2


2  

A web application's context path is the directory that contains the web application's WEB-INF directory. It can be thought of as the 'home' of the web app. Often, when writing web applications, it can be important to get the actual location of this directory in the file system, since this allows you to do things such as read from files or write to files.

Web应用程序的上下文路径是包含Web应用程序的WEB-INF目录的目录。它可以被认为是网络应用程序的“主页”。通常,在编写Web应用程序时,在文件系统中获取此目录的实际位置非常重要,因为这样可以执行诸如从文件读取或写入文件之类的操作。

This location can be obtained via the ServletContext object's getRealPath() method. This method can be passed a String parameter set to File.separator to get the path using the operating system's file separator ("/" for UNIX, "\" for Windows).

可以通过ServletContext对象的getRealPath()方法获取此位置。可以将此方法传递给File参数设置为File.separator,以使用操作系统的文件分隔符获取路径(对于UNIX为“/”,对于Windows为“\”)。

#3


1  

There is also a change between Java 7 and Java 8. Admittedly it involves the a deprecated call, but I hae aWe had to add a "/" to get our program working! Here is the link discussing it Why does servletContext.getRealPath returns null on tomcat 8?

Java 7和Java 8之间也有一个变化。不可否认它涉及一个不赞成的电话,但是我需要添加一个“/”来让我们的程序正常工作!这是讨论它的链接为什么servletContext.getRealPath在tomcat 8上返回null?