java web绝对路径获取

来源:互联网 发布:c语言 if else 编辑:程序博客网 时间:2024/05/21 07:58
      String path="/"+Thread.currentThread().getContextClassLoader().getResource("").getPath().substring(1);        System.out.println(path);        path = path.replace("WEB-INF/classes/", "images/");        System.out.println(path);/D:/green/tomcats/apache-tomcat-6.0.37/webapps/myWeb/WEB-INF/classes//D:/green/tomcats/apache-tomcat-6.0.37/webapps/myWeb/images/

 

public class FileUtil {/** * 获取项目绝对路径 *  * @return */public static String getRootPath() {String classPath = FileUtil.class.getClassLoader().getResource("/").getPath();String rootPath = "";// windows下if ("\\".equals(File.separator)) {rootPath = classPath.substring(1, classPath.indexOf("/WEB-INF/classes"));rootPath = rootPath.replace("/", "\\");}// linux下if ("/".equals(File.separator)) {rootPath = classPath.substring(0, classPath.indexOf("/WEB-INF/classes"));rootPath = rootPath.replace("\\", "/");}return rootPath;}}


 

0 0