JSP

 

GetIpAddress

package config;

import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.util.Enumeration;

public class GetIpAddress {
	
	public static String getIp(){
		String ip = null;
		try {
			

			boolean isLoopBack = true;
			Enumeration<NetworkInterface> en;		
			en = NetworkInterface.getNetworkInterfaces();

			while(en.hasMoreElements()) {
				NetworkInterface ni = en.nextElement();
				if (ni.isLoopback())
					continue;

				Enumeration<InetAddress> inetAddresses = ni.getInetAddresses();
				while(inetAddresses.hasMoreElements()) { 
					InetAddress ia = inetAddresses.nextElement();
					if (ia.getHostAddress() != null && ia.getHostAddress().indexOf(".") != -1) {
						ip = ia.getHostAddress();
						System.out.println(ip);
						isLoopBack = false;
						break;
					}
				}
				if (!isLoopBack)
					break;
			}
			
		} catch (SocketException e) {
			e.printStackTrace();
		}
		return ip;
	}
	
}

 

 

 

 

 

about author

PHRASE

Level 60  라이트

자기가 나설 무대가 아닌 곳에 함부로 나서지 말라. 세계에는 빈 곳이 얼마든지 있다. 어디에나 함부로 나서는 사람은 대개 자기의 능력이 없는 자이기도 하고, 자기의 천직 을 자각하고 있지 못한 자이기도 하다. -입센

댓글 ( 4)

댓글 남기기

작성