兰丽(川) 发表于 2013-2-1 11:20:32

java获取系统消息

private void main(){
StringBuffer licenseXml = new StringBuffer();
    response.setContentType("text/xml");
    licenseXml.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
    licenseXml.append("<xml>");
    licenseXml.append("<ip>").append(
      InetAddress.getLocalHost().getHostAddress()).append("</ip>");
    licenseXml.append("<mac>").append(getMacAddress()).append("</mac>");
    licenseXml.append("<systemName>").append(System.getProperty("os.name"))
      .append("</systemName>");
    licenseXml.append("<structure>").append(System.getProperty("os.arch"))
      .append("</structure>");
    licenseXml.append("<version>").append(System.getProperty("os.version"))
      .append("</version>");
    licenseXml.append("</xml>");
}

private String getMacAddress() throws IOException {
    String os = System.getProperty("os.name");
    try {
      if (os.startsWith("Windows")) {
      return windowsParseMacAddress(windowsRunIpConfigCommand());
      } else if (os.startsWith("Linux")) {
      return linuxParseMacAddress(linuxRunIfConfigCommand());
      } else {
      throw new IOException("unknown operating system: " + os);
      }
    } catch (ParseException ex) {
      ex.printStackTrace();
      throw new IOException(ex.getMessage());
    }
}

/*
   * Linux stuff
   */
private static String linuxParseMacAddress(String ipConfigResponse)
      throws ParseException {
    String localHost = null;
    try {
      localHost = InetAddress.getLocalHost().getHostAddress();
    } catch (java.net.UnknownHostException ex) {
      ex.printStackTrace();
      throw new ParseException(ex.getMessage(), 0);
    }

    StringTokenizer tokenizer = new StringTokenizer(ipConfigResponse, "\n");
    String lastMacAddress = null;

    while (tokenizer.hasMoreTokens()) {
      String line = tokenizer.nextToken().trim();
      boolean containsLocalHost = line.indexOf(localHost) >= 0;

      // see if line contains IP address
      if (containsLocalHost && lastMacAddress != null) {
      return lastMacAddress;
      }

      // see if line contains MAC address
      int macAddressPosition = line.indexOf("HWaddr");
      if (macAddressPosition <= 0)
      continue;

      String macAddressCandidate = line.substring(macAddressPosition + 6)
          .trim();
      if (linuxIsMacAddress(macAddressCandidate)) {
      lastMacAddress = macAddressCandidate;
      continue;
      }
    }

    ParseException ex = new ParseException("cannot read MAC address for "
      + localHost + " from [" + ipConfigResponse + "]", 0);
    ex.printStackTrace();
    throw ex;
}

private static boolean linuxIsMacAddress(String macAddressCandidate) {
    if (macAddressCandidate.length() != 17)
      return false;
    return true;
}

private static String linuxRunIfConfigCommand() throws IOException {

    Process p = Runtime.getRuntime().exec("ifconfig");
    InputStream stdoutStream = new BufferedInputStream(p.getInputStream());

    StringBuffer buffer = new StringBuffer();
    for (;;) {
      int c = stdoutStream.read();
      if (c == -1)
      break;
      buffer.append((char) c);
    }
    String outputText = buffer.toString();

    stdoutStream.close();

    return outputText;
}

private static String windowsParseMacAddress(String ipConfigResponse)
      throws ParseException {
    String localHost = null;
    try {
      localHost = InetAddress.getLocalHost().getHostAddress();
    } catch (java.net.UnknownHostException ex) {
      ex.printStackTrace();
      throw new ParseException(ex.getMessage(), 0);
    }

    StringTokenizer tokenizer = new StringTokenizer(ipConfigResponse, "\n");
    String lastMacAddress = null;

    while (tokenizer.hasMoreTokens()) {
      String line = tokenizer.nextToken().trim();
      if (line.endsWith(localHost) && lastMacAddress != null) {
      return lastMacAddress;
      }
      int macAddressPosition = line.indexOf(":");
      if (macAddressPosition <= 0)
      continue;

      String macAddressCandidate = line.substring(macAddressPosition + 1)
          .trim();
      if (windowsIsMacAddress(macAddressCandidate)) {
      lastMacAddress = macAddressCandidate;
      continue;
      }
    }

    ParseException ex = new ParseException("cannot read MAC address from ["
      + ipConfigResponse + "]", 0);
    ex.printStackTrace();
    throw ex;
}

private static boolean windowsIsMacAddress(String macAddressCandidate) {
    if (macAddressCandidate.length() != 17)
      return false;

    return true;
}

private static String windowsRunIpConfigCommand() throws IOException {
    Process p = Runtime.getRuntime().exec("ipconfig /all");
    InputStream stdoutStream = new BufferedInputStream(p.getInputStream());
    StringBuffer buffer = new StringBuffer();
    for (;;) {
      int c = stdoutStream.read();
      if (c == -1)
      break;
      buffer.append((char) c);
    }
    String outputText = buffer.toString();

    stdoutStream.close();

    return outputText;
}
页: [1]
查看完整版本: java获取系统消息