资讯

精准传达 • 有效沟通

从品牌网站建设到网络营销策划,从策略到执行的一站式服务

java代码中项目路径 javaweb项目路径问题

通过java获取当前项目路径

getClass().getResource() 方法获得相对路径( 此方法在jar包中无效。返回的内容最后包含/)

创新互联专注于企业成都营销网站建设、网站重做改版、休宁县网站定制设计、自适应品牌网站建设、H5网站设计成都做商城网站、集团公司官网建设、外贸网站建设、高端网站制作、响应式网页设计等建站业务,价格优惠性价比高,为休宁县等各大城市提供网站开发制作服务。

例如 项目在/D:/workspace/MainStream/Test

在javaProject中,getClass().getResource("/").getFile().toString() 返回:/D:/workspace/MainStream/Test/bin/

public String getCurrentPath(){  

//取得根目录路径  

String rootPath=getClass().getResource("/").getFile().toString();  

//当前目录路径  

String currentPath1=getClass().getResource(".").getFile().toString();  

String currentPath2=getClass().getResource("").getFile().toString();  

//当前目录的上级目录路径  

String parentPath=getClass().getResource("../").getFile().toString();  

return rootPath;         

}

参考资料:

java项目路径文件怎么写

有绝对路径与相对路径两种:

绝对路径 :以引用文件之网页所在位置为参考基础,而建立出的目录路径。

绝对路径:以Web站点根目录为参考基础的目录路径。

java中类加载路径和项目根路径获取有几种方式?

package my;  

import java.io.File;  

import java.io.IOException;  

import java.net.URL;  

public class MyUrlDemo {  

public static void main(String[] args) {  

MyUrlDemo muDemo = new MyUrlDemo();  

try {  

muDemo.showURL();  

} catch (IOException e) {  

// TODO Auto-generated catch block  

e.printStackTrace();  

}  

}

public void showURL() throws IOException {  

// 第一种:获取类加载的根路径   D:\git\daotie\daotie\target\classes  

File f = new File(this.getClass().getResource("/").getPath());  

System.out.println(f);  

// 获取当前类的所在工程路径; 如果不加“/”  获取当前类的加载目录  D:\git\daotie\daotie\target\classes\my  

File f2 = new File(this.getClass().getResource("").getPath());  

System.out.println(f2);  

// 第二种:获取项目路径    D:\git\daotie\daotie  

File directory = new File("");// 参数为空  

String courseFile = directory.getCanonicalPath();  

System.out.println(courseFile);  

// 第三种:  file:/D:/git/daotie/daotie/target/classes/  

URL xmlpath = this.getClass().getClassLoader().getResource("");  

System.out.println(xmlpath);  

// 第四种: D:\git\daotie\daotie  

System.out.println(System.getProperty("user.dir"));  

/* 

* 结果: C:\Documents and Settings\Administrator\workspace\projectName 

* 获取当前工程路径 

*/  

// 第五种:  获取所有的类路径 包括jar包的路径  

System.out.println(System.getProperty("java.class.path"));  

}  

}

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项目中如何获取某个文件的路径

File类有两个常用方法可以得到文件路径一个是:getCanonicalPath(),另一个是:getAbsolutePath(),可以通过File类的实例调用这两个方法例如file.getAbsolutePath()其中file是File的实例对象。下面是一个具体例子:

public class PathTest

{

public static void main(String[] args)

{

File file = new File(".\\src\\baidu");

System.out.println(file.getAbsolutePath());

try

{

System.out.println(file.getCanonicalPath());

} catch (IOException e)

{

e.printStackTrace();

}

}

}

getAbsolutePath()和getCanonicalPath()的不同之处在于,getCanonicalPath()得到的是一个规范的路径,而getAbsolutePath()是用构造File对象的路径+当前工作目录。例如在上面的例子中.(点号)代表当前目录。getCanonicalPath()就会把它解析为当前目录但是getAbsolutePath()会把它解析成为目录名字(目录名字是点号)。

下面是上面程序在我电脑上的输出:

G:\xhuoj\konw\.\src\baidu

G:\xhuoj\konw\src\baidu


当前标题:java代码中项目路径 javaweb项目路径问题
分享路径:http://cdkjz.cn/article/doddojj.html
多年建站经验

多一份参考,总有益处

联系快上网,免费获得专属《策划方案》及报价

咨询相关问题或预约面谈,可以通过以下方式与我们联系

业务热线:400-028-6601 / 大客户专线   成都:13518219792   座机:028-86922220