「java物理路径」java读取路径

博主:adminadmin 2023-01-09 04:48:09 872

本篇文章给大家谈谈java物理路径,以及java读取路径对应的知识点,希望对各位有所帮助,不要忘了收藏本站喔。

本文目录一览:

java 怎么获取服务器webroot的路径

使用JAVA后台代码取得WEBROOT物理路径,可以有如下两种方式:

1、使用JSP Servlet取得WEB根路径可以用request.getContextPath(),相对路径request.getSession().getServletContext().getRealPath("/"),它们可以使用我们很容易取得根路径。

2、如果使用了spring, 在WEB-INF/web.xml中,创建一个webAppRootKey的param,指定一个值(默认为webapp.root)作为键值,然后通过Listener,或者Filter,或者Servlet执行String webAppRootKey = getServletContext().getRealPath("/"); 并将webAppRootKey对应的webapp.root分别作为Key,Value写到System Properties系统属性中。之后在程序中通过System.getProperty("webapp.root")来获得WebRoot的物理路径。

具体示例代码如下:

web.xml

?xml version="1.0" encoding="UTF-8"?

web-app version="2.4"

xmlns=""

xmlns:xsi=""

xsi:schemaLocation="

"

context-param

param-namewebAppRootKey/param-name

param-valuecsc2.root/param-value

/context-param

listener

listener-classtest.ApplicationListener/listener-class

/listener

/web-app

ApplicationListener.java

package test;

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("csc2.root" , webAppRootKey);

String path =System.getProperty("csc2.root");

System.out.println("sssss:::"+path);

}

}

test.java

public class test {

public void remve(){

String path =System.getProperty("csc2.root");

System.out.println("result::::::::"+path);

}

}

index.jsp

%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%

%@ page import="java.util.*" %

%@ page import="test.test" %

%

String path = request.getContextPath();

String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";

%

%

test t = new test();

t.remve();

%

html

/html

部署程序发布 启动TOMCAT 运行index.jsp 就可以调用JAVA中全局设置的物理路径了(说明这里的JSP 只是调用了TEST.JAVA 的remove方法,不做其他使用。原理解释,TOMCAT启动和读取WEB.XML 监听方式加载SPRING ApplicationListener继承SPRING ContextLoaderListener加载SPRING顺便吧全局路径赋值给csc2.root 描述,这样之后JAVA 代码中就可以使用System.getProperty("csc2.root")调用全路路径了。

java 项目如何获取项目所在的物理根路径

在java中获得文件的路径在我们做上传文件操作时是不可避免的。web上运行1:this.getClass().getClassLoader().getResource("/").getPath();this.getClass().getClassLoader().getResource("").getPath();得到的是ClassPath的绝对URI路径。如:/D:/jboss-4.2.2.GA/server/default/deploy/hp.war/WEB-INF/classes/System.getProperty("user.dir");this.getClass().getClassLoader().getResource(".").getPath();得到的是项目的绝对路径。如:/D:/jboss-4.2.2.GA/server/default/deploy/hp.war2:this.getClass().getResource("/").getPath();this.getClass().getResource("").getPath();得到的是当前类文件的URI目录。如:/D:/jboss-4.2.2.GA/server/default/deploy/hp.war/WEB-INF/classes/com/jebel/helper/this.getClass().getResource(".").getPath();X不能运行3:Thread.currentThread().getContextClassLoader().getResource("/").getPath()Thread.currentThread().getContextClassLoader().getResource("").getPath()得到的是ClassPath的绝对URI路径。如:/D:/jboss-4.2.2.GA/server/default/deploy/hp.war/WEB-INF/classes/Thread.currentThread().getContextClassLoader().getResource(".").getPath()得到的是项目的绝对路径。如:/D:/jboss-4.2.2.GA/server/default/deploy/hp.war在本地运行中1:this.getClass().getClassLoader().getResource("").getPath();this.getClass().getClassLoader().getResource(".").getPath();得到的是ClassPath的绝对URI路径。如:/D:/myProjects/hp/WebRoot/WEB-INF/classesthis.getClass().getClassLoader().getResource(".").getPath();X不能运行2:this.getClass().getResource("").getPath();this.getClass().getResource(".").getPath();得到的是当前类文件的URI目录。如:/D:/myProjects/hp/WebRoot/WEB-INF/classes/com/jebel/helper//D:/myProjects/hp/WebRoot/WEB-INF/classes/得到的是ClassPath的绝对URI路径。如:/D:/myProjects/hp/WebRoot/WEB-INF/classes3:Thread.currentThread().getContextClassLoader().getResource(".").getPath()Thread.currentThread().getContextClassLoader().getResource("").getPath()得到的是ClassPath的绝对URI路径。。如:/D:/myProjects/hp/WebRoot/WEB-INF/classesThread.currentThread().getContextClassLoader().getResource("/").getPath()X不能运行最后在Web应用程序中,我们一般通过ServletContext.getRealPath("/")方法得到Web应用程序的根目录的绝对路径。还有request.getContextPath();在Weblogic中要用request.getServletContext().getContextPath();但如果打包成war部署到Weblogic服务器,项目内部并没有文件结构的概念,用这种方式是始终得到null,获取不到路径,目前还没有找到具体的解决方案。

java绝对路径和相对路径的区别

1.基本概念的理解

绝对路径:绝对路径就是你的主页上的文件或目录在硬盘上真正的路径,(URL和物理路径)例如:

C:\xyz\test.txt 代表了test.txt文件的绝对路径。也代表了一个

URL绝对路径。

相对路径:相对与某个基准目录的路径。包含Web的相对路径(HTML中的相对目录),例如:在

Servlet中,"/"代表Web应用的根目录。和物理路径的相对表示,例如:"./" 代表当前目录,

"../"代表上级目录。这种类似的表示,也是属于相对路径。

如何在 Java 代码中获得 WebRoot 的物理路径

String nowpath = System.getProperty("user.dir");

String path = nowpath.replace("bin", "webapps");

你试一下 应该可以

java 相对路径与绝对路径,物理路径与虚拟路径的区别

相对路径 相对当前目录的路径、或相对某个目录的路径

绝对路径 是从最高层开始的路径,如windows下 C:\a\b\c\d\e

物理路径 实际磁盘中的路径,可以是相对路径、也可以是绝对路径

虚拟路径 是服务映射出来的路径,如/myweb

java物理路径的介绍就聊到这里吧,感谢你花时间阅读本站内容,更多关于java读取路径、java物理路径的信息别忘了在本站进行查找喔。