[英]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.