怎样把String型变成InetAddress

来源:百度知道 编辑:UC知道 时间:2024/06/04 17:23:25
address = InetAddress.getByAddress(adr);
因为adr是String,所以上句语句有问题。
怎样把String型的adr变成InetAddress型?
请各位高手帮帮忙!!!3Q

先把String型的adr转换为字节数组,即adr.getBytes();
因为jdk文档中getByAddress(byte[] addr)这个方法传递进来的是byte[]

即address = InetAddress.getByAddress(adr.getBytes());
楼主再试试

address = InetAddress.getByAddress(InetAddress.getByName(adr))

你试验一下
static InetAddress getByName(String host)


InetAddress.getByName(String)就行了
还有一个是
.getByAddress(byte[])
同时还有
.getLocalHost()
都是返回inetaddress