精华内容
下载资源
问答
  • 总体来说设计模式分为三大类: (1)创建型模式,共五种: 工厂方法模式、抽象工厂...非常全面的设计模式讲解,有讲解,有例子,java代码例子,请参考链接 转载: http://www.phperz.com/article/15/0814/148654.html

    总体来说设计模式分为三大类:

    (1)创建型模式,共五种:工厂方法模式、抽象工厂模式、单例模式、建造者模式、原型模式。
    (2)结构型模式,共七种:适配器模式、装饰器模式、代理模式、外观模式、桥接模式、组合模式、享元模式。
    (3)行为型模式,共十一种:策略模式、模板方法模式、观察者模式、迭代子模式、责任链模式、命令模式、备忘录模式、状态模式、访问者模式、中介者模式、解释器模式。
    非常全面的设计模式讲解,有讲解,有例子,java代码例子,请参考链接

    转载: http://www.phperz.com/article/15/0814/148654.html



    展开全文
  • 第一次接触BACnet ip,开发语言使用java,网上搜了下,都是推荐使用BACnet4j,但是找不到完整的demo,折腾了一段时间,勉强跑通了自己写的demo,读取到的设备模拟器上的数据。 1.下载Yabe设备模拟器 链接: ...

       第一次接触BACnet ip,开发语言使用java,网上搜了下,都是推荐使用BACnet4j,但是找不到完整的demo,折腾了一段时间,勉强跑通了自己写的demo,读取到的设备模拟器上的数据。

    1.下载Yabe设备模拟器

      链接: https://pan.baidu.com/s/1OK1uq-tfU-XoOH10h4Otag 提取码: 4eu4

    安装后

    打开太阳的图标

    这是设备模拟器,Yabe还提供了一个客户端,打开放大镜图标

    可以在client端看到相关的数据信息。

    2.下载BACnet4j

    https://github.com/infiniteautomation/BACnet4J

    下载需要的版本,本博文的代码使用的是5.0.2版本,然后本地安装 mvn install -Dmaven.test.skip=true

    3.写demo代码

    创建maven项目,pom.xml引入BACnet4j(上面下载安装的),比如

    <dependencies>
    	<!-- https://github.com/infiniteautomation/BACnet4J -->
    	<dependency>
    		<groupId>com.infiniteautomation</groupId>
    	    <artifactId>bacnet4j</artifactId>
    	    <version>5.0.2</version>
    	</dependency>
    </dependencies>

    注意:1.Yabe和代码在同一台电脑少,跨网段的话暂时不知道该怎样解决。

               2.运行代码的时候,必须先关闭Yabe的客户端(explorer),否则代码会提示地址被占用了

    ReadTest01.java

    package com.fei;
    
    import java.util.Arrays;
    import java.util.List;
    
    import com.serotonin.bacnet4j.LocalDevice;
    import com.serotonin.bacnet4j.RemoteDevice;
    import com.serotonin.bacnet4j.npdu.ip.IpNetwork;
    import com.serotonin.bacnet4j.npdu.ip.IpNetworkBuilder;
    import com.serotonin.bacnet4j.transport.DefaultTransport;
    import com.serotonin.bacnet4j.type.Encodable;
    import com.serotonin.bacnet4j.type.enumerated.ObjectType;
    import com.serotonin.bacnet4j.type.enumerated.PropertyIdentifier;
    import com.serotonin.bacnet4j.type.primitive.ObjectIdentifier;
    import com.serotonin.bacnet4j.type.primitive.UnsignedInteger;
    import com.serotonin.bacnet4j.util.PropertyValues;
    import com.serotonin.bacnet4j.util.ReadListener;
    import com.serotonin.bacnet4j.util.RequestUtils;
    
    /**
     * 启动Yabe的天气模拟
     * @author Jfei
     *
     */
    public class ReadTest01 {
    
    	/**
    	 * Yabe在本地电脑上启动
    	 * @param args
    	 * @throws Exception 
    	 */
    	public static void main(String[] args) throws Exception {
    		LocalDevice d = null;
    		try {
    			//创建网络对象
    			IpNetwork ipNetwork = new IpNetworkBuilder()
    					 .withLocalBindAddress("192.168.1.114")//本机的ip
    					 .withSubnet("255.255.255.0", 24)  //掩码和长度,如果不知道本机的掩码和长度的话,可以使用后面代码的工具类获取
    					 .withPort(47808) //Yabe默认的UDP端口
    					 .withReuseAddress(true)
    					 .build();
    			//创建虚拟的本地设备,deviceNumber随意
    			 d = new LocalDevice(123, new DefaultTransport(ipNetwork));
    			 d.initialize();
    			 d.startRemoteDeviceDiscovery();
    			 
    			 RemoteDevice rd = d.getRemoteDeviceBlocking(3);//获取远程设备,instanceNumber 是设备的device id
    			 
    			 System.out.println("modelName=" + rd.getDeviceProperty( PropertyIdentifier.modelName));
    			 System.out.println("analogInput2= " +RequestUtils.readProperty(d, rd, new ObjectIdentifier(ObjectType.analogInput, 2), PropertyIdentifier.presentValue, null));
    			 
    			 
    			 List<ObjectIdentifier> objectList =  RequestUtils.getObjectList(d, rd).getValues();
    			 
    			 //打印所有的Object 名称
    			 for(ObjectIdentifier o : objectList){
    				 System.out.println(o);
    			 }
    			 
    			
    			 ObjectIdentifier oid = new ObjectIdentifier(ObjectType.analogInput, 0);
    		     ObjectIdentifier oid1 = new ObjectIdentifier(ObjectType.analogInput, 1);
    		     ObjectIdentifier oid2 = new ObjectIdentifier(ObjectType.analogInput, 2);
    		     
    		     //获取指定的presentValue
    	         PropertyValues pvs = RequestUtils.readOidPresentValues(d, rd,Arrays.asList(oid,oid1,oid2), new ReadListener(){
    				@Override
    				public boolean progress(double progress, int deviceId,
    						ObjectIdentifier oid, PropertyIdentifier pid,
    						UnsignedInteger pin, Encodable value) {
    					System.out.println("========");
    					System.out.println("progress=" + progress);
    					System.out.println("deviceId=" + deviceId);
    					System.out.println("oid="+oid.toString());
    					System.out.println("pid="+pid.toString());
    					System.out.println("UnsignedInteger="+pin);
    					System.out.println("value="+value.toString() + "  getClass =" +value.getClass());
    					return false;
    				}
    	        	 
    	         });
    	         Thread.sleep(3000);
    	         System.out.println("analogInput:0 == " + pvs.get(oid, PropertyIdentifier.presentValue));
    	       //获取指定的presentValue
    	         PropertyValues pvs2 = RequestUtils.readOidPresentValues(d, rd,Arrays.asList(oid,oid1,oid2),null);
    	         System.out.println("analogInput:1 == " + pvs2.get(oid1, PropertyIdentifier.presentValue));
    	         
    	         d.terminate();
    		} catch (Exception e) {
    			e.printStackTrace();
    			if(d != null){
    				d.terminate();
    			}
    		}
    		
    	}
    }
    

    执行结果

    SLF4J: No SLF4J providers were found.
    SLF4J: Defaulting to no-operation (NOP) logger implementation
    SLF4J: See http://www.slf4j.org/codes.html#noProviders for further details.
    modelName=Room_FC_2014
    analogInput2= 12.0
    device 3
    analog-input 0
    analog-input 1
    analog-input 2
    analog-value 0
    analog-value 1
    analog-value 2
    analog-value 3
    characterstring-value 1
    characterstring-value 2
    characterstring-value 3
    binary-value 0
    binary-value 1
    multi-state-value 0
    multi-state-value 1
    ========
    progress=0.3333333333333333
    deviceId=3
    oid=analog-input 0
    pid=present-value
    UnsignedInteger=null
    value=27.8  getClass =class com.serotonin.bacnet4j.type.primitive.Real
    ========
    progress=0.6666666666666666
    deviceId=3
    oid=analog-input 1
    pid=present-value
    UnsignedInteger=null
    value=39.7  getClass =class com.serotonin.bacnet4j.type.primitive.Real
    ========
    progress=1.0
    deviceId=3
    oid=analog-input 2
    pid=present-value
    UnsignedInteger=null
    value=12.0  getClass =class com.serotonin.bacnet4j.type.primitive.Real
    analogInput:0 == 27.8
    analogInput:1 == 39.5
    

    WriteTest01.java

    package com.fei;
    
    import com.serotonin.bacnet4j.LocalDevice;
    import com.serotonin.bacnet4j.RemoteDevice;
    import com.serotonin.bacnet4j.npdu.ip.IpNetwork;
    import com.serotonin.bacnet4j.npdu.ip.IpNetworkBuilder;
    import com.serotonin.bacnet4j.transport.DefaultTransport;
    import com.serotonin.bacnet4j.type.enumerated.ObjectType;
    import com.serotonin.bacnet4j.type.enumerated.PropertyIdentifier;
    import com.serotonin.bacnet4j.type.primitive.Boolean;
    import com.serotonin.bacnet4j.type.primitive.ObjectIdentifier;
    import com.serotonin.bacnet4j.type.primitive.Real;
    import com.serotonin.bacnet4j.util.RequestUtils;
    
    public class WriteTest01 {
    
    	public static void main(String[] args) throws Exception {
    		LocalDevice d = null;
    		try {
    			IpNetwork ipNetwork = new IpNetworkBuilder()
    			 .withLocalBindAddress("192.168.1.114")
    			 .withSubnet("255.255.255.0", 24)
    			 .withPort(47808)
    			 .withReuseAddress(true)
    			 .build();
    			
    			d = new LocalDevice(123, new DefaultTransport(ipNetwork));
    			d.initialize();
    			d.startRemoteDeviceDiscovery();
    			
    			RemoteDevice rd = d.getRemoteDevice(3).get();//获取远程设备
    			
    			//必须先修改out of service为true
    	        RequestUtils.writeProperty(d, rd, new ObjectIdentifier(ObjectType.analogValue, 0),PropertyIdentifier.outOfService, Boolean.TRUE);
    	        Thread.sleep(1000);
    	        //修改属性值
    			RequestUtils.writePresentValue(d, rd, new ObjectIdentifier(ObjectType.analogValue, 0), new Real(77));
    			Thread.sleep(2000);
    			System.out.println("analogValue0= " +RequestUtils.readProperty(d, rd, new ObjectIdentifier(ObjectType.analogValue, 0), PropertyIdentifier.presentValue, null));
    			 
    			 Thread.sleep(1000);
    			d.terminate();
    		} catch (Exception e) {
    			e.printStackTrace();
    			if(d != null){
    				d.terminate();
    			}
    		}
    		
    	}
    }
    

    执行结果

    SLF4J: No SLF4J providers were found.
    SLF4J: Defaulting to no-operation (NOP) logger implementation
    SLF4J: See http://www.slf4j.org/codes.html#noProviders for further details.
    analogValue0= 77.0
    

    注意:修改的话,必须是设备端运行被修改的才行,否则会报异常,拒绝写。可以使用Yabe的客户端(expoler)进行测试确认哪个只能被修改.

    注意:代码里的ip或掩码都是代码所在的电脑的ip,可以使用一下代码获取本地的ip或掩码

    package com.nanhe.common.util;
    
    import java.lang.management.ManagementFactory;
    import java.net.Inet4Address;
    import java.net.InetAddress;
    import java.net.InterfaceAddress;
    import java.net.NetworkInterface;
    import java.net.SocketException;
    import java.net.UnknownHostException;
    import java.util.Enumeration;
    import java.util.Set;
    
    import javax.management.MBeanServer;
    import javax.management.MalformedObjectNameException;
    import javax.management.ObjectName;
    import javax.management.Query;
    
    import org.slf4j.Logger;
    import org.slf4j.LoggerFactory;
    
    public class IpUtil {
    
    	private static Logger LOG = LoggerFactory.getLogger(IpUtil.class);
    	
    	/**
    	 * 获取本机IP地址
    	 * @return
    	 * @throws SocketException
    	 */
    	public static String getIpAddress() throws SocketException {
    	        String ipString = null;
    	        Inet4Address inet4Address  = getInet4Address();
    			if(inet4Address != null){
    				/*NetworkInterface networkInterface = NetworkInterface.getByInetAddress(inet4Address);
    	    		for (InterfaceAddress address : networkInterface.getInterfaceAddresses()) {
    	    			ipString = address.getAddress().getHostAddress();
    	    		}*/
    				ipString = inet4Address.getHostAddress();
    			}
    	    LOG.info("本机IP地址={}" , ipString);
    	    return ipString;
    	  }
    	
    	/**
    	 * 获取tomcat容器的http端口
    	 * @return
    	 * @throws MalformedObjectNameException 
    	 */
    	public static String getTomcatHttpPort() throws MalformedObjectNameException{
    		MBeanServer beanServer = ManagementFactory.getPlatformMBeanServer();
    		Set<ObjectName> objectNames = beanServer.queryNames(new ObjectName("*:type=Connector,*"),
                    Query.match(Query.attr("protocol"), Query.value("HTTP/1.1")));
            String port = objectNames.iterator().next().getKeyProperty("port");
            return port;
    	}
    	
    	/**
    	 * 获取网络前缀长度,
    	 * 如果长度为8,则表示掩码是255.0.0.0,
    	 * 如果长度为16,则表示掩码是255.255.0.0,
    	 * 如果长度为24,则表示掩码是255.255.255.0,
    	 * @return
    	 * @throws UnknownHostException 
    	 * @throws SocketException 
    	 */
    	public static int getNetworkPrefixLength() throws UnknownHostException, SocketException{
    		
    		int networkPrefixLength = 0;
    		Inet4Address inet4Address  = getInet4Address();
    		if(inet4Address != null){
    			NetworkInterface networkInterface = NetworkInterface.getByInetAddress(inet4Address);
        		for (InterfaceAddress address : networkInterface.getInterfaceAddresses()) {
        			if(address.getAddress() instanceof Inet4Address){
        				networkPrefixLength =  address.getNetworkPrefixLength();
        			}
        			
        		}
    		}
    		LOG.info("本机网络前缀长度networkPrefixLength={}" , networkPrefixLength);
    		return networkPrefixLength;
    	}
    	
    	/**
    	 * 获取网络掩码255.0.0.0,255.0.0.0,255.0.0.0,
    	 * @return
    	 * @throws UnknownHostException 
    	 * @throws SocketException 
    	 */
    	public static String getSubnet() throws UnknownHostException, SocketException{
    		String subnet = null;
    		int prefix = getNetworkPrefixLength();
    		if(prefix > 0){
    			if(prefix == 8){
    				subnet = "255.0.0.0";
    			}else if(prefix == 16){
    				subnet = "255.255.0.0";
    			}else if(prefix == 24){
    				subnet = "255.255.255.0";
    			}else if(prefix == 32){
    				subnet = "255.255.255.255";
    			}
    		}
    		return subnet;
    	}
    	
    	private static Inet4Address getInet4Address() throws SocketException{
    		 Inet4Address inet4Address = null;
    		 Enumeration<NetworkInterface> allNetInterfaces = NetworkInterface.getNetworkInterfaces();
    	      InetAddress ip = null;
    	      while (allNetInterfaces.hasMoreElements()) {
    	        NetworkInterface netInterface = (NetworkInterface) allNetInterfaces.nextElement();
    	        //用于排除回送接口,非虚拟网卡,未在使用中的网络接口.
    	        if (netInterface.isLoopback() || netInterface.isVirtual() || !netInterface.isUp()) {
    	          continue;
    	        } else {
    	          Enumeration<InetAddress> addresses = netInterface.getInetAddresses();
    	          while (addresses.hasMoreElements()) {
    	            ip = addresses.nextElement();
    	            if (ip != null && ip instanceof Inet4Address) {
    	            	inet4Address = (Inet4Address)ip;
    	                break;
    	            }
    	          }
    	          if(inet4Address != null){
    	        	  break;
    	          }
    	        }
    	      }
    	      return inet4Address;
    	}
    	
    	public static void main(String[] args) throws SocketException, UnknownHostException {
    		System.out.println(getIpAddress());
    	   System.out.println(getNetworkPrefixLength());
    	   System.out.println(getSubnet());
    	}
    	
    	
    }
    

     

    展开全文
  • 上一篇博客中,代码和设备模拟器Yabe是同一台电脑上,现在试试代码和设备模拟器不在同一台电脑上,但是在同网段内,因为bacnet ip是通过udp在同网段内进行广播的。现在进行测试下。 Yabe运行在192.168.0.200上,...

        上一篇博客中https://blog.csdn.net/dream_broken/article/details/106646604,代码和设备模拟器Yabe是同一台电脑上,现在试试代码和设备模拟器不在同一台电脑上,但是在同网段内,因为bacnet ip是通过udp在同网段内进行广播的。现在进行测试下。

       Yabe运行在192.168.0.200上,代码在192.168.0.123上。测试环境window,开发工具eclipse.

      注意:必须同网段,必须同网段,必须同网段。不明白为什么同网段要求的,自行百度了解UDP协议

    代码

    package com.fei;
    
    import java.util.Arrays;
    import java.util.List;
    
    import com.serotonin.bacnet4j.LocalDevice;
    import com.serotonin.bacnet4j.RemoteDevice;
    import com.serotonin.bacnet4j.npdu.ip.IpNetwork;
    import com.serotonin.bacnet4j.npdu.ip.IpNetworkBuilder;
    import com.serotonin.bacnet4j.transport.DefaultTransport;
    import com.serotonin.bacnet4j.type.Encodable;
    import com.serotonin.bacnet4j.type.enumerated.ObjectType;
    import com.serotonin.bacnet4j.type.enumerated.PropertyIdentifier;
    import com.serotonin.bacnet4j.type.primitive.ObjectIdentifier;
    import com.serotonin.bacnet4j.type.primitive.UnsignedInteger;
    import com.serotonin.bacnet4j.util.PropertyValues;
    import com.serotonin.bacnet4j.util.ReadListener;
    import com.serotonin.bacnet4j.util.RequestUtils;
    
    /**
     * 启动Yabe的天气模拟
     * @author Jfei
     *
     */
    public class ReadTest01 {
    
    	/**
    	 * Yabe在本地电脑上启动
    	 * @param args
    	 * @throws Exception 
    	 */
    	public static void main(String[] args) throws Exception {
    		LocalDevice d = null;
    		try {
    			//创建网络对象
    			IpNetwork ipNetwork = new IpNetworkBuilder()
    					 .withLocalBindAddress("192.168.0.123")//本机的ip
    					 .withSubnet("255.255.255.0", 24)
    					 .withPort(47808) //Yabe默认的UDP端口
    					 .withReuseAddress(true)
    					 .build();
    			//创建虚拟的本地设备,deviceNumber随意
    			 d = new LocalDevice(123, new DefaultTransport(ipNetwork));
    			 d.initialize();
    			 d.startRemoteDeviceDiscovery();
    			 
    			 RemoteDevice rd = d.getRemoteDeviceBlocking(3);//获取远程设备,instanceNumber 是设备的device id
    			 
    			 System.out.println("modelName=" + rd.getDeviceProperty( PropertyIdentifier.modelName));
    			 System.out.println("analogInput2= " +RequestUtils.readProperty(d, rd, new ObjectIdentifier(ObjectType.analogInput, 2), PropertyIdentifier.presentValue, null));
    			 
    			 
    			 List<ObjectIdentifier> objectList =  RequestUtils.getObjectList(d, rd).getValues();
    			 
    			 //打印所有的Object 名称
    			 for(ObjectIdentifier o : objectList){
    				 System.out.println(o);
    			 }
    			 
    			
    			 ObjectIdentifier oid = new ObjectIdentifier(ObjectType.analogInput, 0);
    		     ObjectIdentifier oid1 = new ObjectIdentifier(ObjectType.analogInput, 1);
    		     ObjectIdentifier oid2 = new ObjectIdentifier(ObjectType.analogInput, 2);
    		     
    		     //获取指定的presentValue
    	         PropertyValues pvs = RequestUtils.readOidPresentValues(d, rd,Arrays.asList(oid,oid1,oid2), new ReadListener(){
    				@Override
    				public boolean progress(double progress, int deviceId,
    						ObjectIdentifier oid, PropertyIdentifier pid,
    						UnsignedInteger pin, Encodable value) {
    					System.out.println("========");
    					System.out.println("progress=" + progress);
    					System.out.println("deviceId=" + deviceId);
    					System.out.println("oid="+oid.toString());
    					System.out.println("pid="+pid.toString());
    					System.out.println("UnsignedInteger="+pin);
    					System.out.println("value="+value.toString() + "  getClass =" +value.getClass());
    					return false;
    				}
    	        	 
    	         });
    	         Thread.sleep(3000);
    	         System.out.println("analogInput:0 == " + pvs.get(oid, PropertyIdentifier.presentValue));
    	       //获取指定的presentValue
    	         PropertyValues pvs2 = RequestUtils.readOidPresentValues(d, rd,Arrays.asList(oid,oid1,oid2),null);
    	         System.out.println("analogInput:1 == " + pvs2.get(oid1, PropertyIdentifier.presentValue));
    	         
    	         d.terminate();
    		} catch (Exception e) {
    			e.printStackTrace();
    			if(d != null){
    				d.terminate();
    			}
    		}
    		
    	}
    }
    

      执行结果就不贴了,能正常读取到192.168.0.200上Yabe的数据。更改设备端数据也是一样可以的,就不贴代码了,参考上一篇博文。

    如果代码无法读取到数据,检查:

    1.服务器端(Yabe模拟器)和客户端(代码所在的电脑),IP是否属于同一个网段

    2.检查代码的withLocalBindAddress("192.168.1.150")(代码所在电脑的ip)和withSubnet("255.255.255.0", 24)是否填写正确。IP和subnet可以通过java代码获取(代码在最后贴出供参考)

    3.服务端。netstat -aon|findstr "47808",看是否通过端口47808进行UDP广播了。

    4.检查客户端47808是否有被占用的情况,netstat -aon|findstr "47808"

    5.客户端电脑上启动Yabe的客户端,检查47808是不是只有一个服务器设备

    6.执行代码时,如果客户端电脑上有运行Yabe客户端,为了避免干扰,则关闭Yabe客户端。然后运行代码

     

    java获取IP,subnet的范例

    package com.nanhe.common.util;
    
    import java.lang.management.ManagementFactory;
    import java.net.Inet4Address;
    import java.net.InetAddress;
    import java.net.InterfaceAddress;
    import java.net.NetworkInterface;
    import java.net.SocketException;
    import java.net.UnknownHostException;
    import java.util.Enumeration;
    import java.util.Set;
    
    import javax.management.MBeanServer;
    import javax.management.MalformedObjectNameException;
    import javax.management.ObjectName;
    import javax.management.Query;
    
    import org.slf4j.Logger;
    import org.slf4j.LoggerFactory;
    
    public class IpUtil {
    
    	private static Logger LOG = LoggerFactory.getLogger(IpUtil.class);
    	
    	/**
    	 * 获取本机IP地址
    	 * @return
    	 * @throws SocketException
    	 */
    	public static String getIpAddress() throws SocketException {
    	        String ipString = null;
    	        Inet4Address inet4Address  = getInet4Address();
    			if(inet4Address != null){
    				/*NetworkInterface networkInterface = NetworkInterface.getByInetAddress(inet4Address);
    	    		for (InterfaceAddress address : networkInterface.getInterfaceAddresses()) {
    	    			ipString = address.getAddress().getHostAddress();
    	    		}*/
    				ipString = inet4Address.getHostAddress();
    			}
    	    LOG.info("本机IP地址={}" , ipString);
    	    return ipString;
    	  }
    	
    	/**
    	 * 获取tomcat容器的http端口
    	 * @return
    	 * @throws MalformedObjectNameException 
    	 */
    	public static String getTomcatHttpPort() throws MalformedObjectNameException{
    		MBeanServer beanServer = ManagementFactory.getPlatformMBeanServer();
    		Set<ObjectName> objectNames = beanServer.queryNames(new ObjectName("*:type=Connector,*"),
                    Query.match(Query.attr("protocol"), Query.value("HTTP/1.1")));
            String port = objectNames.iterator().next().getKeyProperty("port");
            return port;
    	}
    	
    	/**
    	 * 获取网络前缀长度,
    	 * 如果长度为8,则表示掩码是255.0.0.0,
    	 * 如果长度为16,则表示掩码是255.255.0.0,
    	 * 如果长度为24,则表示掩码是255.255.255.0,
    	 * @return
    	 * @throws UnknownHostException 
    	 * @throws SocketException 
    	 */
    	public static int getNetworkPrefixLength() throws UnknownHostException, SocketException{
    		
    		int networkPrefixLength = 0;
    		Inet4Address inet4Address  = getInet4Address();
    		if(inet4Address != null){
    			NetworkInterface networkInterface = NetworkInterface.getByInetAddress(inet4Address);
        		for (InterfaceAddress address : networkInterface.getInterfaceAddresses()) {
        			if(address.getAddress() instanceof Inet4Address){
        				networkPrefixLength =  address.getNetworkPrefixLength();
        			}
        			
        		}
    		}
    		LOG.info("本机网络前缀长度networkPrefixLength={}" , networkPrefixLength);
    		return networkPrefixLength;
    	}
    	
    	/**
    	 * 获取网络掩码255.0.0.0,255.0.0.0,255.0.0.0,
    	 * @return
    	 * @throws UnknownHostException 
    	 * @throws SocketException 
    	 */
    	public static String getSubnet() throws UnknownHostException, SocketException{
    		String subnet = null;
    		int prefix = getNetworkPrefixLength();
    		if(prefix > 0){
    			if(prefix == 8){
    				subnet = "255.0.0.0";
    			}else if(prefix == 16){
    				subnet = "255.255.0.0";
    			}else if(prefix == 24){
    				subnet = "255.255.255.0";
    			}else if(prefix == 32){
    				subnet = "255.255.255.255";
    			}
    		}
    		return subnet;
    	}
    	
    	private static Inet4Address getInet4Address() throws SocketException{
    		 Inet4Address inet4Address = null;
    		 Enumeration<NetworkInterface> allNetInterfaces = NetworkInterface.getNetworkInterfaces();
    	      InetAddress ip = null;
    	      while (allNetInterfaces.hasMoreElements()) {
    	        NetworkInterface netInterface = (NetworkInterface) allNetInterfaces.nextElement();
    	        //用于排除回送接口,非虚拟网卡,未在使用中的网络接口.
    	        if (netInterface.isLoopback() || netInterface.isVirtual() || !netInterface.isUp()) {
    	          continue;
    	        } else {
    	          Enumeration<InetAddress> addresses = netInterface.getInetAddresses();
    	          while (addresses.hasMoreElements()) {
    	            ip = addresses.nextElement();
    	            if (ip != null && ip instanceof Inet4Address) {
    	            	inet4Address = (Inet4Address)ip;
    	                break;
    	            }
    	          }
    	          if(inet4Address != null){
    	        	  break;
    	          }
    	        }
    	      }
    	      return inet4Address;
    	}
    	
    	public static void main(String[] args) throws SocketException, UnknownHostException {
    		System.out.println(getIpAddress());
    	   System.out.println(getNetworkPrefixLength());
    	   System.out.println(getSubnet());
    	}
    	
    	
    }
    

     

    展开全文
  • 今天从一个人的blog看到的这个网站,貌似是本书的例子(貌似国外不流行带光盘,直接上网站了) http://www.exampledepot.com/ 记下来,以备用

    今天从一个人的blog看到的这个网站,貌似是本书的例子(貌似国外不流行带光盘,直接上网站了)

     

    http://www.exampledepot.com/

     

    记下来,以备用

    展开全文
  • 一个很有意思的java代码例子

    万次阅读 2012-01-29 09:43:10
    在群里看到一个代码比较有意思,保存下来。很有深度的讨论,随便发下。public class Monday { public static void main(String args[]) { 【place holder】 System.out.println("aaaa"); } } 问题:在括号内加...
  • 关键词: UML 关系 java 关系是事物之间的联系。类之间有4种重要的关系:依赖、泛化、关联和实现。这些关系都可以映射到代码里。1.依赖是一种使用关系,描述了一个事物的规格说明的变化可能会影响到使用它的另一个...
  • UML中类之间的关系及其Java代码例子

    千次阅读 2004-11-11 09:16:00
    [摘自 Java开发者 frenzied]关系是事物之间的联系。类之间有4种重要的关系:依赖、泛化、关联和实现。这些关系都可以映射到代码里。依赖是一种使用关系,描述了一个事物的规格说明的变化可能会影响到使用它的另一个...
  • java代码例子

    2011-11-02 15:16:07
    java代码例子java代码例子java代码例子
  • java例子代码

    2011-11-02 15:01:13
    java例子代码java例子代码java例子代码java例子代码
  • Java泛型代码例子

    2015-06-16 21:26:57
    Java泛型代码例子一篇代码胜过千言万语package com.apkkids.javalanguage;import java.util.ArrayList; import java.util.List;/** * @author wxb * Java泛型例子 */ public class GenericTest { //泛型接口 ...
  • Java代码例子全集

    2011-12-17 15:11:48
    Java代码例子全集: 其中包含所有Java代码小程序,例子,及源代码可供参考 包含很多小技术方面的实例,对开发有很大的帮助!
  • java代码例子

    2015-08-26 14:06:01
    http://www.codefans.net/sort/list_5_2.shtml
  • java web项目下载代码例子

    千次阅读 2015-12-15 11:24:34
    java web项目下载代码例子: 1.新建工具类:DownlaodUtils.java import java.io.BufferedInputStream; import java.io.BufferedOutputStream; import java.io.BufferedWriter; import java.io.File; import java.io....
  • java实现http接口服务端、客户端代码例子。使用jar(commons-codec-1.3.jar、commons-httpclient-3.0.1.jar、commons-logging-1.1.1.jar)
  • Java 语言基础例子代码,主要是一些很基础的Java例子,涉及了一些数组、数学算法、字符串等操作。代码文件一共有20多个,每一个代码文件都完成一种功能,对初学Java帮助很大。
  • 100+条JAVA例子代码

    2009-08-28 22:03:56
    100+条JAVA例子代码 100+条JAVA例子代码 100+条JAVA例子代码
  • java 23种设计模式代码例子 java设计模式
  • java实例源代码java实例源代码例子java实例源代码例子java实例源代码例子java实例源代码例子
  • MQTT推送简单例子服务端java代码实现

    千次下载 热门讨论 2014-04-25 13:53:04
    IBM的MQTT给了一个PHP的服务端代码,在网上找了一个JAVA实现服务端代码例子,调通了。如果想做这个例子需要自己用电脑建一个wifi,手机连上这个wifi,代码中要改几个个地方 1 android服务里有几个MQTT_HOST是ip...
  • 多态例子——Java代码

    千次阅读 2018-03-06 16:43:19
    /*多态的好处:提高了代码的维护性和扩展性;弊端:父类不能使用子类的特有功能。要用子类的特有功能,可以:A:创建子类对象,调用方法即可(不合理,太占内存);B:把父类的引用强制转换为子类的引用(向下转型)...
  • import java.util.concurrent.BlockingQueue; import java.util.concurrent.LinkedBlockingQueue; import java.util.concurrent.ThreadPoolExecutor; import java.util.concurrent.TimeUnit; public class ...
  • Java代码调用Sqlldr命令例子

    千次阅读 2017-03-28 14:36:52
    控制文件test.ctl LOAD DATA INFILE 'C:\case.csv' APPEND INTO TABLE T FIELDS TERMINATED BY ',' OPTIONALLY ENCLOSED BY '"' ( A CHAR, B CHAR ...数据文件case.csv ...import java.io.BufferedReader; i
  • Java数组相关的例子代码 Java数组相关的例子代码
  • Java的四种引用源代码例子不解释,直接上代码,千言万语顶不住一行代码。package com.apkkids.javalanguage; import java.lang.ref.PhantomReference; import java.lang.ref.Reference; import java.lang.ref....
  • 1.1面试受挫里面的样例,自己是个小菜,自学java中,写的代码有问题,请指导一下,让我更加进步,上代码。 package btdp.one.one; import java.util.Scanner; /** * Big talk design pattern 1.1 * desc: ...
  • java swing颜色选择器代码例子下载

    千次阅读 2015-04-27 10:34:50
    java swing颜色选择器代码例子下载 源代码下载地址:http://www.zuidaima.com/share/1550463730420736.htm

空空如也

空空如也

1 2 3 4 5 ... 20
收藏数 23,331
精华内容 9,332
关键字:

java代码例子

java 订阅