본문 바로가기

JAVA / JSP

자바(java) 클라이언트 IP 확인


import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.StringTokenizer;

public class LocalHostTest
{
    public static void main( String[] args )
    {
        try
        {
            final String localhost = InetAddress.getLocalHost( ).toString( );
            final StringTokenizer st = new StringTokenizer( localhost , "/" );
            final String host = st.nextToken( );
            final String ip = st.nextToken( );
            System.out.println( host );
            System.out.println( ip );
        }
        catch ( UnknownHostException e )
        {
            e.printStackTrace( );
        }
    }
}

결과 >

ProWin7-PC
211.xxx.xx.xx

 


사설 아이피 확인하기

String localhost = null;
try
{
 localhost = InetAddress.getLocalHost( ).toString( );
}
catch ( UnknownHostException e )
{
 e.printStackTrace( );
}
final StringTokenizer st = new StringTokenizer( localhost , "/" );
st.nextToken( ); // host
final String ip = st.nextToken( );
if ( ip.startsWith( "192." ) || ip.startsWith( "172." ) || ip.startsWith( "10." ) )
{
 System.out.println( "공유기를 통한 사설아이피 사용" );
}