导航:首页 > 编程语言 > java获取web目录

java获取web目录

发布时间:2023-08-28 12:31:34

1. 怎么在一个java程序里获得当前web应用的路

在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.war

2: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/classes
this.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/classes

3:Thread.currentThread().getContextClassLoader().getResource(".").getPath()
Thread.currentThread().getContextClassLoader().getResource("").getPath() 得到的是 ClassPath的绝对URI路径。。
如:/D:/myProjects/hp/WebRoot/WEB-INF/classes
Thread.currentThread().getContextClassLoader().getResource("/").getPath() X 不 能运行

最后
在Web应用程序中,我们一般通过ServletContext.getRealPath("/")方法得到Web应用程序的根目录的绝对路径。
还有request.getContextPath(); 在Weblogic中要用request.getServletContext().getContextPath();但如果打包成war部署到Weblogic服务器,项目内部并没有文件结构的概念,用这种方式是始终得到null,获取不到路径,目前还没有找到具体的解决方案。

2. Java web项目,在.java程序中如何获取webapp路径

String t=Thread.currentThread().getContextClassLoader().getResource("").getPath();
int num=t.indexOf(".metadata");
String path=t.substring(1,num).replace('/', '\\')+"项目名\\WebContent\\文件";
复制,亲测有效。

3. javaweb 普通工具类 如何在web启动初始化时获取web根目录,项目中只有spring框架

使用Spring,就可以用一种比较优雅的方式来获取了。
在web.xml中的<web-app>节点内加入:

<context-param>
<param-name>webAppRootKey</param-name>
<param-value>tansungWeb.root</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.util.WebAppRootListener</listener-class>
</listener>

然后在普通的Java类中(不是action中),就可以通过System.getProperty("tansungWeb.root")获取了web根目录了。
然后再拼凑路径的时候,最好不要直接使用/或者\,最好使用File.separatorChar

4. java 如何获取webcontent的路径 不是tomcat服务器的路径

用servlet获取
1、获取项目的绝对路径
request.getSession().getServletContext().getRealPath("")
结果:
E:\java_web\workspace\.metadata\.plugins\org.eclipse.wst.server.core\tmp0\wtpwebapps\myWebsite
2、获取浏览器地址
request.getRequestURL()
结果:
http://localhost:8080/myWebsite/QuestionServlet
3、获取当前文件的绝对路径
request.getSession().getServletContext().getRealPath(request.getRequestURI())
结果:
E:\java_web\workspace\.metadata\.plugins\org.eclipse.wst.server.core\tmp0\wtpwebapps\myWebsite\myWebsite\QuestionServlet

5. java怎么获取web资源文件

1、一般工程中使用I/O类指定文件的绝对路径读取
FileInputStream
fis
=
new
FileInputStream("src/main/resources/zsm.properties");
ppt.load(fis);
String
memAddr1
=
ppt.getProperty("memAddr1");
2、Web工程中可以使用ServletContext或ClassLoader来读取
2.1、通过ServletContext来读取资源文件,文件路径是相对于web项目(如/JspServletFeature)根路径而言的。
2.2、通过ClassLoader来读取,文件路径是相对于类目录而言的(maven工程中一般为/target/classes)
示例如下
(1)文件位置
放在src目录(或其子目录)下是相对于项目根目录如JspServletFeature的路径
放在JavaResources下是相对于类目录即classes的目录
(2)代码
//
使用servletContext读取资源文件,相对于web项目的根路径(即JspServletFeature)
out.println("\n使用servletContext读取资源文件,相对于web项目的根路径(即JspServletFeature):");
readFileByServletContext(response,
"FileReadFile1.properties");
readFileByServletContext(response,
"/FileReadFile1.properties");
readFileByServletContext(response,
"WEB-INF/classes/FileReadFile2.properties");
readFileByServletContext(response,
"/WEB-INF/classes/FileReadFile2.properties");

6. java 怎么获取web根目录

以工程名为TEST为例: x0dx0ax0dx0a(1)得到包含工程名的当前页面全路径:request.getRequestURI() x0dx0a结果:/TEST/test.jsp x0dx0a(2)得到工程名:request.getContextPath() x0dx0a结果:/TEST x0dx0a(3)得到当前页面所在目录下全名称:request.getServletPath() x0dx0a结果:如果页面在jsp目录下 /TEST/jsp/test.jsp x0dx0a(4)得到页面所在服务器的全路径:application.getRealPath("页面.jsp") x0dx0a结果:D:/resin/webapps/TEST/test.jsp x0dx0a(5)得到页面所在服务器的绝对路径:absPath=new java.io.File(application.getRealPath(request.getRequestURI())).getParent();x0dx0a结果:D:/resin/webapps/TEST x0dx0ax0dx0a2.在类中取得路径: x0dx0ax0dx0a(1)类的绝对路径:String u=Class.class.getClass().getResource("/").getPath() x0dx0a结果:/D:/TEST/WebRoot/WEB-INF/classes/pack/ x0dx0a(2)得到工程的路径:System.getProperty("user.dir") x0dx0a结果:D:/TEST x0dx0ax0dx0a3.在Servlet中取得路径: x0dx0ax0dx0a(1)得到工程目录:request.getSession().getServletContext().getRealPath("") 参数可具体到包名。 x0dx0a结果:E:/Tomcat/webapps/TEST x0dx0a(2)得到IE地址栏地址:request.getRequestURL() x0dx0a结果:http://localhost:8080/TEST/test x0dx0a(3)得到相对地址:request.getRequestURI() x0dx0a结果:/TEST/test

7. java如何获得linux下web路径

java获取根路径有两种方式:
1),在servlet可以用一下方法取得:
request.getRealPath(“/”) 例如:filepach = request.getRealPath(“/”) ”//upload//”;
2),不从jsp,或servlet中获取,只从普通java类中获取:
String path =
getClass().getProtectionDomain().getCodeSource().getLocation().getPath();
SAXReader() saxReader = new SAXReader();
if(path.indexOf(“WEB-INF”)>0){
path = path.substring(0,path.indexOf(“/WEB-INF/classes”) 16);
// ‘/WEB-INF/classes’为16位
document = saxReader.read(path filename);
}else{
document = saxReader.read(getClass().getResourceAsStream(filename));
}
weblogic tomcat 下都有效
String path =
getClass().getProtectionDomain().getCodeSource().getLocation().getPath();
<!--EndFragment-->

8. jsp 在java程序中获取 web-inf目录

你需要一个servlet或者action,servlet举例:
建一个servlet,在doPost()中通过,
request.getSession().getServletContext().getRealPath("/WEB-INF");

阅读全文

与java获取web目录相关的资料

热点内容
幼儿园设计pdf 浏览:645
干接点输入单片机 浏览:541
亚马逊云服务器查看 浏览:163
如何用免费云服务器 浏览:610
php的输出命令 浏览:264
在家怎么制作解压小玩具 浏览:99
javascript源码辅助阅读 浏览:384
pythonui开发工具 浏览:595
adr指标源码 浏览:217
程序员转架构管理 浏览:959
企业服务器为什么不能被拷贝 浏览:119
用c编程实现txt搜索 浏览:854
JAVA小数随机数 浏览:337
app加拿大pc怎么操控的 浏览:698
光影app苹果怎么下载不了 浏览:971
php会员注册代码 浏览:511
csgo如何用128tick服务器 浏览:571
百度网度怎么解压 浏览:946
windowsopencv源码 浏览:946
origin平滑算法 浏览:875