知识点
相关文章
更多最近更新
更多JAVA获取WEBROOT物理路径的方法
2019-03-26 06:50|来源: 网路
在WEB-INF/web.xml中配置context-param,name为webAppRootKey,value为webapp.root
<web-app version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"> <context-param> <param-name>webAppRootKey</param-name> <param-value>webapp.root</param-value> </context-param> <listener> <listener-class>com.656463.ApplicationListener</listener-class> </listener> </web-app>
然后通过Listener,或者Filter,或者Servlet获取到物理路径
String webAppRootKey = getServletContext().getRealPath("/");
对webAppRootKey对应的webapp.root赋值,并写入到System Properties系统属性中,java程序中可通过System.getProperty("webapp.root")来获得WebRoot的物理路径
import javax.servlet.ServletContextEvent; import org.springframework.web.context.ContextLoaderListener; public class ApplicationListener extends ContextLoaderListener { public void contextDestroyed(ServletContextEvent sce) { // TODO Auto-generated method stub } public void contextInitialized(ServletContextEvent sce) { // TODO Auto-generated method stub String webAppRootKey = sce.getServletContext().getRealPath("/"); System.setProperty("webapp.root" , webAppRootKey); String path =System.getProperty("webapp.root"); System.out.println("path="+path); } }
整理于网络
相关问答
更多-
在Application_Start中获取当前的应用程序物理路径(Get current application physical path within Application_Start)[2022-03-04]
protected void Application_Start(object sender, EventArgs e) { string path = Server.MapPath("/"); //or string path2 = Server.MapPath("~"); //depends on your application needs } protected void Application_Start(object sender, Event ... -
Governance registry使用数据库(默认为h2)来存储它的配置。 因此,您无法在文件系统上找到添加到gov中的文件(除非您使用checkin-client.bat导出注册表)。 解决方案是请求wsdl而不是尝试读取它的文件。 Governance registry uses a database (h2 by default) to store it's configuration. Therefore, you can't find on the filesystem, the file a ...
-
Beanstalk中的物理路径(Physical Path in Beanstalk)[2021-08-17]
您可以在主项目中使用.ebextensions文件,例如,创建目录并更改其访问权限。 从您的问题中不清楚如何安装插件(例如,它是在安装Web应用程序后加载的服务还是Web应用程序的一部分)。 在.ebextensions文件中执行命令,例如: 如何在Windows中使用命令行为用户授予权限? 您将在中找到容器定制的介绍 http://docs.aws.amazon.com/elasticbeanstalk/latest/dg/customize-containers-windows-ec2.html 注意文 ... -
使用魔法常量__FILE__ Since this is the first hit on Google and the accepted answer is incomplete according to the spirit of the question, here is the complete answer. __FILE__ is a magical constant like John Conde said, but it only gives you the physical path fo ...
-
获得物理路径(Getting the physical path)[2021-06-28]
GetFiles()的文档说: 允许path参数指定相对或绝对路径信息。 相对路径信息被解释为相对于当前工作目录 因此,在尝试使用相对路径(例如E:\ IWP)之前,您需要确保正确设置当前工作目录: GetCurrentDirectory SetCurrentDirectory Hey all, i got the answer to my question. I just had to write string[] dirImages = HttpContext.Current.Server.MapPat ... -
尝试使用这个: