简体   繁体   English

如何在Java中检索Web应用程序的根路径

[英]How to retrieve web application root path in Java

I am coding my Spring MVC based web application (which was deployed with a war file) and trying to get the value of 我正在编码基于Spring MVC的Web应用程序(已与war文件一起部署),并试图获取的价值。

String rootDir = request.getSession().getServletContext().getRealPath("/");

I am expecting to get 我期望得到

"C:\user\projects\MyApp"

but the actual value is 但实际值是

"C:\user\projects\.metadata\.plugins\org.eclipse.wst.server.core\tmp0\wtpwebapps\MyApp\"

I tried to change Source Folder Output Location in Eclipse, but after that all my jsp files, web.xml and folder \\WEB-INF are gone. 我试图在Eclipse中更改“ 源文件夹输出位置” ,但是之后我所有的jsp文件,web.xml和文件夹\\ WEB-INF都消失了。 How can I restore them? 我该如何还原它们?

The reason I am asking this is that I want my uploaded images to be saved in "MyApp\\webapp\\WEB-INF\\resources\\images\\" folder. 我之所以这样问,是因为我希望将上传的图像保存在“ MyApp \\ webapp \\ WEB-INF \\ resources \\ images \\”文件夹中。

Why are you expecting the path to be "C:\\user\\projects\\MyApp" ? 您为什么期望该路径为"C:\\user\\projects\\MyApp"

Your are running your application using Eclipse and Eclipse is deploying it to C:\\user\\projects\\.metadata\\.plugins\\org.eclipse.wst.server.core and not running it from project source folder that is why you get "C:\\user\\projects\\.metadata\\.plugins\\org.eclipse.wst.server.core\\tmp0\\wtpwebapps\\MyApp\\" from getRealPath() call. 您正在使用Eclipse运行应用程序,并且Eclipse将其部署到C:\\user\\projects\\.metadata\\.plugins\\org.eclipse.wst.server.core而不是从项目源文件夹运行它,这就是为什么您获得"C:\\user\\projects\\.metadata\\.plugins\\org.eclipse.wst.server.core\\tmp0\\wtpwebapps\\MyApp\\"从getRealPath()调用中"C:\\user\\projects\\.metadata\\.plugins\\org.eclipse.wst.server.core\\tmp0\\wtpwebapps\\MyApp\\"

From ServletContext#getRealPath() ServletContext#getRealPath()

Gets the real path corresponding to the given virtual path. 获取与给定虚拟路径相对应的真实路径。 For example, if path is equal to /index.html, this method will return the absolute file path on the server's filesystem to which a request of the form http://://index.html would be mapped, where corresponds to the context path of this ServletContext. 例如,如果path等于/index.html,则此方法将返回服务器文件系统上的绝对文件路径,该文件的格式为http://://index.html的请求将映射到该文件,该位置对应于此ServletContext的上下文路径。

The real path returned will be in a form appropriate to the computer and operating system on which the servlet container is running, including the proper path separators. 返回的实际路径将采用适合运行servlet容器的计算机和操作系统的形式,包括适当的路径分隔符。

Resources inside the /META-INF/resources directories of JAR files bundled in the application's /WEB-INF/lib directory must be considered only if the container has unpacked them from their containing JAR file, in which case the path to the unpacked location must be returned. 仅当容器从包含其的JAR文件中解压缩了应用程序的/ WEB-INF / lib目录中捆绑的JAR文件的/ META-INF / resources目录内的资源时,才应考虑该资源,在这种情况下,必须提供到解压缩位置的路径被退回。

This method returns null if the servlet container is unable to translate the given virtual path to a real path. 如果servlet容器无法将给定的虚拟路径转换为真实路径,则此方法返回null。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM