[英]java.lang.NumberFormatException in System Groovy script
def ipadd = addr.hostAddress
//println ipadd
String myString = new Integer(ipadd);
def pa = new ParametersAction([new StringParameterValue('IPADDR', myString)]);
Thread.currentThread().executable.addAction(pa)
println 'Script finished! \n';
我正在尝试通过将奴隶的IP地址添加到系统变量中并将其传递给下一个作业来保存它的IP地址。但是当我运行该作业时,出现以下异常:日志:
Slave Machine 2: X.X.X.X
java.lang.NumberFormatException: For input string: "X.X.X.X"
at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65)
at java.lang.Integer.parseInt(Integer.java:580)
at java.lang.Integer.<init>(Integer.java:867)
IPv4地址中包含3个点,因此无法将其直接解析为Integer
。
我想您正在尝试将其转换为代表IP 32位的相应int
。 可以这样在Java中完成:
public static int ipToInt32(String ip) {
Inet4Address ipAddress;
try {
ipAddress = (Inet4Address) InetAddress.getByName(ip);
} catch (UnknownHostException e) {
throw new IllegalStateException("Cannot convert IP to bits: '" + ip + "'", e);
}
byte[] ipBytes = ipAddress.getAddress();
return ((ipBytes[0] & 0xFF) << 24)
| ((ipBytes[1] & 0xFF) << 16)
| ((ipBytes[2] & 0xFF) << 8)
| (ipBytes[3] & 0xFF);
}
您不能将ipadd强制转换为整数。 因为它不是有效的整数。 如我所见,您无需强制将ipadd强制转换为整数。 因此,我的建议是用以下行替换String myString = new Integer(ipadd)
行。
String myString = new String(ipadd)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.