Java tutorial
/* * Copyright 2011-2016 ZXC.com All right reserved. This software is the confidential and proprietary information of * ZXC.com ("Confidential Information"). You shall not disclose such Confidential Information and shall use it only in * accordance with the terms of the license agreement you entered into with ZXC.com. */ package com.ms.commons.utilities; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.net.InetAddress; import java.net.UnknownHostException; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; /** * ?? * * @author zxc Apr 12, 2013 1:36:41 PM */ public class CoreUtilities { private static final Log logger = LogFactory.getLog(CoreUtilities.class); // IP? private static String ipAddress = null; // HostName private static String hostName = null; /** * ??? * * @return */ public static String getHostName() { if (hostName != null) { return hostName; } else { try { String cmd = isWindowsOS() ? "hostname" : "/bin/hostname"; Process process = Runtime.getRuntime().exec(cmd); process.waitFor(); InputStream in = process.getInputStream(); InputStreamReader inr = new InputStreamReader(in); BufferedReader br = new BufferedReader(inr); String wg = br.readLine(); if (wg != null) { hostName = wg.trim(); } else { hostName = "unknown hostname"; } } catch (Exception e) { logger.error("Exception", e); hostName = "unknown hostname"; } } return hostName; } /** * IP??getHostAddress()??Linux? */ public static String getIPAddress() { if (ipAddress != null && ipAddress.length() > 0) { return ipAddress; } String hostIp = null; try { hostIp = InetAddress.getLocalHost().getHostAddress(); } catch (UnknownHostException e) { logger.error("UnknownHostException", e); } catch (Exception e) { logger.error("Exception", e); } if (hostIp != null) { String tmpIp = hostIp.toLowerCase(); if (tmpIp.startsWith("localhost")) { hostIp = null; } else if (tmpIp.startsWith("127")) { hostIp = null; } } if (hostIp != null) { ipAddress = hostIp; return ipAddress; } try { ipAddress = getIfconig(); } catch (Exception e) { logger.error("getIfconig Error", e); ipAddress = "unknownIp"; } return ipAddress; } /** * ?Linux?IP * * @return * @throws IOException * @throws InterruptedException */ private static String getIfconig() throws IOException, InterruptedException { String cmd = "/sbin/ifconfig"; Process process = Runtime.getRuntime().exec(cmd); process.waitFor(); InputStream in = process.getInputStream(); InputStreamReader inr = new InputStreamReader(in); BufferedReader br = new BufferedReader(inr); String wg = ""; while (true) { String line = br.readLine(); if (line == null) { break; } if (line.indexOf("inet addr") != -1 || line.indexOf("inet ?") != -1) { wg = line.substring(line.indexOf(":") + 1, line.length()); wg = wg.trim(); int index = wg.indexOf("Bcast"); if (index == -1) { index = wg.indexOf(""); } if (index != -1) { wg = wg.substring(0, index); wg = wg.trim(); break; } else { if (wg.length() > 14) { wg = wg.substring(0, 14); } } } } return wg.trim(); } /** * * * @param e * @return */ public static String getExceptionText(Throwable e) { if (e != null) { StringBuilder sb = new StringBuilder(); StackTraceElement[] st = e.getStackTrace(); for (StackTraceElement s : st) { sb.append(s.toString()).append("\r\n"); } return sb.toString(); } return null; } public static boolean isWindowsOS() { String p = System.getProperties().getProperty("os.name"); if (p != null && p.toLowerCase().indexOf("windows") != -1) { return true; } return false; } public static void main(String[] a) { // String hostName = getIPAddress(); // String p = System.getProperties().getProperty("os.name"); // System.out.println(p); } }