棒球比分lob:

MDA/MDD/TDD/DDD/DDDDDDD
posts - 536, comments - 111, trackbacks - 0, articles - 0
  BlogJava :: 首頁 :: 新隨筆 :: 聯系 :: 聚合  :: 管理

如何用 Java 獲取系統 IP?

Posted on 2015-03-02 06:29 leekiang 閱讀(488) 評論(0)  編輯  收藏 所屬分類: java
參考dubbo里的NetUtils類

import java.net.InetAddress;
import java.net.NetworkInterface;
import java.util.Enumeration;
import java.util.regex.Pattern;

public class GetIP {
public static void main(String[] args) {
System.out.println(GetIP.getLocalAddress0().getHostAddress());
}
public static InetAddress getLocalAddress0() {
??????? InetAddress localAddress = null;
??????? try {
??????????? localAddress = InetAddress.getLocalHost();
??????????? if (isValidAddress(localAddress)) {
??????????????? return localAddress;
??????????? }
??????? } catch (Throwable e) {
??????????? System.out.println("Failed to retriving ip address, " + e.getMessage());
??????? }
??????? try {
??????????? Enumeration<NetworkInterface> interfaces = NetworkInterface.getNetworkInterfaces();
??????????? if (interfaces != null) {
??????????????? while (interfaces.hasMoreElements()) {
??????????????????? try {
??????????????????????? NetworkInterface network = interfaces.nextElement();
??????????????????????? Enumeration<InetAddress> addresses = network.getInetAddresses();
??????????????????????? if (addresses != null) {
??????????????????????????? while (addresses.hasMoreElements()) {
??????????????????????????????? try {
??????????????????????????????????? InetAddress address = addresses.nextElement();
??????????????????????????????????? if (isValidAddress(address)) {
??????????????????????????????????????? return address;
??????????????????????????????????? }
??????????????????????????????? } catch (Throwable e) {
??????????????????????????????????? System.out.println("Failed to retriving ip address, " + e.getMessage());
??????????????????????????????? }
??????????????????????????? }
??????????????????????? }
??????????????????? } catch (Throwable e) {
??????????????????????? System.out.println("Failed to retriving ip address, " + e.getMessage());
??????????????????? }
??????????????? }
??????????? }
??????? } catch (Throwable e) {
??????????? System.out.println("Failed to retriving ip address, " + e.getMessage());
??????? }
??????? System.out.println("Could not get local host ip address, will use 127.0.0.1 instead.");
??????? return localAddress;
??? }
??? private static boolean isValidAddress(InetAddress address) {
??????? if (address == null || address.isLoopbackAddress())
??????????? return false;
??????? String name = address.getHostAddress();
??????? return (name != null
??????????????? && ! ANYHOST.equals(name)
??????????????? && ! LOCALHOST.equals(name)
??????????????? && IP_PATTERN.matcher(name).matches());
??? }
?? ?
??? public static final String LOCALHOST = "127.0.0.1";

??? public static final String ANYHOST = "0.0.0.0";
?? ?
??? private static final Pattern IP_PATTERN = Pattern.compile("\\d{1,3}(\\.\\d{1,3}){3,5}$");
}

{ganrao} pk10技巧 p62彩票开奖结果50期 广东36选7开奖结果今天晚 美国股市最新行情 配股怎么玩 贵州快3一定牛 河南福彩22选五预测号 pk10稳赢技巧 北京pk拾冠军五码计划 陕快乐十分开奖图 天津福彩快乐十分玩法 河南快三开奖历史结果 炒股心态 重庆快乐10分号码预测 上海快三诀窍 股票最低多少钱开户