| 導購 | 订阅 | 在线投稿
分享
 
 
 

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

來源:互聯網網民  2006-04-20 05:58:45  評論

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();

}

}

}

 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
 
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 黑山([url=mailto:woowind@sina.com]woowind@sina.com[/url]) * @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(); } } }
󰈣󰈤
王朝萬家燈火計劃
期待原創作者加盟
 
 
 
>>返回首頁<<
 
 
 
 
 
 熱帖排行
 
 
靜靜地坐在廢墟上,四周的荒凉一望無際,忽然覺得,淒涼也很美
© 2005- 王朝網路 版權所有