www.pudn.com > network-java.rar > SubnetAddress.java
package netManager.discovery; /** *Title: SubnetAddress
*Description: 获得子网地址
*Copyright: Copyright (c) 2006
*Company:
* @author:谢飞 * @version 1.0 */ import java.net.InetAddress; import java.net.UnknownHostException; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import netManager.framework.exception.BaseException; public class SubnetAddress{ String subnetMask; //子网掩码 String subnetAddress; //本机所在子网地址 String localIP ; //本机地址 private Log log = LogFactory.getLog("SubnetAddress"); public SubnetAddress() throws BaseException{ try{ //localSubnetMask = LocalSubnetMask.getSubnetMask(); subnetMask = SubnetMask.getSubnetMask(); localIP = InetAddress.getLocalHost().getHostAddress(); //subnetMask = "255.255.255.0"; //localIP = "192.168.0.1"; } catch(Exception e){ log.error(e); BaseException be = new BaseException(); be.setMessageKey("error.subadd.error"); be.setRootCause(e); throw be; } } //获得子网地址 public String getSubnetAddress(){ try{ Long longSubnetMask = Convert.toDecimalist(subnetMask); //将子网掩码转换位长整型 Long longLocalIP = Convert.toDecimalist(localIP); Long longSubnetAddress = longSubnetMask & longLocalIP; //子网地址 = 本机地址 & 子网掩码 subnetAddress = Convert.toIP(longSubnetAddress); //System.out.println("子网地址为:" + subnetAddress); } catch(Exception e){ //BaseException be = new BaseException(); //be.setRootCause(e); log.error(e); //throw be; } return subnetAddress; } }