servlets - Java - How to detect IP version -
i'm getting client
ip address via below method :
public static string getclientipaddr(httpservletrequest request) { string ip = request.getheader("x-forwarded-for"); ... return ip }
now want detect if ipv4
or ipv6
.
you create inetaddress , check if became ipv4 or ipv6 instance
inetaddress address = inetaddress.getbyname(ip); if (address instanceof inet6address) { // it's ipv6 } else if (address instanceof inet4address) { // it's ipv4 }
it seems bit awkward, though, , hope there better solution.
Comments
Post a Comment