クライアント側IPアドレスを取得
サンプル
MessageContext context = MessageContext.getCurrentMessageContext(); String ip = context.getProperty(MessageContext.REMOTE_ADDR).toString();
参考文献
http://d.hatena.ne.jp/kokuzawa/20091007/1254880496自IPアドレスを取得
サンプル1
try { InetAddress addr = InetAddress.getLocalHost(); String ip = addr.getHostAddress(); } catch (UnknownHostException e) { e.printStackTrace(); }
注意
* 上記のサンプル1のソースだと、環境によっては、 InetAddress.getLocalHost()で 例外「java.net.UnknownHostException」が発生することもある
参考文献
http://www.yukun.info/blog/2008/10/java-inetaddress-getlocalhost.htmlサンプル2
import java.net.Inet4Address; import java.net.InetAddress; import java.net.NetworkInterface; import java.net.SocketException; import java.util.Enumeration; public class SampleIpPrinter { public static void main(String[] args) { try { Enumeration<NetworkInterface> networkInterfaces = NetworkInterface.getNetworkInterfaces(); while (networkInterfaces.hasMoreElements()) { NetworkInterface networkInterface = networkInterfaces.nextElement(); Enumeration<InetAddress> inetAddresses = networkInterface.getInetAddresses(); while (inetAddresses.hasMoreElements()) { InetAddress inetAddress = inetAddresses.nextElement(); if (!inetAddress.getHostAddress().equals("127.0.0.1") && inetAddress instanceof Inet4Address) System.out.println(inetAddress.getHostAddress()); } } } catch (SocketException ex) { ex.printStackTrace(); } } }
参考文献
http://www.yukun.info/blog/2010/05/java-networkinterface-ipv6-ipv4.htmlhttp://d.hatena.ne.jp/anagotan/20130912/1378967802