1.support foreign coutnry

main
Gary 11 months ago
parent 859a425566
commit 9e0ad0f560

@ -4,6 +4,8 @@ import org.lionsoul.ip2region.xdb.Searcher;
import org.springframework.core.io.ClassPathResource;
import org.springframework.util.StreamUtils;
import constants.Constants;
import java.io.InputStream;
public class IpUtil {
@ -23,7 +25,11 @@ public class IpUtil {
public static String getIpLocation(String ip) {
try {
return searcher.search(ip).split("\\|")[2].replaceFirst("省", "");
String ipLocation = searcher.search(ip);
if (ipLocation.startsWith(Constants.IP_LOCATION_CHINA)) {
return ipLocation.split("\\|")[2].replaceFirst("省", "");
}
return ipLocation.split("\\|")[0];
} catch (Exception e) {
return UNKNOW_IP;
}

@ -59,4 +59,6 @@ public class Constants {
public static final Integer LENGTH_150 = 150;
public static final Integer LENGTH_512 = 512;
public static final String IP_LOCATION_CHINA = "中国";
}

@ -5,6 +5,8 @@ import org.lionsoul.ip2region.xdb.Searcher;
import org.springframework.core.io.ClassPathResource;
import org.springframework.util.StreamUtils;
import constants.Constants;
public class IpUtil {
private static final String IP2REGION_DB_FILE_PATH = "ip2region.xdb";
private static final String UNKNOW_IP = "未知IP";
@ -22,7 +24,11 @@ public class IpUtil {
public static String getIpLocation(String ip) {
try {
return searcher.search(ip).split("\\|")[2].replaceFirst("省", "");
String ipLocation = searcher.search(ip);
if (ipLocation.startsWith(Constants.IP_LOCATION_CHINA)) {
return ipLocation.split("\\|")[2].replaceFirst("省", "");
}
return ipLocation.split("\\|")[0];
} catch (Exception e) {
return UNKNOW_IP;
}

@ -10,6 +10,9 @@ public class IpUtilTest {
public void test() {
String ip="121.8.215.106";
System.out.println(IpUtil.getIpLocation(ip));
String ip2="38.103.161.182";
System.out.println(IpUtil.getIpLocation(ip2));
}
}

Loading…
Cancel
Save