一个挺有用的获取tomcat项目路径的工具类
2013-10-09 09:50
375 查看
import java.io.File; import java.io.FileInputStream; import java.io.IOException; import java.io.UnsupportedEncodingException; import java.util.Date; import java.util.Properties; import java.util.StringTokenizer; import org.omg.CORBA.Object; public class Util { public Util() { } private static void byte2hex(byte b, StringBuffer buf) { char[] hexChars = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' }; int high = ((b & 0xf0) >> 4); int low = (b & 0x0f); buf.append(hexChars[high]); buf.append(hexChars[low]); } public static String toHexStr(byte[] t, int start, int end) { StringBuffer sb = new StringBuffer(); for (int i = 0; i < t.length; i++) { if (start <= i && end >= i) { if (t[i] != 0) { byte2hex(t[i], sb); sb.append(" "); } } } return sb.toString(); } /** * Splits the string on every token into an array of stack frames. * * @param string * the string * @param onToken * the token * @return the resultant array //@deprecated This is an internal utility * method that should not be used */ public static String[] splitString(String string, String onToken) { if (string == null) return null; final StringTokenizer tokenizer = new StringTokenizer(string, onToken); final String[] result = new String[tokenizer.countTokens()]; for (int i = 0; i < result.length; i++) { result[i] = tokenizer.nextToken(); } return result; } public static String getWebModulPath() { String ret = getWebinfPath(); int cnt = ret.indexOf("/WEB-INF"); if (cnt > -1) ret = ret.substring(0, cnt); return ret; } private static String _webinfPath = null; public static String getWebinfPath() { if (_webinfPath == null) { String res = null; Util u = new Util(); String classname = u.getClass().getName().replace('.', '/') + ".class"; ClassLoader cl = u.getClass().getClassLoader(); if (cl != null) { java.net.URL url = cl.getResource(classname); if (url != null) { String path = url.getFile(); int fileStrPosition = path.indexOf("file:/"); int begin = 0; int end = path.length(); if (fileStrPosition >= 0) begin = fileStrPosition + 5; // 先判断是否是未打包文件 end = path.indexOf("classes/" + classname); if (end < 0) { // 如果是在webModule下面的jar包 end = path.indexOf("lib/"); if (end < 0) { // 在普通目录下的jar包 int tmpend = path.indexOf("!/"); end = path.substring(0, tmpend).lastIndexOf("/"); } } String rf = path.substring(begin, end); res = new File(rf).getAbsolutePath().replace('\\', '/') + "/"; } } try { _webinfPath = java.net.URLDecoder.decode(res, "UTF-8"); } catch (UnsupportedEncodingException ex) { } System.out.println("WEB-INF Path=" + _webinfPath); } return _webinfPath; } private static String _systemPath = null; public static String getSystemPath() { if (_systemPath == null) { String res = getWebinfPath(); if (res.indexOf("WEB-INF/") > 0 && res.length() > 10) { res = res.substring(0, res.lastIndexOf("/", res.length() - 12)) + "/"; } _systemPath = res; System.out.println("System Path=" + _systemPath); } return _systemPath; } public static boolean checkStr(String str){ if(str!=null && !"".equals(str)) return true; else return false; } public static boolean checkDate(Date date){ if(date!=null && !date.toString().equals("")) return true; else return false; } public static boolean checkObj(Object obj){ if(obj!=null && !obj.equals("")) return true; else return false; } public static void main(String [] args){ System.out.println(getWebinfPath()); } /** * 获得能量等级 * * @param sex * @param weight * @param height * @param age * @return */ public static int energyDJ(int sex, float weight, float height, int age){ int systarget = 0; if (sex == 1) { systarget = FloatRigor.getIntNum(1.2f * (66 + 13.7f * weight + 5* height - 6.8f * age)*0.9f); } else { systarget = FloatRigor.getIntNum(1.2f * (655 + 9.6f * weight + 1.8f* height - 4.7f * age)*0.9f); } if (systarget < 1400) return 1200; else if (systarget < 1600) return 1400; else if (systarget < 1800) return 1600; else if (systarget < 2000) return 1800; else if (systarget < 2200) return 2000; else if (systarget < 2400) return 2200; else if (systarget < 2600) return 2400; else return 2600; } /** * 返回Properties对象 * * @author xhx 2008-4-28 * @param filename * @return * @throws IOException */ public static Properties getProperties(String path) throws IOException { File file = new File(path); Properties p = new Properties(); if (file.exists()) { FileInputStream input; input = new FileInputStream(path); p.load(input); return p; } else { return null; } } }
主要方法执行结果如下:
Util.getSystemPath():D:/webspace/enterprise/ Util.getWebinfPath():D:/webspace/enterprise/WebRoot/WEB-INF/ Util.getWebModulPath():D:/webspace/enterprise/WebRoot
相关文章推荐
- tomcat绑定域名后无法获取项目路径
- JSP/J2EE 在普通类中获取项目在tomcat里面的物理路径
- 获取 项目所在的tomcat下的webapps 绝对路径
- web项目Log4j日志输出路径配置问题 问题描述:一个web项目想在一个tomcat下运行多个实例(通过修改war包名称的实现),然后每个实例都将日志输出到tomcat的logs目录下实例名命名的文
- tomcat 设置文件指向路径 和发布项目 路径(保证指向同一个项目名)
- 获取当前的系统路径以及将项目部署到Tomcat服务器上
- 获取项目在服务其中的真实路径的工具类
- java 获取当前类所在项目的Tomcat/Resin 的webapps路径
- JAVA获取项目类路径的一个坑
- 一个tomcat部署多个项目(同端口,多项目,不同访问路径)
- wap获取手机号码,今天uc开启云加速,用公司的工具类获取是null,但是另外一个项目可以,应该这个项目的工具旧少了一些代码。。,网上找了 方法自己加可以
- 一个tomcat部署多个项目(同端口,多项目,不同访问路径)
- Tomcat下获取项目绝对路径问题
- 一个java获取当前项目路径的方法
- 在tomcat与weblogic下获取项目路径的区别(如报错:java.io.FileNotFoundException)
- Eclipse部署项目到tomcat,获取项目路径
- 获取tomcat下java项目根目录路径
- JAVA_WEB项目中响应端获取文件路径的方式(tomcat服务器)
- tomcat 和 weblogic获取项目的根路径的方法
- C#获取当前项目路径