java 获取上下文路径

一、在java类中获取路径

1.获取项目的目录,

String filePath = LicValidate.class.getProtectionDomain().getCodeSource().getLocation().getFile();

System.out.println(filePath);


2.获取服务器的目录

 //D:\apache-tomcat-7.0.82\webapps\ROOT\WEB-INF
            File directory = new File("");//设定为当前文件夹
//          System.out.println(directory.getAbsolutePath());//D:\apache-tomcat-7.0.82\bin
            String path = directory.getAbsolutePath().replace("bin","webapps/ROOT/WEB-INF/license.lic");

总结:两种获取方式,侧重点不一,一个是获取项目,一个是服务器。

猜你喜欢

转载自blog.csdn.net/u013630932/article/details/79732233