李林超博客
首页
归档
留言
友链
动态
关于
归档
留言
友链
动态
关于
首页
Java
正文
获取IP工具类
Leefs
2020-02-13 PM
2639℃
0条
# 获取IP工具类 **通过IpAddressUtil工具类来获取IP地址** ```java package com.mmit.modules.user.Util; import java.math.BigInteger; import java.net.InetAddress; import java.net.UnknownHostException; import java.util.*; import javax.servlet.http.HttpServletRequest; import org.apache.commons.validator.routines.InetAddressValidator; import com.googlecode.ipv6.IPv6Network; public class IpAddressUtil { /** * 十进制转十六进制 * @param x * @return */ public static String get16(int x){ return Integer.toHexString(x); } public static long get10(String s){ Long l=Long.parseLong(s, 16); return l; } public static String getIpAddress(HttpServletRequest request ){ String ipAddress = request.getHeader("x-forwarded-for"); if(ipAddress == null || ipAddress.length() == 0 || "unknown".equalsIgnoreCase(ipAddress)) { ipAddress = request.getHeader("Proxy-Client-IP"); } if(ipAddress == null || ipAddress.length() == 0 || "unknown".equalsIgnoreCase(ipAddress)) { ipAddress = request.getHeader("WL-Proxy-Client-IP"); } if(ipAddress == null || ipAddress.length() == 0 || "unknown".equalsIgnoreCase(ipAddress)) { ipAddress = request.getRemoteAddr(); if(ipAddress.equals("127.0.0.1") || ipAddress.equals("0:0:0:0:0:0:0:1")){ //根据网卡取本机配置的IP InetAddress inet=null; try { inet = InetAddress.getLocalHost(); } catch (UnknownHostException e) { e.printStackTrace(); } ipAddress= inet.getHostAddress(); } } //对于通过多个代理的情况,第一个IP为客户端真实IP,多个IP按照','分割 if(ipAddress!=null && ipAddress.length()>15){ //"***.***.***.***".length() = 15 if(ipAddress.indexOf(",")>0){ ipAddress = ipAddress.substring(0,ipAddress.indexOf(",")); } } return ipAddress; } public static String getIpDispose(Map
map,String ipAddress){ Set
> entries = map.entrySet(); List
list = new ArrayList
(); Iterator
> iterator = entries.iterator(); while (iterator.hasNext()) { Map.Entry
entry = iterator.next(); String[] split = entry.getValue().trim().split(","); String ipStart =split[0]; String ipEnd = split[1]; list.add(ipStart); list.add(ipEnd); list.add(ipAddress); Collections.sort(list); if (list.get(1).equals(ipAddress)){ return entry.getKey(); } } return null; } public static boolean checkIpValidator(String ip){ if (ip.contains("/")) return IpAddressUtil.ipv6MaskToRange(ip) != null; return InetAddressValidator.getInstance().isValid(ip); } public static boolean isIPv4Address(String ip){ return InetAddressValidator.getInstance().isValidInet4Address(ip); } public static boolean isIPv6Address(String ip){ return InetAddressValidator.getInstance().isValidInet6Address(ip); } public static BigInteger[] ipv6MaskToRange(String ipv6Mask){ BigInteger[] ipNums = null; try{ IPv6Network ipv6 = IPv6Network.fromString(ipv6Mask); ipNums = new BigInteger[2]; ipNums[0] = ipv6.getFirst().toBigInteger(); ipNums[1] = ipv6.getLast().toBigInteger(); }catch(Exception e){ } return ipNums; } /** * 将字符串形式的ip地址转换为BigInteger * * @param ipInString * 字符串形式的ip地址 * @return 整数形式的ip地址 */ public static BigInteger StringToBigInt(String ipInString) { ipInString = ipInString.replace(" ", ""); byte[] bytes; if (ipInString.contains(":")) bytes = ipv6ToBytes(ipInString); else bytes = ipv4ToBytes(ipInString); return new BigInteger(bytes); } /** * 将整数形式的ip地址转换为字符串形式 * * @param ipInBigInt * 整数形式的ip地址 * @return 字符串形式的ip地址 */ public static String BigIntToString(BigInteger ipInBigInt) { byte[] bytes = ipInBigInt.toByteArray(); byte[] unsignedBytes = Arrays.copyOfRange(bytes, 1, bytes.length); // 去除符号位 try { String ip = InetAddress.getByAddress(unsignedBytes).toString(); return ip.substring(ip.indexOf('/') + 1).trim(); } catch (UnknownHostException e) { throw new RuntimeException(e); } } /** * ipv6地址转有符号byte[17] */ private static byte[] ipv6ToBytes(String ipv6) { byte[] ret = new byte[17]; ret[0] = 0; int ib = 16; boolean comFlag = false;// ipv4混合模式标记 if (ipv6.startsWith(":"))// 去掉开头的冒号 ipv6 = ipv6.substring(1); String groups[] = ipv6.split(":"); for (int ig = groups.length - 1; ig > -1; ig--) {// 反向扫描 if (groups[ig].contains(".")) { // 出现ipv4混合模式 byte[] temp = ipv4ToBytes(groups[ig]); ret[ib--] = temp[4]; ret[ib--] = temp[3]; ret[ib--] = temp[2]; ret[ib--] = temp[1]; comFlag = true; } else if ("".equals(groups[ig])) { // 出现零长度压缩,计算缺少的组数 int zlg = 9 - (groups.length + (comFlag ? 1 : 0)); while (zlg-- > 0) {// 将这些组置0 ret[ib--] = 0; ret[ib--] = 0; } } else { int temp = Integer.parseInt(groups[ig], 16); ret[ib--] = (byte) temp; ret[ib--] = (byte) (temp >> 8); } } return ret; } /** * ipv4地址转有符号byte[5] */ private static byte[] ipv4ToBytes(String ipv4) { byte[] ret = new byte[5]; ret[0] = 0; // 先找到IP地址字符串中.的位置 int position1 = ipv4.indexOf("."); int position2 = ipv4.indexOf(".", position1 + 1); int position3 = ipv4.indexOf(".", position2 + 1); // 将每个.之间的字符串转换成整型 ret[1] = (byte) Integer.parseInt(ipv4.substring(0, position1)); ret[2] = (byte) Integer.parseInt(ipv4.substring(position1 + 1, position2)); ret[3] = (byte) Integer.parseInt(ipv4.substring(position2 + 1, position3)); ret[4] = (byte) Integer.parseInt(ipv4.substring(position3 + 1)); return ret; } } ``` **工具类获取IP的静态方法调用:** ```java String ipAddress = IpAddressUtil.getIpAddress(request); ```
标签:
Java工具类
非特殊说明,本博所有文章均为博主原创。
如若转载,请注明出处:
https://lilinchao.com/archives/594.html
上一篇
数据结构和算法学习--线索二叉树
下一篇
数据结构和算法学习--堆排序
评论已关闭
栏目分类
随笔
2
Java
326
大数据
229
工具
31
其它
25
GO
47
NLP
4
标签云
SpringCloud
Redis
Netty
工具
稀疏数组
MyBatisX
MyBatis-Plus
Linux
Quartz
并发线程
Flink
SpringCloudAlibaba
Yarn
查找
Java
Eclipse
Http
序列化和反序列化
MyBatis
正则表达式
LeetCode刷题
VUE
Python
随笔
链表
Ubuntu
Nacos
排序
Spring
Jenkins
友情链接
申请
范明明
庄严博客
Mx
陶小桃Blog
虫洞
评论已关闭