JavaFX校验IP和端口的合法性
我们人这一辈子不是别人的楷模,就是别人的借鉴。
今天项目添加了一些功能,主要实现网络的一些设置,如图:

主要有几个文本框和两个按钮,这两个按钮一个是查看
btnInternetCheck.setOnAction((ActionEvent event) -> {//查看
int vcode = Datastruct.armmV * 100 * 100 + Datastruct.armsV * 100 + Datastruct.armdV;
if(vcode>=8*10000+100+5){//版本大于8.1.5可以使用
List<Integer> gi = prtl.rdGatewayIP();
List<Integer> sm = prtl.rdSubMask();
List<Integer> ia = prtl.rdIPAddr();
List<Integer> sp = prtl.rdS0Port();
List<Integer> pa = prtl.rdPhyAddr();
tfGatewayIP.setText(""+gi.get(0) + "." + gi.get(1) + "." + gi.get(2) + "." + gi.get(3));
tfSubnetMask.setText(""+sm.get(0) + "." + sm.get(1) + "." + sm.get(2) + "." + sm.get(3));
tfLocalIP.setText(""+ia.get(0) + "." + ia.get(1) + "." + ia.get(2) + "." + ia.get(3));
tfPortNumber.setText("" + (((sp.get(0)&0xff)<<8)+((sp.get(1))&0xff)));
tfPhysicalLocation.setText(""+pa.get(0) + "." + pa.get(1) + "." + pa.get(2) + "." + pa.get(3) + "." + pa.get(4) + "." + pa.get(5));
}
});
一个是修改
btnInternetModify.setOnAction((ActionEvent event) -> {//修改
boolean flag=false;
String gatewayIP = tfGatewayIP.getText();
String subnetMask = tfSubnetMask.getText();
String localIP = tfLocalIP.getText();
String portNumber = tfPortNumber.getText();
String physicalLocation = tfPhysicalLocation.getText();
if(isIp(gatewayIP)){//是IP地址
List<Integer> list=convertListByString(gatewayIP);//转换
prtl.setGateway_IP(list.get(0), list.get(1), list.get(2), list.get(3));//设值
flag=true;
}else{//提示信息
showWaringDialog(null, strGatewayIPError, strInfoError, Global.strInfoOk);
flag=false;
return;
}
if(isIp(subnetMask)){//是IP地址
List<Integer> list=convertListByString(subnetMask);//转换
prtl.setSub_Mask(list.get(0), list.get(1), list.get(2), list.get(3));//设值
flag=true;
}else{//提示信息
showWaringDialog(null, strSubnetMaskError, strInfoError, Global.strInfoOk);
flag=false;
return;
}
if(isIp(localIP)){//是IP地址
List<Integer> list=convertListByString(localIP);//转换
prtl.setIP_Addr(list.get(0), list.get(1), list.get(2), list.get(3));//设值
flag=true;
}else{//提示信息
showWaringDialog(null, strLocalIPError, strInfoError, Global.strInfoOk);
flag=false;
return;
}
if(isPort(portNumber)){
int port = getPort(portNumber);//获取修改后的端口
if(port>=1000&&port<=65535){
int portH = (port >> 8) & 0xff; //高位
int portL = (port) & 0xff; //低位
prtl.SetS0_Port(portH, portL);
flag = true;
}else{
showWaringDialog(null, strPortError, strInfoError, Global.strInfoOk);
flag = false;
return;
}
}else{
showWaringDialog(null, strPortError, strInfoError, Global.strInfoOk);
flag=false;
return;
}
if(isPhysicalLocation(physicalLocation)){//是物理地址
List<Integer> list=convertListByString(physicalLocation);//转换
prtl.setPhy_Addr(list.get(0), list.get(1), list.get(2), list.get(3),list.get(4),list.get(5));//设值
flag=true;
}else{//提示信息
showWaringDialog(null, strPhyError, strInfoError, Global.strInfoOk);
flag=false;
return;
}
if(flag){
showMessageDialog(null, strModifySuccess, strInfoDone, Global.strInfoOk);
}
});
}
从上面代码我们可以看出主要有几个校验是否合格的方法isIP(String)、isPort(String)、isPhysicalLocation(String)和转换字符串(IP)成List集合的方法convertListByString(String),代码如下
boolean isIp(String IP){//判断是否是一个IP boolean b = false; IP = trimSpaces(IP); if(IP.matches("\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}")){ //不能让用户输入字母和符号,只能是数字 String s[] = IP.split("\\."); if(Integer.parseInt(s[0])<=255) if(Integer.parseInt(s[1])<=255) if(Integer.parseInt(s[2])<=255) if(Integer.parseInt(s[3])<=255) b = true; } return b; }
boolean isPort(String port){//判断是否是端口并返回端口号 boolean flag=false; port=trimSpaces(port); int p=5000; try { p=Integer.parseUnsignedInt(port); flag=true; } catch (Exception e) { flag=false; } return flag; }
boolean isPhysicalLocation(String pl){//判断是否是一个物理地址 boolean b = false; pl = trimSpaces(pl); if(pl.matches("\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}")){ //不能让用户输入字母和符号,只能是数字 String s[] = pl.split("\\."); if(Integer.parseInt(s[0])<=255) if(Integer.parseInt(s[1])<=255) if(Integer.parseInt(s[2])<=255) if(Integer.parseInt(s[3])<=255) if(Integer.parseInt(s[4])<=255) if(Integer.parseInt(s[5])<=255) b = true; } return b; }
List<Integer> convertListByString(String IPstr){//转换IP为4个数字存于List集合中
String rx="\\.";// '.' 是转义字符
IPstr=trimSpaces(IPstr);
String[] result=IPstr.split(rx);
List<Integer> list=new ArrayList();
for (int i = 0; i < result.length; i++) {
list.add(Integer.parseInt(result[i]));
}
return list;
}
我么可以看出中间用到了trimSpaces(String)方法来去掉字符串前后的所有空格
String trimSpaces(String IPstr){//去掉IP字符串前后所有的空格
String IP=new String(IPstr);
while(IP.startsWith(" ")){
IP= IP.substring(1,IP.length()).trim();
}
while(IP.endsWith(" ")){
IP= IP.substring(0,IP.length()-1).trim();
}
return IP;
}
通过上述操作我们就可以实现校验IP和端口的合法性,下次见!

浙公网安备 33010602011771号