首页 | 编程语言 | 网站建设 | 游戏天堂 | 冲浪宝典 | 网络安全 | 操作系统 | 软件时空 | 硬件指南 | 病毒相关 | IT 认证
软讯网络 > 编程语言 > Java > 用java得到本机所有的ip地址
【标  题】:用java得到本机所有的ip地址
【关键字】:java,ip
【来  源】:http://blog.csdn.net/woowindice/archive/2006/04/18/667960.aspx

用java得到本机所有的ip地址

package com.sourceware.util;

import org.apache.commons.httpclient.methods.GetMethod;
import org.apache.commons.httpclient.HttpClient;
import org.apache.log4j.Logger;
import java.net.InetAddress;
import java.util.Enumeration;
import java.net.NetworkInterface;
import java.util.*;

/**
 *
 * <p>Title: Sourceware utils.</p>
 *
 * <p>Description: Sourceware utils.</p>
 *
 * <p>Copyright: Copyright (c) 2005</p>
 *
 * <p>Company: Sourceware inc.</p>
 *
 * @author 黑山(woowind@sina.com)
 * @version 1.0
 */
public class NetUtil {

  static Logger logger = Logger.getLogger(NetUtil.class);

  public NetUtil() {
  }

  /**
   *
   * @param url String
   * @return int
   * @throws Exception
   */
  public static int clickURL(String url) throws Exception {
    int retrycount = 3;
    while (true) {
      HttpClient httpClient = new HttpClient();
      GetMethod get = new GetMethod(url);
      get.addRequestHeader("Content-Type", "text/html; charset=GBK");
      int code = httpClient.executeMethod(get);
      if (code != 200) {
        if (retrycount == 0) {
          throw new Exception("发送失败,失败原因=" + code);
        }
        logger.error("send to[" + url + "]error code[" + code + "]");
        retrycount--;
      }
      else {
        return code;
      }

    } //end while(true)...
  }

  /**
   *
   * @return Collection
   */
  public static Collection getAllLocalIP() throws Exception {
    ArrayList ar = new ArrayList();
    Enumeration netInterfaces = NetworkInterface.getNetworkInterfaces();
    while (netInterfaces.hasMoreElements()) {
      NetworkInterface ni = (NetworkInterface) netInterfaces.nextElement();
      InetAddress ip = (InetAddress) ni.getInetAddresses().nextElement();
      if (!ip.isSiteLocalAddress() && !ip.isLoopbackAddress() &&
          ip.getHostAddress().indexOf(":") == -1) {
        System.out.println("Interface " + ni.getName() +
                           " seems to be InternetInterface. I'll take it..."); ;
      }
      else {
        ar.add(ip.getHostAddress());
      }
    }
    return ar;
  }

  public static void main(String[] args) {
    try {
      NetUtil.getAllLocalIP();
    }
    catch (Exception ex) {
      ex.printStackTrace();
    }
  }
}

使用Yahoo Service实现天气预报:【上一篇】
Apache-Tomcat的整合操作的英文介绍--用mod_jk2.so实现:【下一篇】
【相关文章】
  • 基于Eclipse UML2模型的Class digram (1)
  • a perl script automatic make a cppunit test case
  • 几个论坛签名显IP的代码
  • 轻松获得网通、电信和铁通的IP分配范围
  • 在linux上第一次配好了java运行环境
  • TCP/IP详解学习笔记(1)-基本概念
  • TCP/IP详解学习笔记(2)-数据链路层
  • PHP TIPS:关于状态函数应用file_exists()和clearstatcache();
  • 又做java培训讲师
  • A Summary of Principles for User-Interface Design.
  • 【随机文章】
  • Informix配置5.数据库的初始化
  • 以我与“黑客”的一段遭遇简单谈ASP的安全性
  • .Net 1.1下WEB引用Win控件的两个Bug
  • BootStar v7.33 keygen in pure win32asm(1)
  • 对Web页面元素的绝对唯一引用方法
  • 我拿什么来给你!--有感于用户选择Trufun而放弃pd
  • [专辑]# Linux字符安装oracle< SLES && ORACLE > (11月13日更新)
  • dojoTree Controller(1)
  • 自己动手用c#写控件(下)
  • 《设计模式精解》学习笔记(三)------设计模式开始
  • 【相关评论】
    没有相关评论
    【发表评论】
    姓名:
    邮件:
    随机码*
    评论*
          
    |  首 页  |  版权声明  |  联系我们   |  网站地图  |
    CopyRight © 2004-2007 软讯网络 All Rigths Reserved.