看你獲取的是win系統(tǒng)還是linux系統(tǒng)了:
讓客戶(hù)滿(mǎn)意是我們工作的目標(biāo),不斷超越客戶(hù)的期望值來(lái)自于我們對(duì)這個(gè)行業(yè)的熱愛(ài)。我們立志把好的技術(shù)通過(guò)有效、簡(jiǎn)單的方式提供給客戶(hù),將通過(guò)不懈努力成為客戶(hù)在信息化領(lǐng)域值得信任、有價(jià)值的長(zhǎng)期合作伙伴,公司提供的服務(wù)項(xiàng)目有:域名注冊(cè)、網(wǎng)站空間、營(yíng)銷(xiāo)軟件、網(wǎng)站建設(shè)、伊吾網(wǎng)站維護(hù)、網(wǎng)站推廣。
java執(zhí)行操作系統(tǒng)的網(wǎng)卡地址語(yǔ)句:
獲取機(jī)器名:
[java]?view plain?copy
public?String?getLocalHostName()?{
String?hostName;
try?{
InetAddress?addr?=?InetAddress.getLocalHost();
hostName?=?addr.getHostName();
}?catch?(Exception?ex)?{
hostName?=?"";
}
return?hostName;
}
獲取IP(多個(gè)網(wǎng)卡時(shí)獲取了多個(gè)IP):
[java]?view plain?copy
public?ListString?getNetworkAddress()?{
ListString?result?=?new?ArrayListString();
EnumerationNetworkInterface?netInterfaces;
try?{
netInterfaces?=?NetworkInterface.getNetworkInterfaces();
InetAddress?ip;
while?(netInterfaces.hasMoreElements())?{
NetworkInterface?ni?=?netInterfaces.nextElement();
EnumerationInetAddress?addresses=ni.getInetAddresses();
while(addresses.hasMoreElements()){
ip?=?addresses.nextElement();
if?(!ip.isLoopbackAddress()??ip.getHostAddress().indexOf(':')?==?-1)?{
result.add(ip.getHostAddress());
}
}
}
return?result;
}?catch?(Exception?e)?{
return?null;
}
}
java獲取外網(wǎng)ip地址方法:
public class Main {
public static void main(String[] args) throws SocketException {
System.out.println(Main.getRealIp());
}
public static String getRealIp() throws SocketException {
String localip = null;// 本地IP,如果沒(méi)有配置外網(wǎng)IP則返回它
String netip = null;// 外網(wǎng)IP
EnumerationNetworkInterface netInterfaces =
NetworkInterface.getNetworkInterfaces();
InetAddress ip = null;
boolean finded = false;// 是否找到外網(wǎng)IP
while (netInterfaces.hasMoreElements() !finded) {
NetworkInterface ni = netInterfaces.nextElement();
EnumerationInetAddress address = ni.getInetAddresses();
while (address.hasMoreElements()) {
ip = address.nextElement();
if (!ip.isSiteLocalAddress()
!ip.isLoopbackAddress()
ip.getHostAddress().indexOf(":") == -1) {// 外網(wǎng)IP
netip = ip.getHostAddress();
finded = true;
break;
} else if (ip.isSiteLocalAddress()
!ip.isLoopbackAddress()
ip.getHostAddress().indexOf(":") == -1) {// 內(nèi)網(wǎng)IP
localip = ip.getHostAddress();
}
}
}
if (netip != null !"".equals(netip)) {
return netip;
} else {
return localip;
}
}
}
1.得到局域網(wǎng)網(wǎng)段,可由自己機(jī)器的IP來(lái)確定 (也可以手動(dòng)獲取主機(jī)IP-CMD-ipconfig /all)
2.根據(jù)IP類(lèi)型,一次遍歷局域網(wǎng)內(nèi)IP地址
JAVA類(lèi),編譯之后直接運(yùn)行便可以得到局域網(wǎng)內(nèi)所有IP,具體怎樣使用你自己編寫(xiě)相應(yīng)代碼調(diào)用便可
代碼如下::
package bean;
import java.io.*;
import java.util.*;
public class Ip{
static public HashMap ping; //ping 后的結(jié)果集
public HashMap getPing(){ //用來(lái)得到ping后的結(jié)果集
return ping;
}
//當(dāng)前線(xiàn)程的數(shù)量, 防止過(guò)多線(xiàn)程摧毀電腦
static int threadCount = 0;
public Ip() {
ping = new HashMap();
}
public void Ping(String ip) throws Exception{
//最多30個(gè)線(xiàn)程
while(threadCount30)
Thread.sleep(50);
threadCount +=1;
PingIp p = new PingIp(ip);
p.start();
}
public void PingAll() throws Exception{
//首先得到本機(jī)的IP,得到網(wǎng)段
InetAddress host = InetAddress.getLocalHost();
String hostAddress = host.getHostAddress();
int k=0;
k=hostAddress.lastIndexOf(".");
String ss = hostAddress.substring(0,k+1);
for(int i=1;i =255;i++){ //對(duì)所有局域網(wǎng)Ip
String iip=ss+i;
Ping(iip);
}
//等著所有Ping結(jié)束
while(threadCount0)
Thread.sleep(50);
}
public static void main(String[] args) throws Exception{
Ip ip= new Ip();
ip.PingAll();
java.util.Set entries = ping.entrySet();
Iterator iter=entries.iterator();
String k;
while(iter.hasNext()){
Map.Entry entry=(Map.Entry)iter.next();
String key=(String)entry.getKey();
String value=(String)entry.getValue();
if(value.equals("true"))
System.out.println(key+"--"+value);
}
}
class PingIp extends Thread{
public String ip; // IP
public PingIp(String ip){
this.ip=ip;
}
public void run(){
try{
Process p= Runtime.getRuntime().exec ("ping "+ip+ " -w 300 -n 1");
InputStreamReader ir = new InputStreamReader(p.getInputStream());
LineNumberReader input = new LineNumberReader (ir);
//讀取結(jié)果行
for (int i=1 ; i 7; i++)
input.readLine();
String line= input.readLine();
if (line.length() 17 || line.substring(8,17).equals("timed out"))
ping.put(ip,"false");
else
ping.put(ip,"true");
//線(xiàn)程結(jié)束
threadCount -= 1;
}catch (IOException e){}
}
}
}
//獲取ip地址
public String getLocalIpAddress()
{
try {
String ipv4;
ArrayListNetworkInterface nilist = Collections.list(NetworkInterface.getNetworkInterfaces());
for (NetworkInterface ni: nilist)
{
ArrayListInetAddress ialist = Collections.list(ni.getInetAddresses());
for (InetAddress address: ialist)
{
if (!address.isLoopbackAddress() InetAddressUtils.isIPv4Address(ipv4=address.getHostAddress()))
{
return ipv4;
}
}
}
} catch (SocketException ex)
{
//Log.e(LOG_TAG, ex.toString());
}
return null;
}
方法如下:
方法一,使用CMD命令:
public static String getLocalIPForCMD(){
StringBuilder sb = new StringBuilder();
String command = "cmd.exe /c ipconfig | findstr IPv4";
try {
Process p = Runtime.getRuntime().exec(command);
BufferedReader br = new BufferedReader(new InputStreamReader(p.getInputStream()));
String line = null;
while((line = br.readLine()) != null){
line = line.substring(line.lastIndexOf(":")+2,line.length());
sb.append(line);
}
br.close();
p.destroy();
} catch (IOException e) {
e.printStackTrace();
}
return sb.toString();
}
方法二,使用Java方法:
public static String getLocalIPForJava(){
StringBuilder sb = new StringBuilder();
try {
EnumerationNetworkInterface en = NetworkInterface.getNetworkInterfaces();
while (en.hasMoreElements()) {
NetworkInterface intf = (NetworkInterface) en.nextElement();
EnumerationInetAddress enumIpAddr = intf.getInetAddresses();
while (enumIpAddr.hasMoreElements()) {
InetAddress inetAddress = (InetAddress) enumIpAddr.nextElement();
if (!inetAddress.isLoopbackAddress() !inetAddress.isLinkLocalAddress()
inetAddress.isSiteLocalAddress()) {
sb.append(inetAddress.getHostAddress().toString()+"\n");
}
}
}
} catch (SocketException e) { }
return sb.toString();
}
網(wǎng)頁(yè)地址在代碼中的java代碼寫(xiě)法如下:
packagecom.test;
importjava.lang.reflect.Method;
//實(shí)現(xiàn)打開(kāi)瀏覽器并跳到指定網(wǎng)址的類(lèi)
publicclassBareBonesBrowserLaunch{
publicstaticvoidopenURL(Stringurl){
try{
browse(url);
}catch(Exceptione){
}
}
privatestaticvoidbrowse(Stringurl)throwsException{
//獲取操作系統(tǒng)的名字
StringosName=System.getProperty("os.name","");
if(osName.startsWith("MacOS")){
//蘋(píng)果的打開(kāi)方式
ClassfileMgr=Class.forName("com.apple.eio.FileManager");
MethodopenURL=fileMgr.getDeclaredMethod("openURL",newClass[]{String.class});
openURL.invoke(null,newObject[]{url});
}elseif(osName.startsWith("Windows")){
//windows的打開(kāi)方式。
Runtime.getRuntime().exec("rundll32url.dll,FileProtocolHandler"+url);
}else{
//UnixorLinux的打開(kāi)方式
String[]browsers={"firefox","opera","konqueror","epiphany","mozilla","netscape"};
Stringbrowser=null;
for(intcount=0;countbrowsers.lengthbrowser==null;count++)
//執(zhí)行代碼,在brower有值后跳出,
//這里是如果進(jìn)程創(chuàng)建成功了,==0是表示正常結(jié)束。
if(Runtime.getRuntime().exec(newString[]{"which",browsers[count]}).waitFor()==0)
browser=browsers[count];
if(browser==null)
thrownewException("Couldnotfindwebbrowser");
else
//這個(gè)值在上面已經(jīng)成功的得到了一個(gè)進(jìn)程。
Runtime.getRuntime().exec(newString[]{browser,url});
}
}
}
//主方法測(cè)試類(lèi)
publicstaticvoidmain(String[]args){
Stringurl="";
BareBonesBrowserLaunch.openURL(url);
}