更改本地Ip

发布时间:2014-10-23 23:27:39
来源:分享查询网

public class Execute {     public static boolean setIP(int p) throws Exception//设置IP   {    p++;    if(p>255){     p=0;    }else if(p<0){     System.out.println("不可低于0");     return false;    }    String ip = String.valueOf(p);    Runtime.getRuntime().exec("netsh interface ip set addr \"本地连接\" static "+"192.168.0."+ip+" 255.255.255.0 192.168.0.19 1");    Thread.sleep(5000);    if(LinkCl.linkcl()){    //  判断连接成功与否     System.out.println("链接成功");     return true;    }else{    System.out.println(p);    System.out.println("重新设置");    setIP(p);        }    return false;       }            public static int getIP() throws UnknownHostException{//获取IP   String ipg = InetAddress.getLocalHost().getHostAddress();   System.out.println(ipg);   String[] irr= ipg.split("\\.");//取最后一位    int ir4 = Integer.parseInt(irr[3]);    return ir4;   } --------------------------------------------------------------------------------------------------------------------------------------------------------------------- public class LinkCl {    public static boolean linkcl(){          try {          URL url = new URL("http://baidu.com");             try {                 InputStream in = url.openStream();                 in.close();                 System.out.println("网络连接正常!");                return true;            } catch (IOException e) {                 System.out.println("网络连接失败!");                                return false;            }         } catch (MalformedURLException e) {             e.printStackTrace();            return false;        }          }      }

返回顶部
查看电脑版