快上网专注成都网站设计 成都网站制作 成都网站建设
成都网站建设公司服务热线:028-86922220

网站建设知识

十年网站开发经验 + 多家企业客户 + 靠谱的建站团队

量身定制 + 运营维护+专业推广+无忧售后,网站问题一站解决

java获取ip代码 java 获取IP

java如何获取当前登录ip

第一种:获取本机的IP

在景德镇等地区,都构建了全面的区域性战略布局,加强发展的系统性、市场前瞻性、产品创新能力,以专注、极致的服务理念,为客户提供做网站、网站设计 网站设计制作按需求定制设计,公司网站建设,企业网站建设,品牌网站设计,网络营销推广,外贸营销网站建设,景德镇网站建设费用合理。

EnumerationNetworkInterface

netInterfaces

=

null;

try

{

netInterfaces

=

NetworkInterface.getNetworkInterfaces();

while

(netInterfaces.hasMoreElements())

{

NetworkInterface

ni

=

netInterfaces.nextElement();

System.out.println("DisplayName:"

+

ni.getDisplayName());

System.out.println("Name:"

+

ni.getName());

EnumerationInetAddress

ips

=

ni.getInetAddresses();

while

(ips.hasMoreElements())

{

System.out.println("IP:"

+

ips.nextElement().getHostAddress());

ipTemp=

ni.getInetAddresses().nextElement().getHostAddress();

if(ipTemp!="127.0.0.1"

!"127.0.0.1".equals(ipTemp))

{

ip=ipTemp;

}

}

}

}catch(Exception

ee)

{

ee.printStackTrace();

}

第二种:也是本机的:

InetAddress

addr

=

InetAddress.getLocalHost();

ip=addr.getHostAddress().toString();//获得本机IP

java怎么获取请求的ip

java获取外网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,如果没有配置外网IP则返回它

String netip = null;// 外网IP

EnumerationNetworkInterface netInterfaces =

NetworkInterface.getNetworkInterfaces();

InetAddress ip = null;

boolean finded = false;// 是否找到外网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) {// 外网IP

netip = ip.getHostAddress();

finded = true;

break;

} else if (ip.isSiteLocalAddress()

!ip.isLoopbackAddress()

ip.getHostAddress().indexOf(":") == -1) {// 内网IP

localip = ip.getHostAddress();

}

}

}

if (netip != null !"".equals(netip)) {

return netip;

} else {

return localip;

}

}

}

JAVA怎么获取IP地址

这个是获取不到的,因为有代理、端口映射等等转发情况的存在。为什么不保存相对路径/域名/或者在服务器上某个配置文件中配置域名/数据库中一个表/数据库中某个字段保存当前服务器的ip地址呢?

如何用 Java 获取系统 IP

import java.net.*;

public class Test6 {

public static void main(String[] args) {

// TODO Auto-generated method stub

InetAddress ia=null;

try {

ia=ia.getLocalHost();

String localname=ia.getHostName();

String localip=ia.getHostAddress();

System.out.println("本机名称是:"+ localname);

System.out.println("本机的ip是 :"+localip);

} catch (Exception e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

}

}


网站栏目:java获取ip代码 java 获取IP
分享网址:http://6mz.cn/article/higpjd.html

其他资讯