java本機ip地址
⑴ java如何實現對本機的ip地址 網關地址 子網
提供一種可行的方法。供你參考。思路是利用操作系統的shell,執行相版應的命令。
以下以WINDOW操作系統權為例。LINUX的思路相同。
1,在E;下建立如下兩個bat文件,內容分別如下:
e:setip1.bat文件內容:
rem設置IP、子網掩碼、默認網關
c:
cd
netshexece:setip.bat
另一個文件e:setip.bat文件內容:
interface
ip
setaddress"本地連接"static192.168.1.111255.255.255.0192.168.1.1
exit
2,執行腳本命令的JAVA程序
⑵ java中怎樣實現修改本機IP
一般都是用代理也就是跳板機來實現
⑶ java中如何過去本機ip地址並輸出
InetAddress addr = InetAddress.getLocalHost();
ip=addr.getHostAddress().toString;//獲得本機IP
address=addr.getHostName()toString;//獲得本機名稱
⑷ JAVA與本機IP
支持Windows,Linux, 支持多網卡(根據網卡取本機配置的IP) /** * 根據網卡取本機配置的IP * 如果是雙網卡的,則取出外網IP * @return */ public static String getIp(){ String localip=null;//本地IP,如果沒有配置外網IP則返回它 String netip=null;//外網IP try { Enumeration<NetworkInterface> netInterfaces =
NetworkInterface.getNetworkInterfaces(); InetAddress ip = null; boolean finded=false;//是否找到外網IP while(netInterfaces.hasMoreElements() && !finded){ NetworkInterface ni=netInterfaces.nextElement(); Enumeration<InetAddress> address=ni.getInetAddresses(); while(address.hasMoreElements()){ ip=address.nextElement(); System.out.println(ni.getName() +";"+ip.getHostAddress() +";ip.isSiteLocalAddress()="+ip.isSiteLocalAddress()+";ip.isLoopbackAddress()="+ip.isLoopbackAddress()); if( !ip.isSiteLocalAddress() && !ip.isLoopbackAddress()
&& ip.getHostAddress().indexOf(":")==-1){//外網IP netip=ip.getHostAddress(); finded=true; break; }else if(ip.isSiteLocalAddress() && !ip.isLoopbackAddress()
&& ip.getHostAddress().indexOf(":")==-1){//內網IP localip=ip.getHostAddress(); } } } } catch (SocketException e) { e.printStackTrace(); } if(netip!=null && !"".equals(netip)){ return netip; }else{ return localip; } }
⑸ Java如何獲取本地計算機的IP地址和主機名
可以使用 InetAddress.getLocalHost(),代碼如下:
importjava.net.*;
publicclassApp{
publicstaticvoidmain(String[]args)throwsUnknownHostException{
InetAddresslocal=InetAddress.getLocalHost();
System.out.println("計算機名:"+local.getHostName());
System.out.println("IP:"+local.getHostAddress());
}
}
⑹ 如何用java獲取本地ip地址
import java.net.Inet4Address;
import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.List;
/**
* @author Becolette
* @description TODO
* @date 2015-11-5 下午01:58:46
*/
public class IpAddress {
public static String find() {
List<String> ips = new ArrayList<String>();
// 返回所有網路介面的一個枚舉實例
Enumeration<?> allNetInterfaces = null;
try {
allNetInterfaces = NetworkInterface.getNetworkInterfaces();
} catch (SocketException e) {
e.printStackTrace();
}
InetAddress ip = null;
while (allNetInterfaces.hasMoreElements()) {
NetworkInterface netInterface = (NetworkInterface) allNetInterfaces.nextElement();
Enumeration<InetAddress> addresses = netInterface.getInetAddresses();
while (addresses.hasMoreElements()) {
// 獲得當前網路介面
ip = (InetAddress) addresses.nextElement();
if (ip != null && ip instanceof Inet4Address && ip.getHostAddress().indexOf(".") != -1) {
ips.add(ip.getHostAddress());
}
}
}
if (ips.size() == 1) {
return ips.get(0);
} else {
for (String ipa : ips) {
if (!"127.0.0.1".equals(ipa)) {
return ipa;
}
}
}
return MacAddress.find();
}
}