본문

IPv6 Check 로직(JAVA)

반응형

# IPv6 Check 로직

To check IPv6 Format


Source 01) 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
package com.test.test;
 
import java.util.regex.Pattern;
 
import com.googlecode.ipv6.IPv6Address;
 
public class IPUtil {
    public static String format(String ip) {
        if (ip != null && checkIPv6(ip))
            return IPv6Address.fromString(ip).toString().toUpperCase();
        else
            return ip;
    }
 
    public static boolean checkIPv6(String ip) {
        String regexIPv6 = "^(([0-9a-fA-F]{1,4}:){7,7}[0-9a-fA-F]{1,4}|([0-9a-fA-F]{1,4}:){1,7}:|([0-9a-fA-F]{1,4}:){1,6}:[0-9a-fA-F]{1,4}|([0-9a-fA-F]{1,4}:){1,5}(:[0-9a-fA-F]{1,4}){1,2}|([0-9a-fA-F]{1,4}:){1,4}(:[0-9a-fA-F]{1,4}){1,3}|([0-9a-fA-F]{1,4}:){1,3}(:[0-9a-fA-F]{1,4}){1,4}|([0-9a-fA-F]{1,4}:){1,2}(:[0-9a-fA-F]{1,4}){1,5}|[0-9a-fA-F]{1,4}:((:[0-9a-fA-F]{1,4}){1,6})|:((:[0-9a-fA-F]{1,4}){1,7}|:)|fe80:(:[0-9a-fA-F]{0,4}){0,4}%[0-9a-zA-Z]{1,}|::(ffff(:0{1,4}){0,1}:){0,1}((25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9]).){3,3}(25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])|([0-9a-fA-F]{1,4}:){1,4}:((25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9]).){3,3}(25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9]))$";
        Pattern pattern = Pattern.compile(regexIPv6);
        return pattern.matcher(ip).matches();
    }
}
 
cs


Source 02) Main.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
package com.test.test;
 
public class Main {
 
    public static void main(String[] args) {
        String ipv6 = "Fd11:90F2:a0:1900:b:1001:0:62";
 
        System.out.println(IPUtil.checkIPv6(ipv6));     // IPv6 Check
        System.out.println(IPUtil.format(ipv6));         // Convert to upper-case
 
    }
 
}
 
cs


Result)

1
2
true
FD11:90F2:A0:1900:B:1001:0:62
cs



Environment)

1
2
3
4
5
<dependency>
        <groupId>com.googlecode.java-ipv6</groupId>
        <artifactId>java-ipv6</artifactId>
        <version>0.16</version>
</dependency>
cs


1
2
3
4
5
6
7
8
9
10
<repositories>
    <repository>
        <id>Maven repository</id>
        <url>http://repo2.maven.org/maven2/</url>
    </repository>
    <repository>
        <id>mesir-repo</id>
        <url>http://mesir.googlecode.com/svn/trunk/mavenrepo</url>
    </repository>
</repositories>
cs


반응형

공유

댓글