0

I try to connect using FTPClient class. This code runs perfectly:

client.connect("172.20.5.131");

But I can not connect when ip address looks like -1406991772. How can I sole this?

Thank you very much!

user3649515
  • 199
  • 7
  • 17
  • possible duplicate of [Java: convert int to InetAddress](http://stackoverflow.com/questions/1957637/java-convert-int-to-inetaddress) – Scary Wombat Jul 02 '14 at 08:24

1 Answers1

2

To do so, First you have to convert the integer value of IP-Address into a valid IP-Address using InetAddress#getByAddress(byte[]) :

int ipAddress = -1406991772;
byte[] bytes = BigInteger.valueOf(ipAddress).toByteArray();
String address = InetAddress.getByAddress(bytes).getHostAddress();

and then pass the address to client.connect(address);

earthmover
  • 4,395
  • 10
  • 43
  • 74