一个挺有用的获取tomcat项目路径的工具类

来源:互联网 发布:网络传送带使用教程 编辑:程序博客网 时间:2024/06/06 02:22
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;elsereturn false;}public static boolean checkDate(Date date){if(date!=null && !date.toString().equals(""))return true;elsereturn false;}public static boolean checkObj(Object obj){if(obj!=null && !obj.equals(""))return true;elsereturn 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;elsereturn 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