Get country from IP address (Java)

■ Reference



public class InetAddressLookUpMain {

 // see
 static String baseHost = "";

 static String[] locales = "JP,US,KR,CH,HK".split(",");

 public static String getCountry(String s) throws UnknownHostException {

 InetAddress inetAddr = InetAddress.getByName(s); // throws
 // UnknownHostException

 byte[] byteAddr = inetAddr.getAddress();

 StringBuffer sb = new StringBuffer();

 for (int i = (byteAddr.length - 1); i >= 0; i--) {
 int unsignedByte = byteAddr[i] < 0 ? byteAddr[i] + 256
 : byteAddr[i];
 sb.append(unsignedByte + ".");

 for (int n = 0; n < locales.length; n++) {

 String country = locales[n];

 String lookupHost = sb.toString() + country.toLowerCase() + "."
 + baseHost;

 try {
 return (locales[n]);
 } catch (UnknownHostException e) {
 // e.printStackTrace();


 return null;

 public static void main(String[] args) throws Exception {


Exception in thread "main" aho: aho

Recommended Posts

Get country from IP address (Java)
Find the address class and address type from the IP address with Java
Get "2-4, 7, 9" from [4, 7, 9, 2, 3]
Get caller information from stack trace (java)
[Java] Get tag information from music files
Get history from Zabbix server in Java
Find the address class and address type from the IP address with Java [No. 2 decoction]
I want to get the IP address when connecting to Wi-Fi in Java
Translate IP address
How to get Class from Element in Java
Get unixtime (seconds) from ZonedDateTime in Scala / Java
[Java] Get multiple values from one return value
[Java] Get a random value from an array
[Java] Get KFunction from Method / Constructor in Java [Kotlin]
Call Java from JRuby
CentOS IP address Setup
Changes from Java 8 to Java 11
Sum from Java_1 to 100
[Java] Get miscellaneous dates
Eval Java source from Java
Access API.AI from Java
[Java] Initialize, add, get
From Java to Ruby !!
[Kotlin] How to get IP address and user agent
Get weather forecasts from Watson Weather Company Data in simple Java
Get to the abbreviations from 5 examples of iterating Java lists
[Java] Get Charset with Apathce Tika / Initialize String from Charset [Kotlin]
[Kotlin] Get Java Constructor / Method from KFunction and call it
Easily get an integer from a system property in Java
Get attributes and values from an XML file in Java
CData Software Hands-on (Get Twitter data from Java console application)
Get a non-empty collection from an Optional stream in java
Migration from Cobol to JAVA
Get EXIF information in Java
Java starting from beginner, override
Creating ElasticSearch index from Java
New features from Java7 to Java8
Connect from Java to PostgreSQL
[Java] Get KClass in Java [Kotlin]
Java, instance starting from beginner
Java starting from beginner, inheritance
Java life starting from scratch
Using Docker from Java Gradle
From Ineffective Java to Effective Java
JavaScript as seen from Java
Execute non-Java instructions from Java
[Java] Get data from DB using singleton service in Spring (Boot)
[Java] How to convert from String to Path type and get the path