一.得到绝对路径
本人用到的不多,就列举常用的。
以上2种可读取固定配置文件可用得到绝对路径
1.System.getProperty("user.dir");
这个东西局限性十分高
一定要在运行了web程序并且配置了环境变量的环境中才可以得到,例如现在很多环境用myeclipse编写的自带jdk,结果路基就根本找不到。
顺便把System的东西都拿出来
public static void main(String[] args){
System.out.println("Java运行时环境版本:/n"+System.getProperty("java.version"));
System.out.println("Java 运行时环境供应商:/n"+System.getProperty("java.vendor"));
System.out.println("Java 供应商的URL:/n"+System.getProperty("java.vendor.url"));
System.out.println("Java安装目录:/n"+System.getProperty("java.home"));
System.out.println("Java 虚拟机规范版本:/n"+System.getProperty("java.vm.specification.version"));
System.out.println("Java 类格式版本号:/n"+System.getProperty("java.class.version"));
System.out.println("Java类路径:/n"+System.getProperty("java.class.path"));
System.out.println("加载库时搜索的路径列表:/n"+System.getProperty("java.library.path"));
System.out.println("默认的临时文件路径:/n"+System.getProperty("java.io.tmpdir"));
System.out.println("要使用的 JIT 编译器的名称:/n"+System.getProperty("java.compiler"));
System.out.println("一个或多个扩展目录的路径:/n"+System.getProperty("java.ext.dirs"));
System.out.println("操作系统的名称:/n"+System.getProperty("os.name"));
System.out.println("操作系统的架构:/n"+System.getProperty("os.arch"));
System.out.println("操作系统的版本:/n"+System.getProperty("os.version"));
System.out.println("文件分隔符(在 UNIX 系统中是“/”):/n"+System.getProperty("file.separator"));
System.out.println("路径分隔符(在 UNIX 系统中是“:”):/n"+System.getProperty("path.separator"));
System.out.println("行分隔符(在 UNIX 系统中是“/n”):/n"+System.getProperty("line.separator"));
System.out.println("用户的账户名称:/n"+System.getProperty("user.name"));
System.out.println("用户的主目录:/n"+System.getProperty("user.home"));
System.out.println("用户的当前工作目录:/n"+System.getProperty("user.dir"));
}
2.class.getResource("/").getPath());
tempPath.append(Mail.class.getResource("/").getPath());//获取当前class所在的路径
if(tempPath.length()>0){//如果路径长度>0则路径存在
tempPath.deleteCharAt(0);
tempPath.delete(tempPath.length()-8, tempPath.length());//删除掉/classes
}
这东西很十分好用,但是这个只能读单域的web服务器中的程序才可以得到,多域的无法得到。
我用到过的中间件。单域的tomcat jboss 多域的weblogic WebSphere
在Servlet或者一些框架的action中得到绝对路径
3.servlet.getServletContext().getRealPath("/");
struts2 中ServletActionContext.getServletContext().getRealPath("/");
关于为什么要得绝对路径,那是因为读取web项目的一些外面类无法读取相对路径,比如说我们常用的读取xml
很多情况自己些的配置的东西是在web项目加载是启动的,而不是在servlet中读取的。
比如最常见的jdom方式读取xml。
1.用fileInputstream 读取 一般xml 这种方式只能读取绝对路径
例如下面的读取邮件配置的xml文件
一般自定义xml
- <?xml version="1.0" encoding="gb2312"?>
- <onlineorder>
- <address>
- <smtpusername>myzqf88@163.com</smtpusername>
- <smtppwd>111</smtppwd>
- <smtpserver>smtp.163.com</smtpserver>
- </address>
- </onlineorder>
java代码
- SAXBuilder sb = new SAXBuilder(); // 新建立构造器
- ocument doc1 = sb.build(new FileInputStream(path));
- Element root1=doc1.getRootElement();
- String smtpusername=""; //用户名
- String smtppwd=""; //密码
- String smtpserver=""; //邮件服务器域名
- List list1=root1.getChildren("address");
- for(int ii=0;ii<list1.size();ii++){
- Element element=(Element)list1.get(ii);
- smtpusername=element.getChildText("smtpusername");
- smtppwd=element.getChildText("smtppwd");
- smtpserver=element.getChildText("smtpserver");
- }
1.用InputStream 读取 一般xml 这种方式读取相对路径
jdm层次结构的xml
- <?xml version="1.0" encoding="UTF-8"?>
- <data-services>
- <service name="longin">
- <sql>
- <!--[CDATA[
- SELECT count(*) FROM jcewk_com.dbo.BackPerson where flag='0' and userid=? and password=?
- ]]-->
- </sql>
- <param-type>12</param-type>
- </service>
- <service name="pzlist">
- <sql>
- <![CDATA[
- select id,title,committime,currstep,filenumber from jcewk_gwgl.dbo.handle where acceptuserid =? and (currstep ='待批转联合发文' or currstep='重新批转联合发文')
- ]]>
- </sql>
- <param-type>12</param-type>
- </service>
- </data-services>
java
- public static final String DNDI = "/dndi.xml";//包含各业务逻辑的SQL语句
- // 其中/表示相对于web项目classes目录下
- private void config() throws XMLException {
- services = new HashMap();
- InputStream is=this.getClass().getResourceAsStream(Constants.DNDI);
- xml = XMLUtil.getInsance(is);
- List serviceLists = xml.getAllElements("/data-services/service");
- Element each = null;
- String name = null;
- for (Iterator all = serviceLists.iterator(); all.hasNext(); ) {
- each = (Element) all.next();
- name = each.getAttributeValue("name").trim();
- if (services.containsKey(name))
- throw new XMLException("重复的配置:name = " + name);
- services.put(name, this.get_a_dndivo(each));
- }
- }
- private DndiVO get_a_dndivo(Element each) {
- DndiVO dndiVO = new DndiVO();
- dndiVO.setSql(each.getChildTextTrim("sql")); //得到sql语句
- List params = each.getChildren("param-type"); //得到对应的数据连接池上线
- for (Iterator iter = params.iterator(); iter.hasNext(); ) {
- Element item = (Element) iter.next();
- dndiVO.addParams(item.getTextTrim());
- }
- return dndiVO;
- }