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地址。但是当我运行该作业时,我会低于例外:日志:

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)

最新更新