简体   繁体   English

我无法从Servlet(Java EE)获得真正的路径

[英]I can't get the real path from Servlet (Java EE)

When I would like to get a real path in Java EE, using this instructions: 当我想在Java EE中获得真正的路径时,请使用以下说明:

String rootPath = getServletConfig().getServletContext().getRealPath("/");
System.out.println(rootPath);

The result is: C:\\Users\\saad\\Desktop\\pds.metadata.plugins\\org.eclipse.wst.server.core\\tmp0\\wtpwebapps\\Metier\\ 结果是:C:\\ Users \\ saad \\ Desktop \\ pds.metadata.plugins \\ org.eclipse.wst.server.core \\ tmp0 \\ wtpwebapps \\ Metier \\

And normaly, I have to get this result: 而且,我必须得到这个结果:

C:\\Users\\saad\\Desktop\\pds\\Metier\\ C:\\ Users \\用户萨阿德\\桌面\\ PDS \\梅捷\\

Can anyone help me, please? 有人可以帮帮我吗?

This is an excellent example of why the concept of "filesystem path relative to the application" really has no meaning in a servlet. 这是为什么“文件系统路径相对于应用程序”的概念在servlet中没有意义的一个很好的例子。 When a servlet is deployed into a container, there is no guarantee that it resides at a real filesystem location. 将servlet部署到容器中时,无法保证它位于真实的文件系统位置。 The servlet container could choose to execute it directly from the .war file without expanding it, or it could be reading the application code from a URL that is not backed by a traditional file system at all. servlet容器可以选择直接从.war文件执行它而不扩展它,或者它可以从一个完全没有传统文件系统支持的URL读取应用程序代码。

In your case, the long path you got back is exactly where Eclipse deployed your application. 在您的情况下,您获得的长路径正是Eclipse部署应用程序的地方。 That path was 100% correct in that context , because if you look at the filesystem at that location you'll find a copy of your application, but obviously not what you were expecting. 在该上下文中 ,该路径是100%正确的,因为如果您查看该位置的文件系统,您将找到应用程序的副本,但显然不是您所期望的。

In general, it is not possible to portably refer to any file path relative to your application because the path may not exist. 通常,无法以可移植方式引用与应用程序相关的任何文件路径,因为该路径可能不存在。

Path to Tomcat dir (tomcat/bin): Tomcat dir(tomcat / bin)的路径:

File inputFile = new File("file.xml"); // tomcat_dir/bin/file.xml

Path to other Tomcat directories, "webapps" for example: 其他Tomcat目录的路径,例如“webapps”:

File inputFile = new File("../webapps/file.xml"); // tomcat_dir/webapps/file.xml

Good way to get rid of the absolute paths :) 摆脱绝对路径的好方法:)

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

相关问题 除了EAR和EJB之外,我从Java EE应用服务器获得了什么,而我没有像Tomcat那样进入servlet容器? - Besides EAR and EJB, what do I get from a Java EE app server that I don't get in a servlet container like Tomcat? Java EE-找不到GET方法 - Java EE - Can't find GET method 我如何从java servlet到jQuery获取json数据 - how can i get json data from java servlet to jquery 无法在java servlet中获取会话 - Can't get the session in java servlet 我如何在WatchService中获取真实路径 - How can I get the real path in WatchService 在Java EE项目中,删除类后,我无法找回它们吗? - In a Java EE project, after deleting my classes, I can't get them back? 如何获取Java Servlet或JSP中客户端下载位置的路径? - How can i get the path of client side download location in java servlet or jsp? 从哪里可以得到用Java EE开发的任何示例Web应用程序,以便向他们学习? - From where can I get any example web applications developed in Java EE in order to learn from them? 如何获取WEB-INF / class文件夹下的资源路径(java ee web动态项目) - How can I get path of resource under WEB-INF/class folder (java ee web dynamic project) 为什么我不能在Java ee项目中使用PDDOCUMENT PDFBOX? - Why i Can't use PDDOCUMENT PDFBOX in java ee project ?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM