java ip 国家_java通过ip获取用户所在国家 | 学步园

本文介绍了如何使用Java实现通过IP地址获取用户所在国家的功能。首先讲解了GeoIP的概念,包括免费和付费版本的区别。接着,展示了如何下载和安装GeoIP数据库文件,以及如何使用Java代码进行调用。提供了CountryLookupService类的详细实现,该类能够根据IP地址返回对应的国家代码和名称。最后,给出了一个测试示例,演示了如何获取特定IP地址的国家信息。
摘要由CSDN通过智能技术生成

什么是GeoIP ?

所谓GeoIP,就是通过来访者的IP, 定位他的经纬度,国家/地区,省市,甚至街道等位置信息的一个数据库。GeoIP有两个版本,一个免费版,一个收费版本。收费版本的准确率和数据更好一些。

GeoIP如何使用?

GeoIP支持多种语言调用,这里我们以java为例。

执行下面的命令:

wget -N -q http://geolite.maxmind.com/download/geoip/database/GeoLiteCountry/GeoIP.dat.gz

gzip -d GeoIP.dat.gz

cp GeoIP.dat   /usr/local/share/GeoIP/GeoIP.dat

具体执行代码:

package com.test.intl.commons.ip;

/**

* 这个类封装了对国家的数据。 类Country.java的实现描述:TODO 类实现描述

*

*/

public class Country {

private String code;

private String name;

/**

* Creates a new Country.

*

* @param code the country code.

* @param name the country name.

*/

public Country(String code, String name) {

this.code = code;

this.name = name;

}

/**

* Returns the ISO two-letter country code of this country.

*

* @return the country code.

*/

public String getCode() {

return code;

}

/**

* Returns the name of this country.

*

* @return the country name.

*/

public String getName() {

return name;

}

}

package com.test.intl.commons.ip;

import java.io.File;

import java.io.IOException;

import java.net.InetAddress;

import java.util.HashMap;

import org.apache.log4j.Logger;

import sun.net.util.IPAddressUtil;

import com.maxmind.geoip.DatabaseInfo;

public class CountryLookupService {

/**

* 这个是数据文件的。

*/

private File                                  databaseFile              = null;

/**

* buffer最大的数据。

*/

private int                                   maxBuffer                 = 0;

/**

* 判断是否进行了初始化工作。

*/

private boolean                               isInit                    = false;

//

private static Logger                         log                       = Logger.getLogger(CountryLookupService.class);

/**

* 每次进行check的时间间隔,默认是1分钟一次。

*/

private int                                   checkfreshInvertal        = 60 * 1000;

/**

* 最后一次做check的时间间隔。

*/

private long                                  lastcheckfreshTime        = 0;

/**

* Database file.

*/

private SafeBufferRandomAccessFile            file                      = null;

/**

* Information about the database.

*/

// private DatabaseInfo databaseInfo = null;

/**

* The database type. Default is the country edition.

*/

byte                                          databaseType              = DatabaseInfo.COUNTRY_EDITION;

int                                           databaseSegments[];

int                                           recordLength;

String                                        licenseKey;

int                                           dnsService                = 0;

private final static int                      COUNTRY_BEGIN             = 16776960;

private final static int                      STATE_BEGIN               = 16700000;

private final static int                      STRUCTURE_INFO_MAX_SIZE   = 20;

// private final static int DATABASE_INFO_MAX_SIZE = 100;

private final static int                      SEGMENT_RECORD_LENGTH     = 3;

private final static int                      STANDARD_RECORD_LENGTH    = 3;

private final static int                      ORG_RECORD_LENGTH         = 4;

private final static int                      MAX_RECORD_LENGTH         = 4;

// private final static int MAX_ORG_RECORD_LENGTH = 300;

// private final static int FULL_RECORD_LENGTH = 50;

public static final Country                   UNKNOWN_COUNTRY           = new Country("--", "N/A");

private final static HashMap hashmapcountryCodetoindex = new HashMap(512);

private final static HashMap hashmapcountryNametoindex = new HashMap(512);

private final static String[]                 countryCode               = { "--", "AP", "EU", "AD", "AE", "AF",

"AG", "AI", "AL", "AM", "AN", "AO", "AQ", "AR", "AS", "AT", "AU", "AW", "AZ", "BA", "BB", "BD", "BE", "BF",

"BG", "BH", "BI", "BJ", "BM", "BN", "BO", "BR", "BS", "BT", "BV", "BW", "BY", "BZ", "CA", "CC", "CD", "CF",

"CG", "CH", "CI", "CK", "CL", "CM", "CN", "CO", "CR", "CU", "CV", "CX", "CY", "CZ", "DE", "DJ", "DK", "DM",

"DO", "DZ", "EC", "EE", "EG", "EH", "ER", "ES", "ET", "FI", "FJ", "FK", "FM", "FO", "FR", "FX", "GA", "GB",

"GD", "GE", "GF", "GH", "GI", "GL", "GM", "GN", "GP", "GQ", "GR", "GS", "GT", "GU", "GW", "GY", "HK", "HM",

"HN", "HR", "HT", "HU", "ID", &

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
是的,这篇文章介绍了如何使用Java编写爬虫程序来获取邮件信息。这里提供了一个完整的示例代码,可以帮助你更好地理解如何实现邮件爬虫。 以下是示例代码: ```java import java.io.IOException; import java.util.Properties; import javax.mail.Address; import javax.mail.Folder; import javax.mail.Message; import javax.mail.MessagingException; import javax.mail.Session; import javax.mail.Store; import javax.mail.internet.InternetAddress; public class EmailCrawler { public static void main(String[] args) throws MessagingException, IOException { // 邮箱账号配置 String host = "pop3.example.com"; String username = "your_email@example.com"; String password = "your_password"; // 连接到邮件服务器 Properties props = new Properties(); props.setProperty("mail.store.protocol", "pop3"); props.setProperty("mail.pop3.host", host); Session session = Session.getDefaultInstance(props); Store store = session.getStore(); store.connect(username, password); // 获取收件箱 Folder inbox = store.getFolder("INBOX"); inbox.open(Folder.READ_ONLY); // 遍历邮件 Message[] messages = inbox.getMessages(); for (Message message : messages) { Address[] froms = message.getFrom(); String from = ((InternetAddress) froms[0]).getAddress(); String subject = message.getSubject(); String date = message.getSentDate().toString(); System.out.printf("From: %s%nSubject: %s%nDate: %s%n", from, subject, date); System.out.println("------------------------"); } // 关闭连接 inbox.close(false); store.close(); } } ``` 在上面的代码中,我们使用JavaMail API连接到指定的POP3邮件服务器,并获取收件箱。然后,我们遍历所有邮件,并提取出邮件的发件人、主题和日期。最终,我们打印出每个邮件的相关信息。 在使用时,我们需要将代码中的`host`、`username`和`password`替换为自己的邮箱账号信息。同时,我们还需要将代码中的`pop3.example.com`替换为自己的POP3邮件服务器地址。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值