Java获取本机ip和服务器ip

1.获取服务器IP[en]1. Get the server IP

1 String addr = InetAddress.getLocalHost().getHostAddress();//获得本机IP

第二步,获取客户端本机IP[en]Second, obtain the client native IP

1 String remoteAddr = request.getRemoteAddr();
 2 String forwarded = request.getHeader("X-Forwarded-For");
 3 String realIp = request.getHeader("X-Real-IP");
 4 String ip = null;
 5 if (realIp == null) {
 6     if (forwarded == null) {
 7         ip = remoteAddr;
 8     } else {
 9         ip = remoteAddr + "/" + forwarded.split(",")[0];
10     }
11 } else {
12     if (realIp.equals(forwarded)) {
13         ip = realIp;
14     } else {
15         if(forwarded != null){
16             forwarded = forwarded.split(",")[0];
17         }
18         ip = realIp + "/" + forwarded;
19     }
20 }

Original: https://www.cnblogs.com/jpfss/p/12192801.html
Author: 星朝
Title: Java获取本机ip和服务器ip

原创文章受到原创版权保护。转载请注明出处:https://www.johngo689.com/6139/

转载文章受原作者版权保护。转载请注明原作者出处!

(0)

大家都在看

发表回复

登录后才能评论
免费咨询
免费咨询
扫码关注
扫码关注
联系站长

站长Johngo!

大数据和算法重度研究者!

持续产出大数据、算法、LeetCode干货,以及业界好资源!

2022012703491714

微信来撩,免费咨询:xiaozhu_tec

分享本页
返回顶部